"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.reverseIfNeed = exports.loopSeriesRule = exports.chnWeek3Rule = exports.chnWeek2Rule = exports.chnNumberRule = exports.extendNumberRule = exports.otherRule = exports.numberRule = exports.dateRule = void 0;

const fill_1 = require("./utils/fill"), types_1 = require("./types"), series_converters_1 = require("./series-converters"), types_2 = require("./types");

function reverseIfNeed(data, reverse) {
    return reverse ? data.reverse() : data;
}

exports.dateRule = {
    type: types_1.DATA_TYPE.DATE,
    priority: 1100,
    match: (cellData, accessor) => "string" == typeof (null == cellData ? void 0 : cellData.v) && series_converters_1.converterManager.getConverter(cellData.v) instanceof series_converters_1.DateConverter,
    isContinue: (prev, cur) => prev.type === types_1.DATA_TYPE.DATE,
    applyFunctions: {
        [types_1.APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a;
            const {data: data} = dataWithIndex, converter = series_converters_1.converterManager.getConverter(null === (_a = data[0]) || void 0 === _a ? void 0 : _a.v);
            return direction === types_1.Direction.LEFT || direction === types_1.Direction.UP ? (data.reverse(), 
            (0, fill_1.fillSeries)(data, len, direction, converter).reverse()) : (0, fill_1.fillSeries)(data, len, direction, converter);
        }
    }
}, exports.numberRule = {
    type: types_1.DATA_TYPE.NUMBER,
    priority: 1e3,
    match: cellData => "number" == typeof (null == cellData ? void 0 : cellData.v) || (null == cellData ? void 0 : cellData.t) === types_2.CellValueType.NUMBER,
    isContinue: (prev, cur) => prev.type === types_1.DATA_TYPE.NUMBER,
    applyFunctions: {
        [types_1.APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            const {data: data} = dataWithIndex;
            return direction === types_1.Direction.LEFT || direction === types_1.Direction.UP ? (data.reverse(), 
            (0, fill_1.fillSeries)(data, len, direction).reverse()) : (0, fill_1.fillSeries)(data, len, direction);
        }
    }
}, exports.otherRule = {
    type: types_1.DATA_TYPE.OTHER,
    priority: 0,
    match: () => !0,
    isContinue: (prev, cur) => prev.type === types_1.DATA_TYPE.OTHER
}, exports.extendNumberRule = {
    type: types_1.DATA_TYPE.EXTEND_NUMBER,
    priority: 900,
    match: cellData => {
        var _a;
        return (0, fill_1.matchExtendNumber)(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "").isExtendNumber;
    },
    isContinue: (prev, cur) => {
        var _a, _b, _c;
        if (prev.type === types_1.DATA_TYPE.EXTEND_NUMBER) {
            const {beforeTxt: beforeTxt, afterTxt: afterTxt} = (0, fill_1.matchExtendNumber)(`${null !== (_b = null === (_a = prev.cellData) || void 0 === _a ? void 0 : _a.v) && void 0 !== _b ? _b : ""}` || ""), {beforeTxt: curBeforeTxt, afterTxt: curAfterTxt} = (0, 
            fill_1.matchExtendNumber)(`${null !== (_c = null == cur ? void 0 : cur.v) && void 0 !== _c ? _c : ""}` || "");
            if (beforeTxt === curBeforeTxt && afterTxt === curAfterTxt) return !0;
        }
        return !1;
    },
    applyFunctions: {
        [types_1.APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a;
            const {data: data} = dataWithIndex, isReverse = direction === types_1.Direction.UP || direction === types_1.Direction.LEFT;
            let step;
            if (1 === data.length) return step = isReverse ? -1 : 1, reverseIfNeed((0, fill_1.fillExtendNumber)(data, len, step), isReverse);
            const dataNumArr = [];
            for (let i = 0; i < data.length; i++) {
                const txt = `${null === (_a = data[i]) || void 0 === _a ? void 0 : _a.v}`;
                txt && dataNumArr.push(Number((0, fill_1.matchExtendNumber)(txt).matchTxt));
            }
            return isReverse && (data.reverse(), dataNumArr.reverse()), (0, fill_1.isEqualDiff)(dataNumArr) ? (step = dataNumArr[1] - dataNumArr[0], 
            reverseIfNeed((0, fill_1.fillExtendNumber)(data, len, step), isReverse)) : (0, fill_1.fillCopy)(data, len);
        }
    }
}, exports.chnNumberRule = {
    type: types_1.DATA_TYPE.CHN_NUMBER,
    priority: 830,
    match: cellData => {
        var _a;
        return !!(0, fill_1.isChnNumber)(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "");
    },
    isContinue: (prev, cur) => prev.type === types_1.DATA_TYPE.CHN_NUMBER,
    applyFunctions: {
        [types_1.APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a, _b;
            const {data: data} = dataWithIndex, isReverse = direction === types_1.Direction.LEFT || direction === types_1.Direction.UP;
            if (1 === data.length) {
                let step;
                return step = isReverse ? -1 : 1, reverseIfNeed((0, fill_1.fillChnNumber)(data, len, step), isReverse);
            }
            let hasWeek = !1;
            for (let i = 0; i < data.length; i++) {
                if ("日" === (null === (_a = data[i]) || void 0 === _a ? void 0 : _a.v)) {
                    hasWeek = !0;
                    break;
                }
            }
            const dataNumArr = [];
            let weekIndex = 0;
            for (let i = 0; i < data.length; i++) {
                const formattedValue = `${null === (_b = data[i]) || void 0 === _b ? void 0 : _b.v}`;
                "日" === formattedValue ? 0 === i ? dataNumArr.push(0) : (weekIndex++, dataNumArr.push(7 * weekIndex)) : hasWeek && (0, 
                fill_1.chineseToNumber)(formattedValue) > 0 && (0, fill_1.chineseToNumber)(formattedValue) < 7 ? dataNumArr.push((0, 
                fill_1.chineseToNumber)(formattedValue) + 7 * weekIndex) : dataNumArr.push((0, fill_1.chineseToNumber)(formattedValue));
            }
            if (isReverse && (data.reverse(), dataNumArr.reverse()), (0, fill_1.isEqualDiff)(dataNumArr)) {
                const step = dataNumArr[1] - dataNumArr[0];
                return reverseIfNeed((0, fill_1.fillChnNumber)(data, len, step), isReverse);
            }
            return (0, fill_1.fillCopy)(data, len);
        }
    }
}, exports.chnWeek2Rule = {
    type: types_1.DATA_TYPE.CHN_WEEK2,
    priority: 820,
    match: cellData => {
        var _a;
        return !!(0, fill_1.isChnWeek2)(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "");
    },
    isContinue: (prev, cur) => prev.type === types_1.DATA_TYPE.CHN_WEEK2,
    applyFunctions: {
        [types_1.APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a;
            const {data: data} = dataWithIndex, isReverse = direction === types_1.Direction.LEFT || direction === types_1.Direction.UP;
            if (1 === data.length) {
                let step;
                return step = isReverse ? -1 : 1, reverseIfNeed((0, fill_1.fillChnWeek)(data, len, step, 1), isReverse);
            }
            const dataNumArr = [];
            let weekIndex = 0;
            for (let i = 0; i < data.length; i++) {
                const formattedValue = `${null === (_a = data[i]) || void 0 === _a ? void 0 : _a.v}`, lastTxt = null == formattedValue ? void 0 : formattedValue.substr(formattedValue.length - 1, 1);
                "周日" === formattedValue ? 0 === i ? dataNumArr.push(0) : (weekIndex++, dataNumArr.push(7 * weekIndex)) : dataNumArr.push((0, 
                fill_1.chineseToNumber)(lastTxt) + 7 * weekIndex);
            }
            if (isReverse && (data.reverse(), dataNumArr.reverse()), (0, fill_1.isEqualDiff)(dataNumArr)) {
                const step = dataNumArr[1] - dataNumArr[0];
                return reverseIfNeed((0, fill_1.fillChnWeek)(data, len, step, 1), isReverse);
            }
            return (0, fill_1.fillCopy)(data, len);
        }
    }
}, exports.chnWeek3Rule = {
    type: types_1.DATA_TYPE.CHN_WEEK3,
    priority: 810,
    match: cellData => {
        var _a;
        return (0, fill_1.isChnWeek3)(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "");
    },
    isContinue: (prev, cur) => prev.type === types_1.DATA_TYPE.CHN_WEEK3,
    applyFunctions: {
        [types_1.APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a;
            const {data: data} = dataWithIndex, isReverse = direction === types_1.Direction.LEFT || direction === types_1.Direction.UP;
            if (1 === data.length) {
                let step;
                return step = isReverse ? -1 : 1, reverseIfNeed((0, fill_1.fillChnWeek)(data, len, step, 2), isReverse);
            }
            const dataNumArr = [];
            let weekIndex = 0;
            for (let i = 0; i < data.length; i++) {
                const formattedValue = `${null === (_a = data[i]) || void 0 === _a ? void 0 : _a.v}`;
                if (formattedValue) {
                    const lastTxt = formattedValue.substr(formattedValue.length - 1, 1);
                    "星期日" === formattedValue ? 0 === i ? dataNumArr.push(0) : (weekIndex++, dataNumArr.push(7 * weekIndex)) : dataNumArr.push((0, 
                    fill_1.chineseToNumber)(lastTxt) + 7 * weekIndex);
                }
            }
            if (isReverse && (data.reverse(), dataNumArr.reverse()), (0, fill_1.isEqualDiff)(dataNumArr)) {
                const step = dataNumArr[1] - dataNumArr[0];
                return reverseIfNeed((0, fill_1.fillChnWeek)(data, len, step, 2), isReverse);
            }
            return (0, fill_1.fillCopy)(data, len);
        }
    }
}, exports.loopSeriesRule = {
    type: types_1.DATA_TYPE.LOOP_SERIES,
    priority: 800,
    match: cellData => {
        var _a;
        return (0, fill_1.isLoopSeries)(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "");
    },
    isContinue: (prev, cur) => {
        var _a, _b, _c;
        return prev.type === types_1.DATA_TYPE.LOOP_SERIES && (0, fill_1.getLoopSeriesInfo)(`${null !== (_b = null === (_a = prev.cellData) || void 0 === _a ? void 0 : _a.v) && void 0 !== _b ? _b : ""}` || "").name === (0, 
        fill_1.getLoopSeriesInfo)(`${null !== (_c = null == cur ? void 0 : cur.v) && void 0 !== _c ? _c : ""}` || "").name;
    },
    applyFunctions: {
        [types_1.APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a, _b;
            const {data: data} = dataWithIndex, isReverse = direction === types_1.Direction.LEFT || direction === types_1.Direction.UP, {series: series} = (0, 
            fill_1.getLoopSeriesInfo)(`${null === (_a = data[0]) || void 0 === _a ? void 0 : _a.v}` || "");
            if (1 === data.length) {
                let step;
                return step = isReverse ? -1 : 1, reverseIfNeed((0, fill_1.fillLoopSeries)(data, len, step, series), isReverse);
            }
            const dataNumArr = [];
            let cycleIndex = 0;
            for (let i = 0; i < data.length; i++) {
                const formattedValue = `${null === (_b = data[i]) || void 0 === _b ? void 0 : _b.v}`;
                formattedValue && (formattedValue === series[0] ? 0 === i ? dataNumArr.push(0) : (cycleIndex++, 
                dataNumArr.push(cycleIndex * series.length)) : dataNumArr.push(series.indexOf(formattedValue) + 7 * cycleIndex));
            }
            if (isReverse && (data.reverse(), dataNumArr.reverse()), (0, fill_1.isEqualDiff)(dataNumArr)) {
                const step = dataNumArr[1] - dataNumArr[0];
                return reverseIfNeed((0, fill_1.fillLoopSeries)(data, len, step, series), isReverse);
            }
            return (0, fill_1.fillCopy)(data, len);
        }
    }
}, exports.reverseIfNeed = reverseIfNeed;
//# sourceMappingURL=rules.js.map