"use strict";

var __importDefault = this && this.__importDefault || function(mod) {
    return mod && mod.__esModule ? mod : {
        default: mod
    };
};

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.removeCellCustom = exports.fillLoopSeries = exports.getLoopSeriesInfo = exports.isLoopSeries = exports.fillChnNumber = exports.fillChnWeek = exports.fillOnlyFormat = exports.fillExtendNumber = exports.forecast = exports.fillSeries = exports.getXArr = exports.isEqualRatio = exports.fillCopy = exports.getDataIndex = exports.isEqualDiff = exports.getLenS = exports.isChnWeek3 = exports.isChnWeek2 = exports.isChnWeek1 = exports.matchExtendNumber = exports.isChnNumber = exports.numberToChinese = exports.sectionToChinese = exports.chineseToNumber = exports.chnNameValue = exports.chnUnitChar = exports.chnUnitSection = exports.chnNumChar2 = exports.chnNumChar = void 0;

const types_1 = require("../types"), cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));

function chineseToNumber(chnStr) {
    if (!chnStr) return 0;
    let rtn = 0, section = 0, number = 0, secUnit = !1;
    const str = chnStr.split("");
    for (let i = 0; i < str.length; i++) {
        const num = exports.chnNumChar[str[i]];
        if (void 0 !== num) number = num, i === str.length - 1 && (section += number); else {
            const obj = exports.chnNameValue[str[i]], unit = obj.value;
            secUnit = obj.secUnit, secUnit ? (section = (section + number) * unit, rtn += section, 
            section = 0) : section += number * unit, number = 0;
        }
    }
    return rtn + section;
}

function sectionToChinese(section) {
    let strIns = "", chnStr = "", unitPos = 0, zero = !0;
    for (;section > 0; ) {
        const v = section % 10;
        0 === v ? zero || (zero = !0, chnStr = exports.chnNumChar2[v] + chnStr) : (zero = !1, 
        strIns = exports.chnNumChar2[v], strIns += exports.chnUnitChar[unitPos], chnStr = strIns + chnStr), 
        unitPos++, section = Math.floor(section / 10);
    }
    return chnStr;
}

function numberToChinese(num) {
    let unitPos = 0, strIns = "", chnStr = "", needZero = !1;
    if (0 === num) return exports.chnNumChar2[0];
    for (;num > 0; ) {
        const section = num % 1e4;
        needZero && (chnStr = exports.chnNumChar2[0] + chnStr), strIns = sectionToChinese(section), 
        strIns += 0 !== section ? exports.chnUnitSection[unitPos] : exports.chnUnitSection[0], 
        chnStr = strIns + chnStr, needZero = section < 1e3 && section > 0, num = Math.floor(num / 1e4), 
        unitPos++;
    }
    return chnStr;
}

function isChnNumber(txt) {
    if (!txt) return !1;
    let isChnNumber = !0;
    if (txt) if (1 === txt.length) isChnNumber = "日" === txt || txt in exports.chnNumChar; else {
        const str = txt.split("");
        for (let i = 0; i < str.length; i++) if (!(str[i] in exports.chnNumChar) && !(str[i] in exports.chnNameValue)) {
            isChnNumber = !1;
            break;
        }
    }
    return isChnNumber;
}

function matchExtendNumber(txt) {
    if (!txt) return {
        isExtendNumber: !1
    };
    const reg = /0|([1-9]+[0-9]*)/g, isExtendNumber = reg.test(txt);
    if (isExtendNumber) {
        const match = txt.match(reg);
        if (match && match.length > 0) {
            const matchTxt = match[match.length - 1], matchIndex = txt.lastIndexOf(matchTxt), beforeTxt = txt.substr(0, matchIndex), afterTxt = txt.substr(matchIndex + matchTxt.length);
            return {
                isExtendNumber: !0,
                matchTxt: Number(matchTxt),
                beforeTxt: beforeTxt,
                afterTxt: afterTxt
            };
        }
        return {
            isExtendNumber: !1
        };
    }
    return {
        isExtendNumber: isExtendNumber
    };
}

