
import request from '@/utils/request'


export const tagGroupList = (data: any) => {
    return request({
        url: `api/tag_groups`,
        method: 'get',
        params: data

    })
}


export const addTagGroup = (data: any) => {
    return request({
        url: `api/tag_group`,
        method: 'post',
        data
    })
}

export const upTagGroup = (data: any, id) => {
    return request({
        url: `api/tag_group/${id}`,
        method: 'put',
        data: data

    })
}

export const deleteTagGroup = (id) => {
    return request({
        url: `api/tag_group/${id}`,
        method: 'delete'

    })
}


export const tagList = (data: any) => {
    return request({
        url: `api/tags`,
        method: 'get',
        params: data

    })
}


export const addTag = (data: any) => {
    return request({
        url: `api/tag`,
        method: 'post',
        data
    })
}

export const upTag = (data: any, id) => {
    return request({
        url: `api/tag/${id}`,
        method: 'put',
        data: data

    })
}

export const deleteTag = (id) => {
    return request({
        url: `api/tag/${id}`,
        method: 'delete'

    })
}

// 
export const tagContacts = (data) => {
    return request({
        url: `api/tag/contacts`,
        method: 'get',
        params: data
    })
}

// 成员绑定标签
export const tagBindContact = (data) => {
    return request({
        url: `api/tag/bind_contact`,
        method: 'post',
        data
    })
}

// 解绑

export const tagUnBindContact = (data) => {
    return request({
        url: `api/tag/unbind_contact`,
        method: 'post',
        data
    })
}
// 成员标签切换

export const tagSwitchContact = (data) => {
    return request({
        url: `api/tag/switch_contact`,
        method: 'post',
        data
    })
}


// 查询已选择的标签

export const tagSelected = async (data, ids) => {

    try {
        const groupRes = await tagGroupList(data);

        if (!groupRes?.list) {
            return [];
        }

        const groups = groupRes.list;
        const tags = await Promise.all(
            groups.map(async (group) => {
                const tagsRes = await tagList({
                    robot_id: data.robot_id,
                    tag_group_id: group.id,
                    page: 1,
                    page_size: 100
                });
                return tagsRes?.list || [];
            })
        ).then(results => results.flatMap(result => result));

        const idsSet = new Set(ids.split(','));
        const selectedList = tags.filter(tag => idsSet.has(String(tag.id)));

        return selectedList;
    } catch (error) {
        console.error('Error in tagSelected:', error);
        throw error;
    }
    
}


// 获取所有标签
export const getAllTags = async (data) => {
    return request({
        url: `api/tag_and_groups`,
        method: 'get',
        params: data
    })
}

// 获取所有标签下的用户
export const getAllTagsUsers = async (data) => {
    return request({
        url: `api/tag_and_contacts`,
        method: 'get',
        params: data
    })
}

// 客户绑定标签
export const userBindTag = async (data) => {
    return request({
        url: `api/tag/modify_contact`,
        method: 'post',
        data
    })
}