// 计算式执行（不同情况的方法会不同）
import { JE } from './jpc_rte';
import formulasDataSetter from './formulasDataSetter';
import * as stringUtil from '../public/stringUtil'
import { ReportDate as useReportDate } from '../public/ReportDate'
import { ICurrent_RPT, IRptTpl, IDataObj, Fields } from '../interface/basic'
import { JV } from './jpc_value_define';

/* eslint-disable import/prefer-default-export */
export const replaceAll = (FindText: RegExp | string, RepText: string, str: string): string => {
    const regExp = new RegExp(FindText, 'g');
    return str.replace(regExp, RepText);
};

export const formulaExec = (runType: string, $CURRENT_TEMPLATE: IRptTpl, $CURRENT_DATA: IDataObj, $CURRENT_RPT: ICurrent_RPT) => {
    formulasDataSetter.setupData($CURRENT_RPT, $CURRENT_DATA);
    const {
        P,
        F,
        D,
        L,
        getCurrentPage,
        getTotalPage,
        setFieldValue,
        setFieldValueArray,
        removeFieldValue,
        insertFieldValue,
        setData,
        getNumberArray,
        copyPrecision,
        copyFixedPrecisionNum,
        copyProperty,
        setDate,
        DLength
    } = formulasDataSetter;
    const {
        isEmptyString,
        trim,
        leftTrim,
        rightTrim,
        comdify,
        convertToCaptionNum,
        convertStrToBoolean,
        formatNumber,
    } = stringUtil;

    const $JV = JV;
    const $JE = JE;
    const reportDate = useReportDate;
    for (let execFmlIdx = 0; execFmlIdx < $CURRENT_RPT.formulas.length; execFmlIdx++) {
        if ($CURRENT_RPT.formulas[execFmlIdx].run_type === runType) {
            let expression = $CURRENT_RPT.formulas[execFmlIdx].expression;
            if (expression) {
                // 计算式集合，目前是放置打印信息的，用于配置属性的计算
                let $ME = $CURRENT_RPT.formulas[execFmlIdx];
                try {
                    let newExpression = expression;
                    const { format } = $ME;
                    // console.log(expression);
                    //临时处理
                    if (expression.indexOf(`new Date(`)) {
                        newExpression = replaceAll(/new Date\(/g, `new reportDate(`, expression)
                    }
                    eval(newExpression);
                } catch (ex) {
                    console.log(ex);
                }
            }
        }
    }
    formulasDataSetter.resetData();
    return {
        $JE,
        $JV,
        P,
        F,
        D,
        L,
        getCurrentPage,
        getTotalPage,
        setFieldValue,
        setFieldValueArray,
        removeFieldValue,
        insertFieldValue,
        setData,
        getNumberArray,
        copyPrecision,
        copyFixedPrecisionNum,
        copyProperty,
        setDate,
        DLength,
        isEmptyString,
        trim,
        leftTrim,
        rightTrim,
        comdify,
        convertToCaptionNum,
        convertStrToBoolean,
        formatNumber, 
        reportDate
    };
}

export const combinedGridFormula = ($RUN_TYPE: String, $TEXT: Fields, $TIMES: number, $CURRENT_RPT: ICurrent_RPT) => {
    const {
        $JE,
        $JV,
        P,
        F,
        D,
        L,
        getCurrentPage,
        getTotalPage,
        setFieldValue,
        setFieldValueArray,
        removeFieldValue,
        insertFieldValue,
        setData,
        getNumberArray,
        copyPrecision,
        copyFixedPrecisionNum,
        copyProperty,
        setDate,
        DLength,
        isEmptyString,
        trim,
        leftTrim,
        rightTrim,
        comdify,
        convertToCaptionNum,
        convertStrToBoolean,
        formatNumber,
        reportDate 
    }=$CURRENT_RPT.formulasObject;
    
    
    if ($CURRENT_RPT.formulas) {
        for (let execFmlIdx = 0; execFmlIdx < $CURRENT_RPT.formulas.length; execFmlIdx++) {
            if ($CURRENT_RPT.formulas[execFmlIdx].run_type === $RUN_TYPE) {
                let expression = $CURRENT_RPT.formulas[execFmlIdx].expression;
                if (expression) {
                    let $ME = $CURRENT_RPT.formulas[execFmlIdx];
                    try {
                        eval(expression);
                    } catch (ex) {
                        console.log(ex);
                    }
                }
            }
        }
    }
    formulasDataSetter.resetData();

    return {
        $JE,
        $JV,
        P,
        F,
        D,
        L,
        getCurrentPage,
        getTotalPage,
        setFieldValue,
        setFieldValueArray,
        removeFieldValue,
        insertFieldValue,
        setData,
        getNumberArray,
        copyPrecision,
        copyFixedPrecisionNum,
        copyProperty,
        setDate,
        DLength,
        isEmptyString,
        trim,
        leftTrim,
        rightTrim,
        comdify,
        convertToCaptionNum,
        convertStrToBoolean,
        formatNumber, 
        reportDate
    };

}
export default {};