var GH = /** @class */ (function () {
    function GH() {
        this.clientId = '';
        this.host = window.location.host;
        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  ";
        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, function (data) {
            this.get("/widget-jobs/" + this.clientId, function (data) {
                if (data) {
                    if (data['colour']) {
                        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-item-detail .gohire-job-list-item-detail-title { color: " + data['colour'] + " !important; }\n            .gohire-job-list-item-nav path { fill: " + data['colour'] + " !important; }\n\n            ";
                        document.body.appendChild(style_1);
                    }
                    if (!Array.isArray(data['jobs'])) {
                        var div = document.createElement('div');
                        div.className = 'gohire-job-list-empty';
                        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 {
                        _this.template = data['template'] || "\n\n            <a href=\"{{link}}\" target=\"_blank\">\n              <div class=\"gohire-job-list-item\">\n                <div class=\"gohire-job-list-item-detail\">\n                  <p class=\"gohire-job-list-item-detail-title gohire-brand-colour\">{{title}}</p>\n                  <p class=\"gohire-job-list-item-detail-sub\">{{location}} &middot; Posted {{date}}</p>\n                </div>\n                <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              </div>\n            </a>\n\n            ";
                        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.innerHTML = html;
                            _this.jobs.parentNode.insertBefore(div, _this.jobs);
                            _this.jobs.parentNode.removeChild(_this.jobs);
                            _this.jobs = div;
                        }
                    }
                }
            });
        }
    };
    GH.prototype.open = function (jobId, source) {
        var _this = this;
        if (!jobId) {
            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();
    }
}