import { DriverWeb } from "./DriverWeb";
import { DriverWin } from "./DriverWin";
import { EPageToHostCmd } from "./EPageToHostCmd";
var Connector = /** @class */ (function () {
    function Connector(readyCallback) {
        var _this = this;
        Connector._connectorInstance = this;
        this.hostParametersReady = false;
        var tThis = this;
        function checkConfigRes(res) {
            if ((typeof res == "boolean" && res === true) ||
                (typeof res == "object" &&
                    res.ready === true)) {
                tThis.hostParametersReady = true;
                if (typeof res == "boolean") {
                    tThis._hostParameters = {};
                }
                else {
                    tThis._hostParameters = (res).config;
                }
                //console.info("HP:", tThis._hostParameters);
                return true;
            }
            else {
                tThis.driver = undefined;
                throw new Error(Connector.NoHostException);
            }
        }
        if (window.external && window.external["isHostReady"] !== undefined) {
            if (checkConfigRes(window.external["isHostReady"]())) {
                //console.warn("IGD.Driver found a windows embedded browser host");
                this.driver = new DriverWin();
                readyCallback(this._hostParameters);
            }
        }
        else {
            this.driver = new DriverWeb();
            // give the host 5s to reply
            var tTimerID_1 = window.setTimeout(function () {
                _this.driver = undefined;
                throw new Error(Connector.NoHostException);
            }, 5000);
            this.driver.sendToHost(EPageToHostCmd.IsHostReady, null, function (res) {
                window.clearTimeout(tTimerID_1);
                if (checkConfigRes(res)) {
                    if (readyCallback) {
                        readyCallback(_this._hostParameters);
                    }
                }
            });
        }
    }
    /**
     * Send Command with Data to Host.
     * Implement this in Driver subclass
     * @param cmd Command to send
     * @param dataObj Data send along with command. See enum EPageToHostCmd
     * @param callback Callback function with the result or the requested data
     */
    Connector.prototype.sendToHost = function (cmd, dataObj, callback) {
        if (this.driver) {
            return this.driver.sendToHost(cmd, dataObj, callback);
        }
    };
    Connector.prototype.getHostParameters = function () {
        if (this.driver) {
            return {
                ready: this.hostParametersReady,
                config: this._hostParameters,
            };
        }
        else {
            return undefined;
        }
    };
    Connector.prototype.destroy = function () {
        if (this.driver) {
            this.driver.detachListeners();
        }
        this.driver = undefined;
        Connector._connectorInstance = undefined;
    };
    Connector.NotImplementedException = "Not implemented";
    Connector.NoHostException = "IGD.Core.Connector could not find a host!";
    return Connector;
}());
export { Connector };