function isChnWeek1(txt) {
    let isChnWeek1;
    return isChnWeek1 = 1 === txt.length && ("日" === txt || chineseToNumber(txt) < 7), 
    isChnWeek1;
}

function isChnWeek2(txt) {
    let isChnWeek2;
    return isChnWeek2 = 2 === txt.length && ("周一" === txt || "周二" === txt || "周三" === txt || "周四" === txt || "周五" === txt || "周六" === txt || "周日" === txt), 
    isChnWeek2;
}

function isChnWeek3(txt) {
    let isChnWeek3;
    return isChnWeek3 = 3 === txt.length && ("星期一" === txt || "星期二" === txt || "星期三" === txt || "星期四" === txt || "星期五" === txt || "星期六" === txt || "星期日" === txt), 
    isChnWeek3;
}

function getLenS(indexArr, rsd) {
    let s = 0;
    for (let j = 0; j < indexArr.length && indexArr[j] < rsd; j++) s++;
    return s;
}

function isEqualDiff(arr) {
    let diff = !0;
    const step = arr[1] - arr[0];
    for (let i = 1; i < arr.length; i++) if (arr[i] - arr[i - 1] !== step) {
        diff = !1;
        break;
    }
    return diff;
}

function getDataIndex(csLen, asLen, indexArr) {
    const obj = [], num = Math.floor(asLen / csLen), rsd = asLen % csLen;
    let sum = 0;
    if (num > 0) {
        for (let i = 0; i < num; i++) for (let j = 0; j < indexArr.length; j++) obj[indexArr[j] + i * csLen] = sum, 
        sum++;
        for (let a = 0; a < indexArr.length && indexArr[a] < rsd; a++) obj[indexArr[a] + csLen * num] = sum, 
        sum++;
    } else for (let a = 0; a < indexArr.length && indexArr[a] < rsd; a++) obj[indexArr[a]] = sum, 
    sum++;
    return obj;
}

function fillCopy(data, len) {
    const applyData = [];
    for (let i = 1; i <= len; i++) {
        const index = (i - 1) % data.length, d = (0, cloneDeep_1.default)(data[index]);
        removeCellCustom(d), applyData.push(Object.assign({
            v: null,
            t: null
        }, d));
    }
    return applyData;
}

function isEqualRatio(arr) {
    let ratio = !0;
    const step = arr[1] / arr[0];
    for (let i = 1; i < arr.length; i++) if (arr[i] / arr[i - 1] !== step) {
        ratio = !1;
        break;
    }
    return ratio;
}

function getXArr(len) {
    const xArr = [];
    for (let i = 1; i <= len; i++) xArr.push(i);
    return xArr;
}

function fillSeries(data, len, direction, converter) {
    var _a, _b, _c, _d;
    converter && data.forEach((item => {
        const {v: v, meta: meta} = converter.toNumber(item.v);
        item.v = v, item.f = meta;
    }));
    const applyData = [], dataNumArr = [];
    for (let j = 0; j < data.length; j++) dataNumArr.push(Number(null === (_a = data[j]) || void 0 === _a ? void 0 : _a.v));
    if (data.length > 2 && isEqualRatio(dataNumArr)) for (let i = 1; i <= len; i++) {
        const index = (i - 1) % data.length, d = (0, cloneDeep_1.default)(data[index]);
        removeCellCustom(d);
        const lastValue = Number(null === (_b = data[data.length - 1]) || void 0 === _b ? void 0 : _b.v), firstValue = Number(null === (_c = data[0]) || void 0 === _c ? void 0 : _c.v), num = lastValue * (Number(null === (_d = data[1]) || void 0 === _d ? void 0 : _d.v) / firstValue) ** i;
        d && (d.v = converter ? converter.fromNumber(num, d.f) : num, applyData.push(d));
    } else {
        const xArr = getXArr(data.length);
        for (let i = 1; i <= len; i++) {
            const index = (i - 1) % data.length, d = (0, cloneDeep_1.default)(data[index]);
            removeCellCustom(d);
            const forward = direction === types_1.Direction.DOWN || direction === types_1.Direction.RIGHT, y = forecast(data.length + i, dataNumArr, xArr, forward);
            d && (d.v = converter ? converter.fromNumber(y, d.f) : y, applyData.push(d));
        }
    }
    return applyData;
}

