import { cloneDeep } from "lodash-es";
export const useSearchInfoArray = (target: any[]): any => {
    //深拷贝一份，不影响源数据
    let targetClone: any[] = cloneDeep(target);
    let returnTarget: any[] = [];
    //如果是数组
    if (Array.isArray(targetClone) && targetClone.length) {
        targetClone.forEach(item => {
            let obj = {
                value: item.id,
                label: item.name,
                ...item
            };
            returnTarget.push(obj);
        });
    }
    return returnTarget;
};
export const useSearchCodeInfoArray = (target: any[]): any => {
    //深拷贝一份，不影响源数据
    let targetClone: any[] = cloneDeep(target);
    let returnTarget: any[] = [];
    //如果是数组
    if (Array.isArray(targetClone) && targetClone.length) {
        targetClone.forEach(item => {
            let obj = {
                value: item.code,
                label: item.name,
                ...item
            };
            returnTarget.push(obj);
        });
    }
    return returnTarget;
};
export const useSearchInfoObject = (target: Record<string, any>): any => {
    //深拷贝一份，不影响源数据
    let targetClone: Record<string, any> = cloneDeep(target);
    let returnTarget: any[] = [];
    if (Array.isArray(targetClone) && targetClone.length) {
        targetClone.forEach(item => {
            let obj = {
                value: item.id,
                label: item.name,
                ...item
            };
            returnTarget.push(obj);
        });
        return returnTarget;
    }
    //如果是对象
    if (Object.prototype.toString.call(target) === "[object Object]") {
        for (let key in targetClone) {
            let obj: Record<string, any> = {
                value: isNaN(parseInt(key)) ? key : parseInt(key), //是字符串数字就转为数组
                label: targetClone[key]
            };
            returnTarget.push(obj);
        }
    }

    return returnTarget;
};

//search配置项添加数据
export const useSetSearchData = (target: any[], data: Record<string, any>) => {
    let targetClone = cloneDeep(target);
    let length = targetClone.length;
    //如果是数组
    if (Array.isArray(targetClone) && length) {
        for (let i = 0; i < length; i++) {
            let item = targetClone[i];
            if (data[item.prop] || data[item.label]) {
                item.options = data[item.prop] || data[item.label];
            }
        }
    }
    return targetClone;
};
export const useSearchStockCodeInfoArray = (target: any[]): any => {
    //深拷贝一份，不影响源数据
    let targetClone: any[] = cloneDeep(target);
    let returnTarget: any[] = [];
    //如果是数组
    if (Array.isArray(targetClone) && targetClone.length) {
        targetClone.forEach(item => {
            let obj = {
                value: item.code,
                label: item.name,
                ...item
            };
            returnTarget.push(obj);
        });
    }
    return returnTarget;
};
