import { chineseToNumber, fillChnNumber, fillChnWeek, fillCopy, fillExtendNumber, fillLoopSeries, fillSeries, getLoopSeriesInfo, isChnNumber, isChnWeek2, isChnWeek3, isEqualDiff, isLoopSeries, matchExtendNumber } from "./utils/fill";

import { APPLY_TYPE, DATA_TYPE, Direction } from "./types";

import { converterManager, DateConverter } from "./series-converters";

import { CellValueType } from "./types";

export const dateRule = {
    type: DATA_TYPE.DATE,
    priority: 1100,
    match: (cellData, accessor) => "string" == typeof (null == cellData ? void 0 : cellData.v) && converterManager.getConverter(cellData.v) instanceof DateConverter,
    isContinue: (prev, cur) => prev.type === DATA_TYPE.DATE,
    applyFunctions: {
        [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a;
            const {data: data} = dataWithIndex, converter = converterManager.getConverter(null === (_a = data[0]) || void 0 === _a ? void 0 : _a.v);
            return direction === Direction.LEFT || direction === Direction.UP ? (data.reverse(), 
            fillSeries(data, len, direction, converter).reverse()) : fillSeries(data, len, direction, converter);
        }
    }
};

export const numberRule = {
    type: DATA_TYPE.NUMBER,
    priority: 1e3,
    match: cellData => "number" == typeof (null == cellData ? void 0 : cellData.v) || (null == cellData ? void 0 : cellData.t) === CellValueType.NUMBER,
    isContinue: (prev, cur) => prev.type === DATA_TYPE.NUMBER,
    applyFunctions: {
        [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            const {data: data} = dataWithIndex;
            return direction === Direction.LEFT || direction === Direction.UP ? (data.reverse(), 
            fillSeries(data, len, direction).reverse()) : fillSeries(data, len, direction);
        }
    }
};

export const otherRule = {
    type: DATA_TYPE.OTHER,
    priority: 0,
    match: () => !0,
    isContinue: (prev, cur) => prev.type === DATA_TYPE.OTHER
};

export const extendNumberRule = {
    type: DATA_TYPE.EXTEND_NUMBER,
    priority: 900,
    match: cellData => {
        var _a;
        return matchExtendNumber(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "").isExtendNumber;
    },
    isContinue: (prev, cur) => {
        var _a, _b, _c;
        if (prev.type === DATA_TYPE.EXTEND_NUMBER) {
            const {beforeTxt: beforeTxt, afterTxt: afterTxt} = matchExtendNumber(`${null !== (_b = null === (_a = prev.cellData) || void 0 === _a ? void 0 : _a.v) && void 0 !== _b ? _b : ""}` || ""), {beforeTxt: curBeforeTxt, afterTxt: curAfterTxt} = matchExtendNumber(`${null !== (_c = null == cur ? void 0 : cur.v) && void 0 !== _c ? _c : ""}` || "");
            if (beforeTxt === curBeforeTxt && afterTxt === curAfterTxt) return !0;
        }
        return !1;
    },
    applyFunctions: {
        [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a;
            const {data: data} = dataWithIndex, isReverse = direction === Direction.UP || direction === Direction.LEFT;
            let step;
            if (1 === data.length) return step = isReverse ? -1 : 1, reverseIfNeed(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(matchExtendNumber(txt).matchTxt));
            }
            return isReverse && (data.reverse(), dataNumArr.reverse()), isEqualDiff(dataNumArr) ? (step = dataNumArr[1] - dataNumArr[0], 
            reverseIfNeed(fillExtendNumber(data, len, step), isReverse)) : fillCopy(data, len);
        }
    }
};

