// waveData.worker.js
self.addEventListener('message', function (e) {
    const { wp, value, iphasic, isOpen, boxoList, requestId } = e.data

    // 处理波形数据的函数
    const fliteWaveData = (wp, step) => {
        // 将原有的fliteWaveData函数实现复制到这里
        const shunData = wp.listWaveData
        const pt = Number(wp.pt) / 1000
        const ct = Number(wp.ct)
        const titleList = wp.waveTitle
        let xishu = pt
        let aTitle = '',
            bTitle = '',
            cTitle = '',
            unit = '电压'
        let ifmax = 0,
            ifmin = 0,
            ismax = 0,
            ismin = 0

        const shunshiFA = []
        const shunshiFB = []
        const shunshiFC = []
        const shunshiSA = []
        const shunshiSB = []
        const shunshiSC = []

        if (shunData.length > 0) {
            if (titleList[iphasic * step + 1]?.substring(0, 1) !== 'U') {
                xishu = ct
                unit = '电流'
            }

            for (let i = 1; i <= iphasic; i++) {
                switch (i) {
                    case 1:
                        aTitle = titleList[iphasic * step + i]?.substring(1) || ''
                        break
                    case 2:
                        bTitle = titleList[iphasic * step + i]?.substring(1) || ''
                        break
                    case 3:
                        cTitle = titleList[iphasic * step + i]?.substring(1) || ''
                        break
                }
            }

            if (shunData[0][iphasic * step + 1] !== undefined) {
                ifmax = shunData[0][iphasic * step + 1] * xishu
                ifmin = shunData[0][iphasic * step + 1] * xishu
                ismax = shunData[0][iphasic * step + 1]
                ismin = shunData[0][iphasic * step + 1]
            }

            for (let shun = 0; shun < shunData.length; shun++) {
                if (shunData[shun][iphasic * step + 1] === undefined) {
                    break
                }

                switch (iphasic) {
                    case 1:
                        const shunFirstA = shunData[shun][iphasic * step + 1] * xishu
                        shunshiFA.push([shunData[shun][0], shunFirstA])
                        ifmax = Math.max(ifmax, shunFirstA)
                        ifmin = Math.min(ifmin, shunFirstA)

                        const shunSecondA = shunData[shun][iphasic * step + 1]
                        shunshiSA.push([shunData[shun][0], shunSecondA])
                        ismax = Math.max(ismax, shunSecondA)
                        ismin = Math.min(ismin, shunSecondA)
                        break
                    case 2:
                        const shunFirstA2 = shunData[shun][iphasic * step + 1] * xishu
                        const shunFirstB2 = shunData[shun][iphasic * step + 2] * xishu
                        shunshiFA.push([shunData[shun][0], shunFirstA2])
                        shunshiFB.push([shunData[shun][0], shunFirstB2])
                        ifmax = Math.max(ifmax, shunFirstA2, shunFirstB2)
                        ifmin = Math.min(ifmin, shunFirstA2, shunFirstB2)

                        const shunSecondA2 = shunData[shun][iphasic * step + 1]
                        const shunSecondB2 = shunData[shun][iphasic * step + 2]
                        shunshiSA.push([shunData[shun][0], shunSecondA2])
                        shunshiSB.push([shunData[shun][0], shunSecondB2])
                        ismax = Math.max(ismax, shunSecondA2, shunSecondB2)
                        ismin = Math.min(ismin, shunSecondA2, shunSecondB2)
                        break
                    case 3:
                        const shunFirstA3 = shunData[shun][iphasic * step + 1] * xishu
                        const shunFirstB3 = shunData[shun][iphasic * step + 2] * xishu
                        const shunFirstC3 = shunData[shun][iphasic * step + 3] * xishu
                        shunshiFA.push([shunData[shun][0], shunFirstA3])
                        shunshiFB.push([shunData[shun][0], shunFirstB3])
                        shunshiFC.push([shunData[shun][0], shunFirstC3])
                        ifmax = Math.max(ifmax, shunFirstA3, shunFirstB3, shunFirstC3)
                        ifmin = isOpen
                            ? Math.min(ifmin, shunFirstA3, shunFirstC3)
                            : Math.min(ifmin, shunFirstA3, shunFirstB3, shunFirstC3)

                        const shunSecondA3 = shunData[shun][iphasic * step + 1]
                        const shunSecondB3 = shunData[shun][iphasic * step + 2]
                        const shunSecondC3 = shunData[shun][iphasic * step + 3]
                        shunshiSA.push([shunData[shun][0], shunSecondA3])
                        shunshiSB.push([shunData[shun][0], shunSecondB3])
                        shunshiSC.push([shunData[shun][0], shunSecondC3])
                        ismax = Math.max(ismax, shunSecondA3, shunSecondB3, shunSecondC3)
                        ismin = isOpen
                            ? Math.min(ismin, shunSecondA3, shunSecondC3)
                            : Math.min(ismin, shunSecondA3, shunSecondB3, shunSecondC3)
                        break
                }
            }
        }

        const instantF = { max: ifmax, min: ifmin }
        const instantS = { max: ismax, min: ismin }
        const shunshiF = { shunshiFA, shunshiFB, shunshiFC }
        const shunshiS = { shunshiSA, shunshiSB, shunshiSC }
        const title = { aTitle, bTitle, cTitle, unit }

        return { instantF, instantS, shunshiF, shunshiS, title, unit }
    }

    // 处理标题
    let titles = ''
    if (boxoList.systemType == 'pms') {
        titles =
            '变电站名称:' +
            boxoList.powerStationName +
            '　监测点名称:' +
            boxoList.measurementPointName +
            '　发生时刻:' +
            boxoList.startTime +
            '　暂降(骤升)幅值:' +
            (boxoList.featureAmplitude * 100).toFixed(2) +
            '%　持续时间:' +
            boxoList.duration +
            's'
    } else if (boxoList.systemType == 'ZL') {
        titles =
            (boxoList.engineeringName == undefined ? '' : ' 项目名称:' + boxoList.engineeringName) +
            '　监测点名称:' +
            boxoList.equipmentName +
            '　发生时刻:' +
            boxoList.startTime +
            '　暂降(骤升)幅值:' +
            boxoList.evtParamVVaDepth +
            '%　持续时间:' +
            boxoList.evtParamTm +
            's'
    } else if (boxoList.systemType == 'YPT') {
        titles =
            (boxoList.engineeringName == undefined ? '' : ' 项目名称:' + boxoList.engineeringName) +
            '　监测点名称:' +
            boxoList.lineName +
            '　发生时刻:' +
            boxoList.startTime +
            '　暂降(骤升)幅值:' +
            (boxoList.featureAmplitude * 100).toFixed(2) +
            '%　持续时间:' +
            boxoList.persistTime +
            's'
    } else {
        titles =
            '变电站名称:' +
            boxoList.subName +
            '　监测点名称:' +
            boxoList.lineName +
            '　发生时刻:' +
            boxoList.startTime +
            '　暂降(骤升)幅值:' +
            (boxoList.featureAmplitude * 100).toFixed(2) +
            '%　持续时间:' +
            boxoList.duration +
            's'
    }

    const iphasicValue = wp.iphasic || 1
    const picCounts = (wp.waveTitle.length - 1) / iphasicValue
    const waveDatas = []

    for (let i = 0; i < picCounts; i++) {
        const data = fliteWaveData(wp, i)
        waveDatas.push(data)
    }

    const time = wp.time
    const type = wp.waveType
    let severity = wp.yzd

    if (severity < 0) {
        severity = '/'
        type = '/'
    }

    // 将处理结果发送回主线程
    self.postMessage({
        requestId,
        success: true,
        waveDatas,
        time,
        type,
        severity,
        titles,
        iphasic: iphasicValue
    })
})
