"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Session_1 = require("leadfoot/Session");
var Task_1 = require("@dojo/core/async/Task");
function getCoverageData(coverageVariable) {
    var coverageData = (function () { return this; })()[coverageVariable];
    return coverageData && JSON.stringify(coverageData);
}
var ProxiedSession = (function (_super) {
    __extends(ProxiedSession, _super);
    function ProxiedSession() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.coverageEnabled = false;
        _this.coverageVariable = '';
        _this.serverBasePathLength = 0;
        _this.serverUrl = '';
        return _this;
    }
    ProxiedSession.prototype.get = function (url) {
        var _this = this;
        if (!/^[A-Za-z][A-Za-z0-9+.-]+:/.test(url)) {
            if (url.indexOf(this.executor.config.basePath) === 0) {
                url = url.slice(this.serverBasePathLength);
            }
            url = this.serverUrl + url;
        }
        if (!this.coverageEnabled) {
            return _super.prototype.get.call(this, url);
        }
        var shouldGetPromise;
        if (this.capabilities.brokenExecuteForNonHttpUrl) {
            shouldGetPromise = Task_1.default.resolve(this.getCurrentUrl().then(function (url) { return (/^https?:/i).test(url); }));
        }
        else {
            shouldGetPromise = Task_1.default.resolve(true);
        }
        var task = shouldGetPromise.then(function (shouldGetCoverage) {
            if (shouldGetCoverage) {
                return _this.execute(getCoverageData, [_this.coverageVariable]).then(function (coverageData) {
                    return coverageData && _this.executor.emit('coverage', {
                        sessionId: _this.sessionId,
                        coverage: JSON.parse(coverageData)
                    });
                });
            }
        }).finally(function () {
            return _super.prototype.get.call(_this, url);
        });
        return task;
    };
    ProxiedSession.prototype.quit = function () {
        var _this = this;
        return this
            .setHeartbeatInterval(0)
            .then(function () {
            if (_this.coverageEnabled) {
                return _this.execute(getCoverageData, [_this.coverageVariable]).then(function (coverageData) {
                    return coverageData && _this.executor.emit('coverage', {
                        sessionId: _this.sessionId,
                        coverage: JSON.parse(coverageData)
                    });
                });
            }
        })
            .finally(function () {
            return _super.prototype.quit.call(_this);
        });
    };
    ProxiedSession.prototype.setHeartbeatInterval = function (delay) {
        var _this = this;
        this._heartbeatIntervalHandle && this._heartbeatIntervalHandle.remove();
        if (delay) {
            var sendHeartbeat_1 = function () {
                var timeoutId;
                var cancelled = false;
                var startTime = Date.now();
                _this._heartbeatIntervalHandle = {
                    remove: function () {
                        cancelled = true;
                        clearTimeout(timeoutId);
                    }
                };
                _this.getCurrentUrl().then(function () {
                    if (!cancelled) {
                        timeoutId = setTimeout(sendHeartbeat_1, delay - (Date.now() - startTime));
                    }
                }).catch(function (error) { return _this.executor.emit('error', error); });
            };
            sendHeartbeat_1();
        }
        return Task_1.default.resolve();
    };
    return ProxiedSession;
}(Session_1.default));
exports.default = ProxiedSession;
//# sourceMappingURL=ProxiedSession.js.map