"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var Ajax_1 = require("./Ajax");
var Validate_1 = require("./Validate");
var copyValue = function (val, def) {
    if (_.isArray(val)) {
        return _.concat([], val || def);
    }
    else if (_.isNumber(val)) {
        return val || def;
    }
    return _.assign({}, def, val);
};
var IPS = [];
function IpToInt(ip) {
    var ipNum = 0;
    if (!Validate_1.default.isIp(ip)) {
        return ipNum;
    }
    return ip.split('.').reduce(function (ipInt, octet) { return (ipInt << 8) + parseInt(octet, 10); }, 0) >>> 0;
}
var default_1 = /** @class */ (function () {
    function default_1() {
    }
    /**
     * 获取当前页面皮肤样式
    */
    default_1.theme = function () {
        var clazzes = document.body.className.split(' ');
        if (_.indexOf(clazzes, 'vap-blue') >= 0) {
            return 'blue';
        }
        else if (_.indexOf(clazzes, 'vap-screen') >= 0) {
            return 'screen';
        }
        else if (_.indexOf(clazzes, 'vap-admin') >= 0) {
            return 'admin';
        }
        return window['__theme'] || 'default';
    };
    /**
     * 获取URL链接的参数 ?a=1&b=2
     * Page.getParam('a') // '1'
     * Page.getParam('b') // '2'
     * Page.getParam('c') // ''
    */
    default_1.getParam = function (param) {
        var reg = window.location.search.match(new RegExp("[\?\&]" + param + "=([^\&]*)(\&?)", "i"));
        if (reg != null) {
            return decodeURI(reg[1]);
        }
        return '';
    };
    //权限
    // private static Roles = new Set<string>();
    /**
     * 加载权限
    */
    default_1.loadRole = function (roleApi) {
        if (roleApi === void 0) { roleApi = '/api-common/role/rules'; }
        window['_VAP_ROLE'] = new Set();
        Ajax_1.default.SESION(roleApi);
        Ajax_1.default.GET(roleApi, function (json) {
            json.data.map(function (item) {
                window['_VAP_ROLE'].add(item.path);
            });
        });
    };
    /**
     * 判断是否有权限
    */
    default_1.hasRole = function (role) {
        return window['_VAP_ROLE'].has(role);
    };
    default_1.layoutV = function (layout, compoment, state) {
        var LAYOUT = {};
        LAYOUT.l = copyValue(layout.l, {});
        LAYOUT.h = copyValue(layout.h, layout.l);
        var height = window.innerHeight;
        var size = 'l';
        if (height >= 768) {
            size = 'h';
        }
        window.addEventListener('resize', function () {
            var _a;
            height = window.innerHeight;
            size = 'l';
            if (height >= 768) {
                size = 'h';
            }
            if (compoment) {
                compoment.setState((_a = {},
                    _a[state] = LAYOUT[size],
                    _a));
            }
        });
        return LAYOUT[size];
    };
    /**
     * 动态变化的高度与宽度
     *
     * s -1440 (1280)
     * m 1440-1600
     * l 1600-1920
     * xl 1920+
    */
    default_1.layout = function (layout, compoment, state) {
        var LAYOUT = {};
        LAYOUT.s = copyValue(layout.s, {});
        LAYOUT.m = copyValue(layout.m, layout.s);
        LAYOUT.l = copyValue(layout.l, layout.m);
        LAYOUT.xl = copyValue(layout.xl, layout.l);
        var width = window.innerWidth;
        var size = 's';
        if (width >= 1920) {
            size = 'xl';
        }
        else if (width >= 1600) {
            size = 'l';
        }
        else if (width >= 1440) {
            size = 'm';
        }
        window.addEventListener('resize', function () {
            var _a;
            width = window.innerWidth;
            size = 's';
            if (width >= 1920) {
                size = 'xl';
            }
            else if (width >= 1600) {
                size = 'l';
            }
            else if (width >= 1440) {
                size = 'm';
            }
            if (compoment) {
                compoment.setState((_a = {},
                    _a[state] = LAYOUT[size],
                    _a));
            }
        });
        return LAYOUT[size];
    };
    default_1.ipToInt = function (ip) {
        var ipNum = 0;
        if (!Validate_1.default.isIp(ip)) {
            return ipNum;
        }
        return ip.split('.').reduce(function (ipInt, octet) { return (ipInt << 8) + parseInt(octet, 10); }, 0) >>> 0;
    };
    ;
    default_1.ipToArea = function (ip) {
        var areaName = '未知';
        if (IPS.length <= 0) {
            var resp = Ajax_1.default.GETASYNC('/api-common/organization/ip/byArea');
            IPS = _.isArray(resp.data) ? resp.data : [];
        }
        var ipNum = IpToInt(ip);
        var ipMatch = _.filter(IPS, function (ip) { return ip.startIpNum <= ipNum && ip.endIpNum >= ipNum; });
        if (ipMatch.length == 1) {
            areaName = ipMatch[0].areaName;
        }
        return areaName;
    };
    /**
     * 当前系统的版本，默认是空的，当设置了版本时，打包前导出
     * 比如： gab、hubei、bejing 等
     */
    default_1.version = document.body.getAttribute('version');
    return default_1;
}());
exports.default = default_1;
