"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var moment = require("moment");
var _ = require("lodash");
var d3 = require("d3");
var Cache_1 = require("./Cache");
var jquery_1 = require("jquery");
var NumberPoints = new Map();
var DICT_URL = '/api-common/dictionary';
var __GLOBAL_LOADED = false;
var DICTMAP = new Map();
//初始化字典
function INITDICT() {
    var dict = Cache_1.default.sessionData(DICT_URL);
    if (dict === null) {
        jquery_1.ajax(DICT_URL, { async: false, method: 'GET' }).done(function (resp) {
            if (resp.code == '0') {
                dict = resp;
                Cache_1.default.sessionData(DICT_URL, dict);
            }
        });
    }
    var roots = _.filter(dict.data, function (item) { return item.parentType == '0'; });
    // window['roots'] = dict.data;
    roots.map(function (item) {
        var childrens = _.filter(dict.data, function (child) { return child.parentType == item.type; });
        var map = new Map();
        var reverse = new Map();
        var nodes = [];
        childrens.map(function (node) {
            map.set(node.code + '', node.codeValue + '');
            reverse.set(node.codeValue + '', node.code + '');
            nodes.push({ name: node.codeValue + '', value: node.code + '', description: node.description + '' });
        });
        DICTMAP.set(item.type, {
            map: map, reverse: reverse, nodes: nodes
        });
    });
}
var formatDecimal = function (number, limit) {
    if (limit === void 0) { limit = 2; }
    if (!NumberPoints.has(limit)) {
        NumberPoints.set(limit, d3.format("." + limit + "~f"));
    }
    return parseFloat(NumberPoints.get(limit)(number));
};
var default_1 = /** @class */ (function () {
    function default_1() {
    }
    /**
     * 获取字典关系
     * */
    default_1.getDict = function (type) {
        if (!__GLOBAL_LOADED) {
            INITDICT();
        }
        return DICTMAP.get(type + '');
    };
    // /**
    //  * 根据字典类型和code进行转换
    //  * */
    // static async formatDict(type, code) {
    //     if (!__GLOBAL_LOADED) {
    //         await INITDICT();
    //     }
    //     let map = DICTMAP.get(type + '');
    //     if (map) {
    //         return map.map.get(code) || '';
    //     }
    //     return '';
    // }
    /**
     * 字符串截取，把字符串按最大长度进行截取
     * @param txt 需要截取的字符串
     * @param len 最大长度，默认为20
     * @param hasDot 超长后是否以 `...` 进行结尾，默认为 `true`
     *
    */
    default_1.formatString = function (txt, len, hasDot) {
        if (len === void 0) { len = 20; }
        if (hasDot === void 0) { hasDot = true; }
        if (_.isEmpty(txt)) {
            return '';
        }
        var newLength = 0;
        var newStr = [];
        var chineseRegex = /[^\x00-\xff]/g;
        var singleChar = "";
        var strLength = txt.replace(chineseRegex, "**").length;
        for (var i = 0; i < strLength; i++) {
            singleChar = txt.charAt(i).toString();
            if (singleChar.match(chineseRegex) !== null) {
                newLength += 2;
            }
            else {
                newLength++;
            }
            if (newLength > len) {
                break;
            }
            newStr.push(singleChar);
        }
        if (hasDot && strLength > len) {
            newStr.push("...");
        }
        return newStr.join('');
    };
    /**
    * @param  number 一个数字
    * @return  格式化后的字符串
    * @description 将数字格式化为带中文单位,保留两位小数，支持`万`,`亿`
    ```javascript
    PageUtil.formatNumber(123); 			//123
    PageUtil.formatNumber(100000); 			//10万
    PageUtil.formatNumber(111111); 			//11.11万
    PageUtil.formatNumber(123456789); 		//1.23亿
    ```
    */
    default_1.formatNumber = function (number) {
        if (number > 100000000) {
            return formatDecimal(number / 100000000) + '亿';
        }
        else if (number > 10000) {
            return formatDecimal(number / 10000) + '万';
        }
        return number;
    };
    /**
    * @param  number 一个数字，单位为Byte(字节)
    * @return  格式化后的字符串
    * @description 格式化容量大小,保留两位小数，支持 `B`,`KB`,`MB`,`GB`,`TB`,`PB`
    ```javascript
    PageUtil.formatSize(123); 			//123MB
    PageUtil.formatSize(1024); 			//1GB
    PageUtil.formatSize(2050); 			//2.01GB
    ```
    */
    default_1.formatSize = function (byteSize) {
        if (byteSize < 1024) {
            return this.formatDecimal(byteSize) + 'b';
        }
        var kbSize = (byteSize / 1024);
        if (kbSize < 1024) {
            return this.formatDecimal(kbSize) + 'KB';
        }
        var mbSize = (kbSize / 1024);
        if (mbSize < 1024) {
            return this.formatDecimal(mbSize) + 'MB';
        }
        var gbSize = (mbSize / 1024);
        if (gbSize < 1024) {
            return this.formatDecimal(gbSize) + 'GB';
        }
        var tbSize = (gbSize / 1024);
        if (tbSize < 1024) {
            return this.formatDecimal(tbSize) + 'TB';
        }
        var pbSize = (tbSize / 1024);
        return this.formatDecimal(pbSize) + 'PB';
    };
    default_1.formatDate = function (date, format) {
        if (format === void 0) { format = 'YYYY-MM-DD HH:mm:ss'; }
        if (_.isString(date)) {
            return moment(new Date(date)).format(format);
        }
        return moment(date).format(format);
    };
    /**
     * 格式化字符串，默认格式化为
     * aa***bb
     * @param value 被格式化字符串
     * @param beginNum 开始的字符数
     * @param endNum 结束的字符数
     */
    default_1.formatStarStr = function (value, beginNum, endNum) {
        var leftNum = beginNum ? beginNum : 2;
        var rightNum = endNum ? endNum : 2;
        return value.substring(0, leftNum) + '***' + value.substring(value.length - rightNum, value.length);
    };
    /**
        * @param number 一个数字
        * @return 格式化后的字符串
        * @description 格式化为美国数字显示方式（三位分开）
        ```javascript
        PageUtil.formatUSD(123); 			//123
        PageUtil.formatUSD(1234); 			//1,234
        PageUtil.formatUSD(12345); 			//12,345
        PageUtil.formatUSD(123456); 		//123,456
        PageUtil.formatUSD(1234567); 		//1,234,567
        PageUtil.formatUSD(1234567890); 	//1,234,567,890
    ```
        */
    default_1.formatUSD = d3.format(',');
    /**
    * @param number 一个数字
    * @return 格式化后的字符串
    * @description 格式化后保留两位小数
    ```javascript
    PageUtil.formatDecimal(1.234); 			//1.23
    PageUtil.formatDecimal(1.236); 			//1,24
    PageUtil.formatDecimal(1.2); 			//1.2
    PageUtil.formatDecimal(1); 				//1
    ```
    */
    default_1.formatDecimal = formatDecimal;
    return default_1;
}());
exports.default = default_1;