function forecast(x, yArr, xArr, forward = !0) {
    function getAverage(arr) {
        let sum = 0;
        for (let i = 0; i < arr.length; i++) sum += arr[i];
        return sum / arr.length;
    }
    const ax = getAverage(xArr), ay = getAverage(yArr);
    let b, sum_d = 0, sum_n = 0;
    for (let j = 0; j < xArr.length; j++) sum_d += (xArr[j] - ax) * (yArr[j] - ay), 
    sum_n += (xArr[j] - ax) * (xArr[j] - ax);
    b = 0 === sum_n ? forward ? 1 : -1 : sum_d / sum_n;
    const a = ay - b * ax;
    return Math.round(1e5 * (a + b * x)) / 1e5;
}

function fillExtendNumber(data, len, step) {
    var _a, _b;
    const applyData = [], reg = /0|([1-9]+[0-9]*)/g;
    for (let i = 1; i <= len; i++) {
        const index = (i - 1) % data.length, d = (0, cloneDeep_1.default)(data[index]);
        removeCellCustom(d);
        const last = `${null !== (_b = null === (_a = data[data.length - 1]) || void 0 === _a ? void 0 : _a.v) && void 0 !== _b ? _b : ""}`;
        if (!last) continue;
        const match = null == last ? void 0 : last.match(reg), lastTxt = null == match ? void 0 : match[match.length - 1];
        if (!lastTxt) continue;
        const num = Math.abs(Number(lastTxt) + step * i), lastIndex = last.lastIndexOf(lastTxt), valueTxt = last.substr(0, lastIndex) + num.toString() + last.substr(lastIndex + lastTxt.length);
        d && (d.v = valueTxt, applyData.push(d));
    }
    return applyData;
}

function fillOnlyFormat(data, len) {
    const applyData = [];
    for (let i = 1; i <= len; i++) {
        const index = (i - 1) % data.length, d = (0, cloneDeep_1.default)(data[index]);
        removeCellCustom(d), d && (delete d.v, applyData.push(d));
    }
    return applyData;
}

