const EMPTY_SERIES: number[][] = []

function ds(data: number[][] | undefined): number[][] {
    if (!data?.length) return EMPTY_SERIES
    return data
}

/** Worker 完整计算后，剔除当前未使用的值类型，降低内存占用 */
export function compactShuWaveResult(data: any, value: number) {
    const waveDatas = data.waveDatas.map((wd: any) => {
        if (value === 1) {
            return {
                ...wd,
                shunshiF: {
                    shunshiFA: ds(wd.shunshiF.shunshiFA),
                    shunshiFB: ds(wd.shunshiF.shunshiFB),
                    shunshiFC: ds(wd.shunshiF.shunshiFC)
                },
                shunshiS: { shunshiSA: EMPTY_SERIES, shunshiSB: EMPTY_SERIES, shunshiSC: EMPTY_SERIES }
            }
        }
        return {
            ...wd,
            shunshiF: { shunshiFA: EMPTY_SERIES, shunshiFB: EMPTY_SERIES, shunshiFC: EMPTY_SERIES },
            shunshiS: {
                shunshiSA: ds(wd.shunshiS.shunshiSA),
                shunshiSB: ds(wd.shunshiS.shunshiSB),
                shunshiSC: ds(wd.shunshiS.shunshiSC)
            }
        }
    })
    return { ...data, waveDatas }
}

export function compactRmsWaveResult(data: any, value: number) {
    const waveDatas = data.waveDatas.map((wd: any) => {
        if (value === 1) {
            return {
                ...wd,
                RMSFWave: {
                    rmsFA: ds(wd.RMSFWave.rmsFA),
                    rmsFB: ds(wd.RMSFWave.rmsFB),
                    rmsFC: ds(wd.RMSFWave.rmsFC)
                },
                RMSSWave: { rmsSA: EMPTY_SERIES, rmsSB: EMPTY_SERIES, rmsSC: EMPTY_SERIES }
            }
        }
        return {
            ...wd,
            RMSFWave: { rmsFA: EMPTY_SERIES, rmsFB: EMPTY_SERIES, rmsFC: EMPTY_SERIES },
            RMSSWave: {
                rmsSA: ds(wd.RMSSWave.rmsSA),
                rmsSB: ds(wd.RMSSWave.rmsSB),
                rmsSC: ds(wd.RMSSWave.rmsSC)
            }
        }
    })
    return { ...data, waveDatas }
}
