"use strict";

var PBI = PBI || {};

PBI.RemoteLoginLoad = function (args) {
    if (typeof PBI.RemoteLogin !== "function") { return; }

    new PBI.RemoteLogin(args);
};

PBI.RemoteLogin = function (args) {
    var _this = this;

    if (!args.applicationPath) { return; }
    if (!args.formId) { return; }
    if (!args.passwordId) { return; }
    if (!args.routingTransit) { return; }
    if (!args.usernameId) { return; }

    _this._applicationPath = args.applicationPath;
    if (args.applicationPath.substr(args.applicationPath.length - 1) !== "/") {
        _this._applicationPath += "/";
    }

    const cloudHosts = ["fiservapps.com", "onefiserv.net"];
    const isCloud = cloudHosts.some(function (x) {
        return args.applicationPath.includes(x);
    });

    var script = document.createElement("script");
    script.setAttribute('async', false);
    script.type = "text/javascript";
    script.language = "javascript";
    script.src = _this._applicationPath + (isCloud ? "js/remoteLoginPostCloud.js?v=3.0" : "js/remoteLoginPost.js?v=3.0");

    script.addEventListener('load', function () {
        if (typeof PBI.RemoteLoginPost !== "function") { return; }

        new PBI.RemoteLoginPost(args);
    });

    document.head.appendChild(script);
};