'Returns autocomplete data for tag values ordered from most frequently used to
least frequently used.'

def _get_tag_autocomplete_values(self, trans, q, limit, timestamp, user=None, item=None, item_class=None):

    tag_name_and_value = q.split(':')
    tag_name = tag_name_and_value[0]
    tag_value = tag_name_and_value[1]
    tag = self.get_tag_handler(trans).get_tag_by_name(tag_name)
    if (tag is None):
        return ''
    if ((item is None) and (item_class is None)):
        raise RuntimeError('Both    item    and    item_class    cannot    be    None')
    elif (item is not None):
        item_class = item.__class__
    item_tag_assoc_class = self.get_tag_handler(trans).get_tag_assoc_class(item_class)
    cols_to_select = [item_tag_assoc_class.table.c.value, func.count('*')]
    from_obj = item_tag_assoc_class.table.join(item_class.table).join(trans.app.model.Tag.table)
    where_clause = and_((item_tag_assoc_class.table.c.user_id == user.id), (trans.app.model.Tag.table.c.id == tag.id), item_tag_assoc_class.table.c.value.like((tag_value + '%')))
    order_by = [func.count('*').desc(), item_tag_assoc_class.table.c.value]
    group_by = item_tag_assoc_class.table.c.value
    query = select(columns=cols_to_select, from_obj=from_obj, whereclause=where_clause, group_by=group_by, order_by=order_by, limit=limit)
    result_set = trans.sa_session.execute(query)
    ac_data = ("#Header|Your    Values    for    '%s'\n" % tag_name)
    tag_uname = self._get_usernames_for_tag(trans, trans.user, tag, item_class, item_tag_assoc_class)[0]
    for row in result_set:
        ac_data += (((((tag_uname + ':') + row[0]) + '|') + row[0]) + '\n')
    return ac_data
