var GH = /** @class */ (function () {
    function GH() {
        this.clientId = '';
        this.host = window.location.host;
        this.generalPoolID = 0;
        this.apiDomain = 'https://api2.gohire.io';
        this.baseDomain = 'https://app.gohire.io';
        this.isMobile = false;
        this.zIndex = 10000;
        this.jobs = document.getElementsByTagName('gohire-jobs')[0];
        this.jobsData = [];
        this.elements = {};
        this.timeout = 15;
        this.width = 700;
        this.css = "\n\n  .gohire,.gohire * { color:#333 !important; }\n  .gohire_active { overflow:hidden !important; }\n  .gohire_active_mobile { overflow:hidden !important; height:100% !important; width:100% !important; position:fixed !important; }\n  .gohire_holder { position:fixed; top:0; right:0; width:" + this.width + "px; height:100vh; background-color:#fff; transition:right 0.5s; z-index:" + (this.zIndex + 1).toString() + "; }\n  .gohire_cover { position:fixed; top:0; left:0; width:100vw; height:100vh; background-color:rgba(0,0,0,.5); transition: opacity 0.5s; z-index:" + this.zIndex.toString() + "; }\n  .gohire_iframe { position:absolute; top:0; left:0; width:100%; height:100%; border:0; transition:opacity 0.5s; }\n  .gohire_loading { position:absolute; top:0; left:0; width:100%; height:100%; }\n  .gohire_error { position:absolute; top:0; left:0; width:100%; height:100%; }\n  .gohire_holder_hidden { right:-" + this.width + "px; }\n  .gohire_cover_hidden { pointer-events:none; opacity:0; }\n  .gohire_iframe_hidden { opacity:0; }\n  .gohire_hidden { display:none; }\n  .gohire_no_select { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }\n  .gohire_spinner:before {  border-top: 2px solid #c3c8d1; content: ''; box-sizing: border-box; position: absolute; top: 47%; left: 50%; width: 40px; height: 40px; margin-top: -20px; margin-left: -20px; border-radius: 50%; border-right: 2px solid transparent; animation: gohire_spinner .6s linear infinite; }\n\n  @keyframes gohire_spinner { to {transform: rotate(360deg); } }\n\n  @media screen and (max-width: 700px) {\n    .gohire_holder { width: 100vw; }\n    .gohire_holder_hidden { right:calc(-100vw); }\n  }\n\n  .gohire-job-list-empty { width: 100%; }\n\n  .gohire-job-list { width: 100%; box-shadow: 0 1px 3px 1px rgba(160,161,188,.14); background-color:#fff; border-radius: 12px; margin: 20px 0; border: solid 1px #E0E6ED; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }\n  .gohire-job-list-item { cursor: pointer; display: flex; flex-direction: row; align-items: center; padding: 25px 0; margin:0 20px 0 20px; border-bottom: solid 1px #E0E6ED !important; transition: all 0.5s; }\n  .gohire-job-list a:last-child .gohire-job-list-item { border-bottom: none !important; }\n  .gohire-job-list-item-detail { text-align: left; }\n  .gohire-job-list-item-detail .gohire-job-list-item-detail-title { font-size: 16px !important; font-weight: bold !important; transition: all 0.5s; margin: 0 !important; color:#005cff !important; }\n  .gohire-job-list-item-detail .gohire-job-list-item-detail-sub { font-size: 13px !important; color: #3e5167 !important; margin:0 !important; }\n  .gohire-job-list-item:hover .gohire-job-list-item-detail-title { text-decoration: underline !important; }\n  .gohire-job-list-item-nav { margin-left:auto !important; }\n  .gohire-job-list-item-nav path { fill:#005cff !important; }\n\n  @media screen and (max-width: 500px) {\n    .gohire-job-list-item-nav { display:none !important; }\n  }\n\n\n\t.jobs-ga { padding: 0 20px 80px !important; }\n\t.jobs-ga-detail h2 { font-size: 1.8rem; line-height: 1.4; }\n\t.jobs-ga-detail p { max-width: 500px; margin: 5px auto 30px; font-size: 0.84rem !important; line-height: 1.7 !important; }\n\t.jobs-ga-btn { max-width: 200px; height: 50px !important; padding: 0 20px; }\n\t.jobs-ga-btn p { font-size: .94rem !important; }\n\n\t:root { --primary-03: #ffffff; }\n\t.btn p { font-weight: 700 !important; font-size: .82rem; }\n\n\t.primary-btn { background-color: var(--primary-01); }\n\t.primary-btn p { color: var(--primary-03); font-size: .8rem; }\n\t.primary-btn:hover { box-shadow: inset 0 -2px 0 rgba(31,45,61,.15); }\n\n\t@media screen and (max-width: 600px) {\n\n\t.jobs-ga-detail h2 { font-size: 1.5rem; } }\n\n  ";
        if (window.innerWidth < this.width) {
            this.isMobile = true;
        }
        this.create();
    }
    GH.prototype.create = function () {
        var _this = this;
        this.elements['holder'] = document.createElement('div');
        this.elements['holder'].className = 'gohire gohire_holder gohire_no_select gohire_holder_hidden';
        document.body.appendChild(this.elements['holder']);
        this.elements['cover'] = document.createElement('div');
        this.elements['cover'].className = 'gohire gohire_cover gohire_cover_hidden';
        this.elements['cover'].style.display = 'none';
        document.body.appendChild(this.elements['cover']);
        setTimeout(function () {
            _this.elements['cover'].style.display = 'block';
        }, 0);
        this.elements['frame'] = document.createElement('iframe');
        this.elements['frame'].className = 'gohire gohire_iframe gohire_iframe_hidden';
        this.elements['holder'].appendChild(this.elements['frame']);
        this.elements['error'] = document.createElement('div');
        this.elements['error'].className = 'gohire gohire_error gohire_hidden';
        this.elements['holder'].appendChild(this.elements['error']);
        this.elements['loading'] = document.createElement('div');
        this.elements['loading'].className = 'gohire gohire_loading';
        this.elements['holder'].appendChild(this.elements['loading']);
        this.elements['loading'].innerHTML = "<div class=\"gohire_spinner\"></div>";
        var style = document.createElement('style');
        style.id = 'gohire_styles';
        style.type = 'text/css';
        style.innerHTML = this.css;
        document.body.appendChild(style);
        /*this.elements['cover'].addEventListener('click', () => {
            gh.hide();
          });*/
        if (this.jobs) {
            var attrString = '';
            if (this.jobs.attributes) {
                for (var _i = 0, _a = this.jobs.attributes; _i < _a.length; _i++) {
                    var attr = _a[_i];
                    attrString += "&" + attr.nodeName + "=" + attr.nodeValue;
                }
            }
            //   this.get(`/widget-jobs?clientId=${this.clientId}${attrString}`, (data: any) => {
            this.get("/widget-jobs/" + this.clientId, function (data) {
                if (data) {
                    if (data.generalPoolID) {
                        _this.generalPoolID = data.generalPoolID;
                    }
                    if (data['colour']) {
                        var style_2 = document.createElement('style');
                        style_2.id = 'gohire_styles_brand';
                        style_2.type = 'text/css';
                        style_2.innerHTML = "\n\n            .gohire-job-list-item-detail .gohire-job-list-item-detail-title { color: " + data['colour'] + " !important; }\n            .gohire-job-list-item-nav path { fill: " + data['colour'] + " !important; }\n            .client-brand-background { background-color: " + data['colour'] + " !important; }\n\n            ";
                        document.body.appendChild(style_2);
                    }
                    if (!Array.isArray(data['jobs'])) {
                        var div = document.createElement('div');
                        div.className = 'gohire-job-list-empty';
                        div.id = 'gohire-job-list';
                        div.innerHTML = 'There are currently no job openings.';
                        _this.jobs.parentNode.insertBefore(div, _this.jobs);
                        _this.jobs.parentNode.removeChild(_this.jobs);
                        _this.jobs = div;
                    }
                    else {
                        if (data['jobs'].length > 0) {
                            _this.template = data['template'] || "\n\n\t\t\t\t\t\t\t<a href=\"{{link}}\" target=\"_blank\">\n\t\t\t\t\t\t\t<div class=\"gohire-job-list-item\">\n\t\t\t\t\t\t\t\t<div class=\"gohire-job-list-item-detail\">\n\t\t\t\t\t\t\t\t<p class=\"gohire-job-list-item-detail-title gohire-brand-colour\">{{title}}</p>\n\t\t\t\t\t\t\t\t<p class=\"gohire-job-list-item-detail-sub\">{{location}} &middot; Posted {{date}}</p>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t<div class=\"gohire-job-list-item-nav\"><svg width=\"20\" height=\"20\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 192 512\"><path d=\"M187.8 264.5L41 412.5c-4.7 4.7-12.3 4.7-17 0L4.2 392.7c-4.7-4.7-4.7-12.3 0-17L122.7 256 4.2 136.3c-4.7-4.7-4.7-12.3 0-17L24 99.5c4.7-4.7 12.3-4.7 17 0l146.8 148c4.7 4.7 4.7 12.3 0 17z\" /></svg></div>\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</a>\n\n\t\t\t\t\t\t\t";
                            var html = '';
                            for (var _i = 0, _a = data['jobs']; _i < _a.length; _i++) {
                                var job = _a[_i];
                                _this.jobsData.push(job);
                                html += _this.templateReplacements(job);
                            }
                            if (data['custom']) {
                                _this.jobs.innerHTML = html;
                                while (_this.jobs.firstChild) {
                                    _this.jobs.parentNode.insertBefore(_this.jobs.firstChild, _this.jobs);
                                }
                                _this.jobs.parentNode.removeChild(_this.jobs);
                            }
                            else {
                                var div = document.createElement('div');
                                div.className = 'gohire-job-list';
                                div.id = 'gohire-job-list';
                                div.innerHTML = html;
                                _this.jobs.parentNode.insertBefore(div, _this.jobs);
                                _this.jobs.parentNode.removeChild(_this.jobs);
                                _this.jobs = div;
                            }
                        }
                        else {
                            var style_1 = document.createElement('style');
                            style_1.id = 'gohire_styles_brand';
                            style_1.type = 'text/css';
                            style_1.innerHTML = '\n\n  .gohire-job-list {padding:100px 20px 100px 20px;text-align:center; }\n\n   .gohire-job-list-empty{padding:100px 20px 100px 20px;text-align:center;}         ';
                            document.body.appendChild(style_1);
                            var div = document.createElement('div');
                            div.className = 'gohire-job-list-empty';
                            div.id = 'gohire-job-list';
                            div.innerHTML = '';
                            if (_this.generalPoolID == 0) {
                                div.innerHTML = 'There are currently no job openings';
                            }
                            _this.jobs.parentNode.insertBefore(div, _this.jobs);
                            _this.jobs.parentNode.removeChild(_this.jobs);
                            _this.jobs = div;
                        }
                        if (_this.generalPoolID !== 0) {
                            var style_general_app = document.createElement('style');
                            style_general_app.id = 'style_general_app';
                            style_general_app.type = 'text/css';
                            if (data['jobs'].length > 0) {
                                style_general_app.innerHTML = '\n\n .jobs-ga-detail p {max-width: 500px;margin: 5px auto 30px;font-size: 0.84rem !important;line-height: 1.7 !important;} \n\n .jobs-ga-detail h2 {font-size: 1.8rem; line-height: 1.4;margin-bottom:0px;} \n\n  .jobs-ga {margin-top:60px; padding: 0 0 80px 0;text-align: center;display: flex;flex-direction: column;align-items: center;justify-content: center;} \n\n .jobs-ga-btn {max-width: 200px;height: 50px !important;padding: 0 20px;} \n\n .client-brand-background{background-color: #f63de9;} \n\n .btn {height: 40px;padding: 0 15px;display: flex;align-items: center;justify-content: center;white-space: nowrap;border-radius: 4px; cursor: pointer;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none; user-select: none;transition: all 0.5s;} \n\n  .jobs-ga-detail p { max-width: 500px;margin: 5px auto 30px;font-size: 0.84rem !important;line-height: 1.7 !important;}           ';
                            }
                            else {
                                style_general_app.innerHTML = '\n\n  .jobs-ga-detail p {max-width: 500px;margin: 5px auto 30px;font-size: 0.84rem !important;line-height: 1.7 !important;} \n\n .jobs-ga-detail h2 {font-size: 1.8rem; line-height: 1.4;margin-bottom:0px;} \n\n .gohire-job-list-empty{padding: 100px 20px 100px 20px;text-align: center;}  \n\n .jobs-ga {padding: 100px 20px 100px 20px;text-align: center;display: flex;flex-direction: column;align-items: center;justify-content: center;} \n\n .jobs-ga-btn {max-width: 200px;height: 50px !important;padding: 0 20px;} \n\n .client-brand-background{background-color: #f63de9 ;} \n\n .btn {height: 40px;padding: 0 15px;display: flex;align-items: center;justify-content: center;white-space: nowrap;border-radius: 4px; cursor: pointer;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none; user-select: none;transition: all 0.5s;} \n\n  .jobs-ga-detail p { max-width: 500px;margin: 5px auto 30px;font-size: 0.84rem !important;line-height: 1.7 !important;}            ';
                            }
                            document.body.appendChild(style_general_app);
                            var generalApHtml = '';
                            generalApHtml += '<div class="jobs-ga">';
                            generalApHtml += '<div class="jobs-ga-detail">';
                            generalApHtml += '<h2>General Application </h2>';
                            generalApHtml += "<p>Can't find a career you're looking for? We want to hear from you, please send your application by hitting the button below.</p></div>";
                            // this.get(`/getGeneralPoolId/?clientId=${this.clientId}`, (dataRes: any) => {
                            generalApHtml += "<div class=\"jobs-ga-btn btn primary-btn client-brand-background\" onclick=\"applyPool(" + data.generalPoolID + "); return false;\">";
                            generalApHtml += '<p>Send Application</p></div></div>';
                            // });
                            document.getElementById('gohire-job-list').innerHTML += generalApHtml;
                        }
                    }
                }
            });
        }
    };
    // This function is called for General Application Pool open
    GH.prototype.generalPoolOpen = function (source) {
        var _this = this;
        if (source === void 0) { source = ''; }
        var poolId = 0;
        this.get("/widget-jobs/" + this.clientId + "/getPoolId/", function (dataRes) {
            poolId = dataRes['poolId'];
            _this.jobId = poolId;
            _this.source = source;
            setTimeout(function () {
                var url = _this.baseDomain + "/widget/" + _this.clientId + "/" + _this.jobId.toString() + "?host=" + _this.host + (document.referrer ? "&referrer=" + encodeURIComponent(document.referrer) : '') + "  " + (_this.source ? "&source=" + _this.source.toString() : '');
                _this.elements['frame'].setAttribute('src', url);
            }, 500);
            _this.show();
            if (_this.timer) {
                clearTimeout(_this.timer);
            }
            _this.timer = setTimeout(function () {
                _this.showError('There seems to be a problem loading the apply widget, please <a href="#" onclick="window.location.reload(); return false;">reload the page</a> and try again');
            }, _this.timeout * 1000);
        });
    };
    GH.prototype.open = function (jobId, source) {
        var _this = this;
        if (!jobId) {
            if (source === 'generalApplication') {
                this.generalPoolOpen(source);
            }
            else {
                alert('# Invalid job ID');
                return;
            }
        }
        this.jobId = jobId;
        this.source = source;
        setTimeout(function () {
            var url = _this.baseDomain + "/widget/" + _this.clientId + "/" + _this.jobId.toString() + "?host=" + _this.host + (document.referrer ? "&referrer=" + encodeURIComponent(document.referrer) : '') + "  " + (_this.source ? "&source=" + _this.source.toString() : '');
            _this.elements['frame'].setAttribute('src', url);
        }, 500);
        this.show();
        if (this.timer) {
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(function () {
            _this.showError('There seems to be a problem loading the apply widget, please <a href="#" onclick="window.location.reload(); return false;">reload the page</a> and try again');
        }, this.timeout * 1000);
    };
    GH.prototype.show = function () {
        var _this = this;
        document.body.classList.add('gohire_active');
        if (this.isMobile) {
            document.body.classList.add('gohire_active_mobile');
        }
        this.elements['holder'].classList.remove('gohire_holder_hidden');
        this.elements['cover'].classList.remove('gohire_cover_hidden');
        this.elements['loading'].classList.remove('gohire_hidden');
        this.elements['error'].classList.add('gohire_hidden');
        this.elements['frame'].classList.add('gohire_iframe_hidden');
        if (this.isMobile) {
            this.sizeInterval = setInterval(function () {
                _this.resize();
            }, 100);
        }
    };
    GH.prototype.hide = function () {
        var _this = this;
        document.body.classList.remove('gohire_active');
        document.body.classList.remove('gohire_active_mobile');
        this.elements['holder'].classList.add('gohire_holder_hidden');
        this.elements['cover'].classList.add('gohire_cover_hidden');
        setTimeout(function () {
            _this.elements['frame'].setAttribute('src', '');
        }, 500);
        if (this.sizeInterval) {
            clearInterval(this.sizeInterval);
        }
    };
    GH.prototype.loaded = function () {
        if (this.timer) {
            clearTimeout(this.timer);
        }
        this.elements['frame'].classList.remove('gohire_iframe_hidden');
        this.elements['loading'].classList.add('gohire_hidden');
    };
    GH.prototype.showError = function (error) {
        this.elements['loading'].classList.add('gohire_hidden');
        this.elements['error'].classList.remove('gohire_hidden');
        this.elements['error'].innerHTML = error;
    };
    GH.prototype.get = function (url, success, error) {
        var request = new XMLHttpRequest();
        request.open('GET', this.apiDomain + url);
        request.setRequestHeader('Widget-Host', this.host);
        request.onload = function () {
            var json = JSON.parse(request.responseText);
            if (request.status >= 200 && request.status < 300) {
                if (typeof success == 'function') {
                    success(json);
                }
            }
            else if (typeof error == 'function') {
                success(error);
            }
        };
        request.send();
    };
    GH.prototype.resize = function () {
        this.elements['holder'].style.height = window.innerHeight + "px";
    };
    GH.prototype.templateReplacements = function (job) {
        if (this.template) {
            var template = this.template;
            if (typeof job == 'object' && job) {
                var keys = Object.keys(job);
                for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
                    var key = keys_1[_i];
                    var data = this.htmlEncode(job[key]).replace(new RegExp('{{'), '').replace(new RegExp('}}'), '');
                    template = template.replace(new RegExp("{{" + key + "}}", 'g'), data);
                }
            }
            return template;
        }
        return '';
    };
    GH.prototype.htmlEncode = function (string) {
        if (string === null) {
            return '';
        }
        return String(string).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
    };
    return GH;
}());
var gh = null;
window.addEventListener('load', function () {
    if (gh === null) {
        gh = new GH();
    }
});
window.addEventListener('message', function (message) {
    if (message.data['type'] === 'gohire') {
        gohire('callback', message.data['data']);
    }
});
function gohire() {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        args[_i] = arguments[_i];
    }
    if (!gh) {
        return;
    }
    if (arguments[0] === 'open') {
        gh.open(arguments[1], arguments[2]);
    }
    else if (arguments[0] === 'callback' && arguments[1] === 'loaded') {
        gh.loaded();
    }
    else if (arguments[0] === 'callback' && arguments[1] === 'close') {
        gh.hide();
    }
}
function applyPool(generalPoolID) {
    gohire('open', generalPoolID, 'generalApplication');
}
