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 __());
    };
})();
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "@dojo/core/request/providers/xhr", "@dojo/core/async/Task", "./Base"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var xhr_1 = require("@dojo/core/request/providers/xhr");
    var Task_1 = require("@dojo/core/async/Task");
    var Base_1 = require("./Base");
    var HttpChannel = (function (_super) {
        __extends(HttpChannel, _super);
        function HttpChannel(options) {
            var _this = _super.call(this, options) || this;
            _this._sequence = 1;
            _this._maxPostSize = options.maxPostSize || 100000;
            _this._messageBuffer = [];
            _this._lastRequest = Task_1.default.resolve();
            return _this;
        }
        HttpChannel.prototype._sendData = function (name, data) {
            var _this = this;
            var id = String(this._sequence++);
            var sessionId = this.sessionId;
            var message = { id: id, sessionId: sessionId, name: name, data: data };
            this._messageBuffer.push(JSON.stringify(message));
            return this._lastRequest = this._lastRequest.then(function () { return _this._send(); });
        };
        /**
         * If there are messages to send, send them.
         */
        HttpChannel.prototype._send = function () {
            if (this._messageBuffer.length === 0) {
                return Task_1.default.resolve();
            }
            return this._sendMessages();
        };
        /**
         * Some testing services have problems handling large message POSTs, so limit the maximum size of each POST body to
         * maxPostSize bytes. Always send at least one message, even if it's more than maxPostSize bytes.
         */
        HttpChannel.prototype._sendMessages = function () {
            var _this = this;
            var messages = this._messageBuffer;
            var block = [messages.shift()];
            var size = block[0].length;
            while (messages.length > 0 && size + messages[0].length < this._maxPostSize) {
                size += messages[0].length;
                block.push(messages.shift());
            }
            return xhr_1.default(this.url, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(block)
            }).then(function () {
                if (messages.length > 0) {
                    return _this._sendMessages();
                }
            });
        };
        return HttpChannel;
    }(Base_1.default));
    exports.default = HttpChannel;
});
//# sourceMappingURL=Http.js.map