import { isArrayLength } from '@/utils/is';
import { computed, Ref, unref } from 'vue';
/**
 * 过滤列表以及剩余的列表数据
 * @param sourceList 使用过的列表
 * @param sourceKey 取的列表的key值
 * @param lists 下拉列表数据
 * @param key 取的列表的key值
 * 
 * @returns newSourceLists 剩余列表数据
 * @returns filterNewsList 合并当前选中+剩余列表数据
 * @returns oldList 剩余列表数据
 * 
 */
function useFilterTypeList<T extends Recordable>(sourceList: T, sourceKey: string, lists: Ref, listKey: string) {
    // 过滤对应的key值
    const filterListKey = computed(() => {
        if (isArrayLength(sourceList.dataSource)) {
            return sourceList.dataSource.map(item => item[sourceKey]).filter(key => key)
        } else {
            return sourceList.data.map(item => item[sourceKey]).filter(key => key)
        }
    })

    // 过滤下拉列表数据-剩余类型数据
    const newSourceLists = computed(() => {
        const list = unref(lists).filter(item => !unref(filterListKey).includes(item[listKey]))
        return list
    })

    // 过滤来源列表数据-类型数据
    const oldList = computed(() => {
        const FeeItemList = unref(lists).filter(item => unref(filterListKey).includes(item[listKey]))
        return FeeItemList
    })

    // 合并并且过滤类型数据
    const filterNewsList = (val: number) => {
        // 过滤已选中的类型信息
        const filterData = unref(lists).filter(item => item[listKey] === val)
        // 合并没有选中的类型数据
        const list = [...filterData, ...unref(newSourceLists)]
        return list
    }

    return { newSourceLists, oldList, filterNewsList }
}

export {
    useFilterTypeList
}