"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EndUserBrowserInfo = exports.eu_wait = void 0;
let eu_wait = function (first) {
    // @ts-ignore
    return new (function () {
        // @ts-ignore
        var self = this;
        var callback = function () {
            var args;
            if (self.deferred.length) {
                args = [].slice.call(arguments);
                // @ts-ignore
                args.unshift(callback);
                self.deferred[0].apply(self, args);
                self.deferred.shift();
            }
        };
        // @ts-ignore
        this.deferred = [];
        // @ts-ignore
        this.eu_wait = function (run) {
            this.deferred.push(run);
            return self;
        };
        first(callback);
    });
};
exports.eu_wait = eu_wait;
//================================================================================
class EndUserBrowserInfo {
    constructor() {
        var info = this._getBrowserInfo();
        this.m_name = info.name;
        this.m_version = info.version;
        this.m_arch = this._getArch();
        this.m_os_name = this._getOSName();
    }
    _getBrowserInfo() {
        var pThis = this;
        var info = {
            name: EndUserBrowserInfo.BROWSER_NAME_UNKNOWN,
            version: ''
        };
        var ua = navigator.userAgent, tem;
        var M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
        if (/msie/i.test(M[1]) || /trident/i.test(M[1])) {
            info.name = EndUserBrowserInfo.BROWSER_NAME_IE;
            if (/msie/i.test(M[1])) {
                info.version = parseInt(M[2]);
            }
            else {
                tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
                info.version = parseInt(tem[1]);
            }
            return info;
        }
        if (M[1] === 'Chrome') {
            tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
            if (tem != null) {
                info.name = tem[1].replace('OPR', EndUserBrowserInfo.BROWSER_NAME_OPERA);
                info.version = parseInt(tem[2]);
                return info;
            }
        }
        M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
        if ((tem = ua.match(/version\/(\d+)/i)) != null)
            M.splice(1, 1, tem[1]);
        info.name = M[0];
        info.version = parseInt(M[1]);
        return info;
    }
    _getOSName() {
        var np = navigator.platform || '';
        var osName;
        if (np.indexOf('Win') != -1) {
            osName = EndUserBrowserInfo.OS_NAME_WINDOWS;
        }
        else if (np.indexOf('Mac') != -1) {
            osName = EndUserBrowserInfo.OS_NAME_MAC;
        }
        else if (np.indexOf('Linux') != -1) {
            osName = EndUserBrowserInfo.OS_NAME_LINUX;
        }
        else if (np.indexOf('FreeBSD') != -1) {
            osName = EndUserBrowserInfo.OS_NAME_UNIX;
        }
        else {
            osName = EndUserBrowserInfo.OS_NAME_UNKNOWN;
        }
        return osName;
    }
    _getArch() {
        var pThis = this;
        var platform = navigator.platform || '';
        var osName = pThis._getOSName();
        var arch;
        switch (osName) {
            case EndUserBrowserInfo.OS_NAME_WINDOWS:
                arch = (platform.indexOf('Win32') != -1) ?
                    EndUserBrowserInfo.BROWSER_ARCH_NAME_X86 :
                    EndUserBrowserInfo.BROWSER_ARCH_NAME_X64;
                break;
            case EndUserBrowserInfo.OS_NAME_MAC:
                arch = (platform.indexOf('MacIntel') != -1) ?
                    EndUserBrowserInfo.BROWSER_ARCH_NAME_X86 :
                    EndUserBrowserInfo.BROWSER_ARCH_NAME_X64;
                break;
            case EndUserBrowserInfo.OS_NAME_UNIX:
            case EndUserBrowserInfo.OS_NAME_LINUX:
                arch = (platform.indexOf('x86_64') != -1) ?
                    EndUserBrowserInfo.BROWSER_ARCH_NAME_X64 :
                    EndUserBrowserInfo.BROWSER_ARCH_NAME_X86;
                break;
            default:
                arch = EndUserBrowserInfo.BROWSER_ARCH_NAME_UNKNOWN;
        }
        return arch;
    }
    GetName() {
        return this.m_name;
    }
    GetVersion() {
        return this.m_version;
    }
    GetArch() {
        return this.m_arch;
    }
    GetOSName() {
        return this.m_os_name;
    }
}
exports.EndUserBrowserInfo = EndUserBrowserInfo;
EndUserBrowserInfo.BROWSER_NAME_IE = 'IE';
EndUserBrowserInfo.BROWSER_NAME_OPERA = 'Opera';
EndUserBrowserInfo.BROWSER_NAME_CHROME = 'Chrome';
EndUserBrowserInfo.BROWSER_NAME_FIREFOX = 'Firefox';
EndUserBrowserInfo.BROWSER_NAME_SAFARI = 'Safari';
EndUserBrowserInfo.BROWSER_NAME_EDGE = 'Edge';
EndUserBrowserInfo.BROWSER_NAME_UNKNOWN = 'Unknown';
EndUserBrowserInfo.OS_NAME_WINDOWS = 'Windows';
EndUserBrowserInfo.OS_NAME_LINUX = 'Linux';
EndUserBrowserInfo.OS_NAME_UNIX = 'Unix';
EndUserBrowserInfo.OS_NAME_MAC = 'Mac OS X';
EndUserBrowserInfo.OS_NAME_UNKNOWN = 'Unknown';
EndUserBrowserInfo.BROWSER_ARCH_NAME_X86 = 'x86';
EndUserBrowserInfo.BROWSER_ARCH_NAME_X64 = 'x64';
EndUserBrowserInfo.BROWSER_ARCH_NAME_UNKNOWN = 'Unknown';
