'use strict';

import { Fields, ICell, IControlCollection } from '../../interface/basic';
import { JV } from '../jpc_value_define';
import { JpcFieldHelper } from './jpc_helper_field';
const OFFSET_FLOAT = 0.0000000001;

export let JpcCommonOutputHelper = {
    createCommonOutputWithoutDecorate: function (node: Fields, value: number | string, forceCombine: boolean) {
        let me = this,
            //1. font/style/control
            rst = {
                font: node.font,
                control: node.control as string,
                style: node.style,
                Value: value
            } as ICell;
        //2. value
        me.formatCell(node.Format, rst);
        // innerFormat(node.Format, rst);
        if (node.Prefix && rst.Value !== null) {
            rst.Value = node.Prefix + rst.Value;
        } else if (node.Prefix && forceCombine) {
            rst.Value = node.Prefix;
        }
        if (node.Suffix && rst.Value !== null) {
            rst.Value = rst.Value + node.Suffix;
        } else if (node.Suffix && forceCombine) {
            rst.Value = node.Suffix;
        }
        return rst;
    },
    createCommonOutput: function (node: Fields, value: number | string | null, controls: IControlCollection | null) {
        let me = this,
            //1. font/style/control
            rst: ICell = {
                font: node.font,
                control: node.control as string,
                style: node.style,
                Value: value
            } as ICell;
        //2. value
        JpcFieldHelper.decorateValue(rst, controls);
        me.formatCell(node.Format, rst);
        // innerFormat(node.Format, rst);
        if (node.Prefix && rst.Value !== null && rst.Value !== '') {
            rst.Value = node.Prefix + rst.Value;
        }
        if (node.Suffix && rst.Value !== null && rst.Value !== '') {
            rst.Value = rst.Value + node.Suffix;
        }
        return rst;
    },
    formatCell: function (formatStr: string, rstCell: ICell) {
        if (formatStr) {
            const rstCellFloatVal = parseFloat(rstCell.Value as string);
            if (!(isNaN(rstCellFloatVal))) {
                const dotIdx = formatStr.indexOf('.');
                if (dotIdx >= 0) {
                    let tmpStr = (rstCellFloatVal + OFFSET_FLOAT).toFixed(formatStr.length - dotIdx - 1);
                    const digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx);
                    for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) {
                        if (digStr[sIdx] === '#') {
                            if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') {
                                tmpStr = tmpStr.substr(0, tmpStr.length - 1);
                            } else {
                                break;
                            }
                        } else {
                            break;
                        }
                    }
                    if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1);
                    rstCell.Value = tmpStr;
                } else {
                    rstCell.Value = (rstCellFloatVal + OFFSET_FLOAT).toFixed(0);
                }
                const commaIdx = formatStr.indexOf(',');
                if (commaIdx >= 0) {
                    rstCell.Value = comdify(rstCell.Value.toString());
                }
            }
        }
    }
};

function comdify(numStr: string) {
    let re = /\d{1,3}(?=(\d{3})+$)/g;
    return numStr.replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) { return s1.replace(re, '$&,') + s2; });
}