function fillChnWeek(data, len, step, weekType = 0) {
    var _a, _b, _c;
    const keywordMap = [ [ "日", "一", "二", "三", "四", "五", "六" ], [ "周日", "周一", "周二", "周三", "周四", "周五", "周六" ], [ "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ] ];
    if (weekType >= keywordMap.length) return [];
    const keyword = keywordMap[weekType], applyData = [];
    for (let i = 1; i <= len; i++) {
        const index = (i - 1) % data.length, d = (0, cloneDeep_1.default)(data[index]);
        removeCellCustom(d);
        let num = 0;
        if ((null === (_a = data[data.length - 1]) || void 0 === _a ? void 0 : _a.v) === keyword[0]) num = 7 + step * i; else {
            const last = `${null !== (_c = null === (_b = data[data.length - 1]) || void 0 === _b ? void 0 : _b.v) && void 0 !== _c ? _c : ""}`;
            if (last) {
                num = chineseToNumber(last.substr(last.length - 1, 1)) + step * i;
            }
        }
        num < 0 && (num = 7 * Math.ceil(Math.abs(num) / 7) + num);
        const rsd = num % 7;
        d && (d.v = keyword[rsd], applyData.push(d));
    }
    return applyData;
}

function fillChnNumber(data, len, step) {
    var _a, _b;
    const applyData = [];
    for (let i = 1; i <= len; i++) {
        const index = (i - 1) % data.length, d = (0, cloneDeep_1.default)(data[index]);
        removeCellCustom(d);
        const num = chineseToNumber(`${null !== (_b = null === (_a = data[data.length - 1]) || void 0 === _a ? void 0 : _a.v) && void 0 !== _b ? _b : ""}`) + step * i;
        let txt;
        txt = num <= 0 ? "零" : numberToChinese(num), d && (d.v = txt, applyData.push(d));
    }
    return applyData;
}

exports.chnNumChar = {
    "零": 0,
    "一": 1,
    "二": 2,
    "三": 3,
    "四": 4,
    "五": 5,
    "六": 6,
    "七": 7,
    "八": 8,
    "九": 9
}, exports.chnNumChar2 = [ "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" ], exports.chnUnitSection = [ "", "万", "亿", "万亿", "亿亿" ], 
exports.chnUnitChar = [ "", "十", "百", "千" ], exports.chnNameValue = {
    "十": {
        value: 10,
        secUnit: !1
    },
    "百": {
        value: 100,
        secUnit: !1
    },
    "千": {
        value: 1e3,
        secUnit: !1
    },
    "万": {
        value: 1e4,
        secUnit: !0
    },
    "亿": {
        value: 1e8,
        secUnit: !0
    }
}, exports.chineseToNumber = chineseToNumber, exports.sectionToChinese = sectionToChinese, 
exports.numberToChinese = numberToChinese, exports.isChnNumber = isChnNumber, exports.matchExtendNumber = matchExtendNumber, 
exports.isChnWeek1 = isChnWeek1, exports.isChnWeek2 = isChnWeek2, exports.isChnWeek3 = isChnWeek3, 
exports.getLenS = getLenS, exports.isEqualDiff = isEqualDiff, exports.getDataIndex = getDataIndex, 
exports.fillCopy = fillCopy, exports.isEqualRatio = isEqualRatio, exports.getXArr = getXArr, 
exports.fillSeries = fillSeries, exports.forecast = forecast, exports.fillExtendNumber = fillExtendNumber, 
exports.fillOnlyFormat = fillOnlyFormat, exports.fillChnWeek = fillChnWeek, exports.fillChnNumber = fillChnNumber;

const LOOP_SERIES = {
    enWeek1: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
    enWeek2: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
    enMonth1: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ],
    enMonth2: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],
    chnMonth1: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ],
    chnMonth2: [ "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "腊月" ],
    chHour1: [ "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" ],
    chHour2: [ "子时", "丑时", "寅时", "卯时", "辰时", "巳时", "午时", "未时", "申时", "酉时", "戌时", "亥时" ],
    chYear1: [ "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" ],
    chSeason1: [ "春", "夏", "秋", "冬" ],
    chSeason2: [ "春季", "夏季", "秋季", "冬季" ]
};

function isLoopSeries(txt) {
    let isLoopSeries = !1;
    return Object.keys(LOOP_SERIES).forEach((key => {
        LOOP_SERIES[key].includes(txt) && (isLoopSeries = !0);
    })), isLoopSeries;
}

function getLoopSeriesInfo(txt) {
    let name = "";
    const series = [];
    return Object.keys(LOOP_SERIES).forEach((key => {
        LOOP_SERIES[key].includes(txt) && (name = key, series.push(...LOOP_SERIES[key]));
    })), {
        name: name,
        series: series
    };
}

function fillLoopSeries(data, len, step, series) {
    var _a, _b;
    const seriesLen = series.length, applyData = [];
    for (let i = 1; i <= len; i++) {
        const index = (i - 1) % data.length, d = (0, cloneDeep_1.default)(data[index]);
        removeCellCustom(d);
        const last = `${null !== (_b = null === (_a = data[data.length - 1]) || void 0 === _a ? void 0 : _a.v) && void 0 !== _b ? _b : ""}`;
        let num = series.indexOf(last) + step * i;
        num < 0 && (num += Math.abs(step) * seriesLen);
        const rsd = num % seriesLen;
        d && (d.v = series[rsd], applyData.push(d));
    }
    return applyData;
}

function removeCellCustom(cell) {
    cell && "custom" in cell && delete cell.custom;
}

exports.isLoopSeries = isLoopSeries, exports.getLoopSeriesInfo = getLoopSeriesInfo, 
exports.fillLoopSeries = fillLoopSeries, exports.removeCellCustom = removeCellCustom;
//# sourceMappingURL=fill.js.map