export const chnNumberRule = {
    type: DATA_TYPE.CHN_NUMBER,
    priority: 830,
    match: cellData => {
        var _a;
        return !!isChnNumber(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "");
    },
    isContinue: (prev, cur) => prev.type === DATA_TYPE.CHN_NUMBER,
    applyFunctions: {
        [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a, _b;
            const {data: data} = dataWithIndex, isReverse = direction === Direction.LEFT || direction === Direction.UP;
            if (1 === data.length) {
                let step;
                return step = isReverse ? -1 : 1, reverseIfNeed(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 && chineseToNumber(formattedValue) > 0 && chineseToNumber(formattedValue) < 7 ? dataNumArr.push(chineseToNumber(formattedValue) + 7 * weekIndex) : dataNumArr.push(chineseToNumber(formattedValue));
            }
            if (isReverse && (data.reverse(), dataNumArr.reverse()), isEqualDiff(dataNumArr)) {
                const step = dataNumArr[1] - dataNumArr[0];
                return reverseIfNeed(fillChnNumber(data, len, step), isReverse);
            }
            return fillCopy(data, len);
        }
    }
};

export const chnWeek2Rule = {
    type: DATA_TYPE.CHN_WEEK2,
    priority: 820,
    match: cellData => {
        var _a;
        return !!isChnWeek2(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "");
    },
    isContinue: (prev, cur) => prev.type === DATA_TYPE.CHN_WEEK2,
    applyFunctions: {
        [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a;
            const {data: data} = dataWithIndex, isReverse = direction === Direction.LEFT || direction === Direction.UP;
            if (1 === data.length) {
                let step;
                return step = isReverse ? -1 : 1, reverseIfNeed(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(chineseToNumber(lastTxt) + 7 * weekIndex);
            }
            if (isReverse && (data.reverse(), dataNumArr.reverse()), isEqualDiff(dataNumArr)) {
                const step = dataNumArr[1] - dataNumArr[0];
                return reverseIfNeed(fillChnWeek(data, len, step, 1), isReverse);
            }
            return fillCopy(data, len);
        }
    }
};

export const chnWeek3Rule = {
    type: DATA_TYPE.CHN_WEEK3,
    priority: 810,
    match: cellData => {
        var _a;
        return isChnWeek3(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "");
    },
    isContinue: (prev, cur) => prev.type === DATA_TYPE.CHN_WEEK3,
    applyFunctions: {
        [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a;
            const {data: data} = dataWithIndex, isReverse = direction === Direction.LEFT || direction === Direction.UP;
            if (1 === data.length) {
                let step;
                return step = isReverse ? -1 : 1, reverseIfNeed(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(chineseToNumber(lastTxt) + 7 * weekIndex);
                }
            }
            if (isReverse && (data.reverse(), dataNumArr.reverse()), isEqualDiff(dataNumArr)) {
                const step = dataNumArr[1] - dataNumArr[0];
                return reverseIfNeed(fillChnWeek(data, len, step, 2), isReverse);
            }
            return fillCopy(data, len);
        }
    }
};

export const loopSeriesRule = {
    type: DATA_TYPE.LOOP_SERIES,
    priority: 800,
    match: cellData => {
        var _a;
        return isLoopSeries(`${null !== (_a = null == cellData ? void 0 : cellData.v) && void 0 !== _a ? _a : ""}` || "");
    },
    isContinue: (prev, cur) => {
        var _a, _b, _c;
        return prev.type === DATA_TYPE.LOOP_SERIES && getLoopSeriesInfo(`${null !== (_b = null === (_a = prev.cellData) || void 0 === _a ? void 0 : _a.v) && void 0 !== _b ? _b : ""}` || "").name === getLoopSeriesInfo(`${null !== (_c = null == cur ? void 0 : cur.v) && void 0 !== _c ? _c : ""}` || "").name;
    },
    applyFunctions: {
        [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {
            var _a, _b;
            const {data: data} = dataWithIndex, isReverse = direction === Direction.LEFT || direction === Direction.UP, {series: series} = getLoopSeriesInfo(`${null === (_a = data[0]) || void 0 === _a ? void 0 : _a.v}` || "");
            if (1 === data.length) {
                let step;
                return step = isReverse ? -1 : 1, reverseIfNeed(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()), isEqualDiff(dataNumArr)) {
                const step = dataNumArr[1] - dataNumArr[0];
                return reverseIfNeed(fillLoopSeries(data, len, step, series), isReverse);
            }
            return fillCopy(data, len);
        }
    }
};

export function reverseIfNeed(data, reverse) {
    return reverse ? data.reverse() : data;
}
//# sourceMappingURL=rules.js.map