"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var _ = require("lodash");
var moment = require("moment");
var Format_1 = require("./Format");
var antd_1 = require("antd");
var PageUtil_1 = require("./PageUtil");
var display_1 = require("../display");
function dictRender(type, def, render) {
    if (def === void 0) { def = '未知'; }
    if (render === void 0) { render = function (t, o) { return t; }; }
    var tmp = Format_1.default.getDict(type);
    var _a = __read([new Map(), new Map(), []], 3), map = _a[0], reverse = _a[1], nodes = _a[2];
    if (tmp) {
        map = tmp.map;
        reverse = tmp.reverse;
        nodes = tmp.nodes;
    }
    else {
        console.error('没能找到字典类型：' + type);
    }
    var fn = function (code) { return render(map.get(code + '') || def, code); };
    fn.reverse = function (name) { return reverse.get(name + '') || ''; };
    fn.nodes = nodes;
    fn.get = function (value) {
        var ns = _.filter(nodes, { value: value });
        if (ns && ns.length == 1) {
            return ns[0];
        }
        return null;
    };
    fn.getByName = function (name) {
        var ns = _.filter(nodes, { name: name });
        if (ns && ns.length == 1) {
            return ns[0];
        }
        return null;
    };
    fn.optAs = function (nameField, valueField, shift) {
        if (nameField === void 0) { nameField = 'name'; }
        if (valueField === void 0) { valueField = 'value'; }
        if (shift === void 0) { shift = null; }
        var opts = nodes.map(function (item) {
            var _a;
            return _a = {}, _a[nameField] = item.name, _a[valueField] = item.value, _a;
        });
        if (shift) {
            opts.unshift(shift);
        }
        return opts;
    };
    return fn;
}
var default_1 = /** @class */ (function () {
    function default_1() {
    }
    /**
     * 字符串截取 Render
     * @param len 截取长度，默认为 20
     * @param hasDot 是否有3个省略号 默认为 true
     * @param className className
    */
    default_1.strRender = function (len, hasDot, className, onlyText) {
        if (len === void 0) { len = 20; }
        if (hasDot === void 0) { hasDot = true; }
        if (className === void 0) { className = ''; }
        if (onlyText === void 0) { onlyText = true; }
        var fn = function (str) {
            str = str == 'null' ? '' : str;
            return onlyText ? Format_1.default.formatString(str, len, hasDot) : React.createElement("span", { className: className, title: str }, Format_1.default.formatString(str, len, hasDot));
        };
        return fn;
    };
    /**
     * 长字符串截取 Render， 可copy
     * @param len 截取长度，默认为 20
     * @param hasDot 是否有3个省略号 默认为 true
     * @param className className
     *
     * @returns 不能为text
    */
    default_1.longstrRender = function (len, hasDot) {
        if (len === void 0) { len = 20; }
        if (hasDot === void 0) { hasDot = true; }
        var fn = function (str) {
            return React.createElement(display_1.Tooltip, { title: str }, Format_1.default.formatString(str, len, hasDot));
        };
        return fn;
    };
    // function asyncListRender(){
    // }
    /**
     * 列表 Render 构造器
     * @param arr 默认为一个空数组
     * @param value 数据对应取值的字段 默认为 'id'
     * @param name 数据对应显示的字段 默认为 'name'
     * @param def 数据没有找到时的字段 默认为 '未知'
    */
    default_1.listRender = function (arr, value, name, def, onlyText) {
        if (arr === void 0) { arr = []; }
        if (value === void 0) { value = 'id'; }
        if (name === void 0) { name = 'name'; }
        if (def === void 0) { def = '未知'; }
        if (onlyText === void 0) { onlyText = true; }
        var VALUES = {};
        arr.map(function (item) {
            VALUES[item[value]] = item[name];
        });
        var fn = function (val) {
            var key = val + '';
            var text = def ? def : key;
            if (_.has(VALUES, key)) {
                text = VALUES[key];
            }
            return onlyText ? text : React.createElement("span", null, text);
        };
        fn.update = function (list) {
            VALUES = {};
            list.map(function (item) {
                VALUES[item[value]] = item[name];
            });
        };
        return fn;
    };
    /**
     * 状态 Render 构造器
     * @param statusMap 一个StatusMap 默认为  { success: { name: '正常', value: 0 }, error: { name: '异常', value: 1 } }
     * @param def 数据没有找到时的显示名称 默认为 '未知'
    */
    default_1.statusRender = function (statusMap, def, onlyText) {
        if (statusMap === void 0) { statusMap = { success: { name: '正常', value: 0 }, error: { name: '异常', value: 1 } }; }
        if (def === void 0) { def = '未知'; }
        if (onlyText === void 0) { onlyText = false; }
        var STYLES = {};
        var VALUES = {};
        _.keys(statusMap).map(function (style) {
            var key = statusMap[style].value + '';
            STYLES[key] = style;
            VALUES[key] = statusMap[style].name;
        });
        var fn = function (val) {
            var key = val + '';
            var style = 'default';
            var text = def === false ? val : def;
            if (_.has(VALUES, key)) {
                text = VALUES[key];
            }
            if (_.has(STYLES, key)) {
                style = STYLES[key];
            }
            return onlyText ? text : React.createElement(antd_1.Badge, { status: style, text: text });
        };
        fn.update = function (statusMap) {
            _.keys(statusMap).map(function (style) {
                var key = statusMap[style].value + '';
                STYLES[key] = style;
                VALUES[key] = statusMap[style].name;
            });
        };
        return fn;
    };
    /**
     * Monment 时间 Render 构造器,可以在近一段时间内显示 （x小时前）之类的字串
     * @param time 是否显示时分秒，默认为 true 即 `YYYY-MM-DD HH:mm:ss`, 设置为 `false` 则显示为'YYYY-MM-DD'
     * @param nearly 最近 {x} 天 时，转换为 moment 方式显示，比如 5秒前，15小时前，2天前等等。 默认为 3
     *
    */
    default_1.momentRender = function (time, nearly, onlyText) {
        if (time === void 0) { time = true; }
        if (nearly === void 0) { nearly = 3; }
        if (onlyText === void 0) { onlyText = true; }
        var now = moment();
        var from = now.unix();
        var fn = function (val) {
            // let time = format ? moment(val, format) : moment(new Date(val));
            var time = moment(new Date(val));
            var to = time.unix();
            var diff = from - to;
            var txt = '';
            if (diff >= 0 && diff < 86400 * nearly) {
                txt = time.fromNow();
            }
            else if (time) {
                txt = time.format('YYYY-MM-DD HH:mm:ss');
            }
            else {
                txt = time.format('YYYY-MM-DD');
            }
            return onlyText ? txt : React.createElement("span", { title: val }, txt);
        };
        fn.update = function () {
            now = moment();
            from = now.unix();
        };
        return fn;
    };
    /**
     *
     * 时间 Render 构造器
     * @param 时间
    */
    default_1.dateRender = function (format, onlyText) {
        if (format === void 0) { format = 'YYYY-MM-DD HH:mm:ss'; }
        if (onlyText === void 0) { onlyText = true; }
        var fn = function (val) {
            return onlyText ? moment(new Date(val)).format(format) : React.createElement("span", { title: val }, moment(new Date(val)).format(format));
        };
        return fn;
    };
    /**
     *
     * 容量 Render 构造器
     * @param byte number 大小
    */
    default_1.sizeRender = function (onlyText) {
        if (onlyText === void 0) { onlyText = true; }
        var fn = function (val) {
            return onlyText ? Format_1.default.formatSize(val) : React.createElement("span", { className: "vapfont", title: Format_1.default.formatSize(val) }, Format_1.default.formatSize(val));
        };
        return fn;
    };
    /**
     * 返回一个 htmlRender，用于显示自定义的html,会用div包住
     * @param className 包住自定义html的 className 默认为空
    */
    default_1.htmlRender = function (className) {
        if (className === void 0) { className = ''; }
        return function (val) {
            return React.createElement("div", { className: className, dangerouslySetInnerHTML: { __html: val } });
        };
    };
    /**
     * 角色权限Render
     * */
    default_1.roleRender = function (role, render) {
        return PageUtil_1.default.hasRole(role) ? render : [];
    };
    /**
     * 字典表Render
     * */
    default_1.dictRender = dictRender;
    return default_1;
}());
exports.default = default_1;
