import { CostFeeItemComputeDto } from '@/api/providerfeeset/providerFeeSetModel';
import type { Ref } from 'vue';
import { cloneDeep } from '../cloneDeep';
import { isArrayLength, isNumber } from "../is"

/**
 * 费用明细-更新目标数组内容
 * @param detailList 原始数组
 * @param allFeeItemList 目标数组
 * @param packageType 包裹类型
 * @returns 
 */
function fee_deal_Expense<T extends Recordable>(detailList, allFeeItemList: Ref<Recordable[]>, packageType: number): T[] {
    if (!isArrayLength(detailList)) return []
    // 全部费用明细
    let allFeeItemComputeDetailsList = []
    // 快速导入的费用id
    const expenseFeeItemId = detailList[0].feeItemId
    if (!isNumber(expenseFeeItemId)) return []
    const filterAllFeeItemList = allFeeItemList.value.filter(item => item.feeItemId !== expenseFeeItemId)
    const allFeeItemComputeDetails = cloneDeep(filterAllFeeItemList)

    allFeeItemComputeDetailsList = detailList.reduce((acc, curr) => {
        if (acc) acc.push({ ...curr, packageType });
        return acc;
    }, allFeeItemComputeDetails);

    return allFeeItemComputeDetailsList
}

/**
 * 过滤vxe-table并且返回校验状态
 * @param results 
 */
async function fee_deal_FilterValidate(results) {
    const results_ = results.filter(item => item !== undefined)
    const isValidate = results_.every((item: any) => {
        if (Object.keys(item).length > 0) return false
        return item
    })
    return isValidate
}

/**
 * 处理费用计算信息
 * @param tableSource 
 */
function fee_deal_FeeItemComputeList<T extends Recordable>(tableSource) {
    if (!isArrayLength(tableSource)) return []
    const feeItemListdataSource = cloneDeep(tableSource) as T[]
    const feeItemComputeList = feeItemListdataSource.map((item: Recordable) => {
        if (item.oldFees) delete item.oldFees; // 删除oldFees对象数据
        return item;
    }) as T[]
    return feeItemComputeList
}

export {
    fee_deal_Expense,
    fee_deal_FilterValidate,
    fee_deal_FeeItemComputeList
}