var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports", '../util/tryCatch', '../util/errorObject', '../util/subscribeToResult', '../OuterSubscriber'], function (require, exports, tryCatch_1, errorObject_1, subscribeToResult_1, OuterSubscriber_1) {
    var MergeMapOperator = (function () {
        function MergeMapOperator(project, resultSelector, concurrent) {
            if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
            this.project = project;
            this.resultSelector = resultSelector;
            this.concurrent = concurrent;
        }
        MergeMapOperator.prototype.call = function (observer) {
            return new MergeMapSubscriber(observer, this.project, this.resultSelector, this.concurrent);
        };
        return MergeMapOperator;
    })();
    exports.MergeMapOperator = MergeMapOperator;
    var MergeMapSubscriber = (function (_super) {
        __extends(MergeMapSubscriber, _super);
        function MergeMapSubscriber(destination, project, resultSelector, concurrent) {
            if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
            _super.call(this, destination);
            this.project = project;
            this.resultSelector = resultSelector;
            this.concurrent = concurrent;
            this.hasCompleted = false;
            this.buffer = [];
            this.active = 0;
            this.index = 0;
        }
        MergeMapSubscriber.prototype._next = function (value) {
            if (this.active < this.concurrent) {
                var resultSelector = this.resultSelector;
                var index = this.index++;
                var ish = tryCatch_1.default(this.project)(value, index);
                var destination = this.destination;
                if (ish === errorObject_1.errorObject) {
                    destination.error(ish.e);
                }
                else {
                    this.active++;
                    this._innerSub(ish, value, index);
                }
            }
            else {
                this.buffer.push(value);
            }
        };
        MergeMapSubscriber.prototype._innerSub = function (ish, value, index) {
            this.add(subscribeToResult_1.default(this, ish, value, index));
        };
        MergeMapSubscriber.prototype._complete = function () {
            this.hasCompleted = true;
            if (this.active === 0 && this.buffer.length === 0) {
                this.destination.complete();
            }
        };
        MergeMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex) {
            var _a = this, destination = _a.destination, resultSelector = _a.resultSelector;
            if (resultSelector) {
                var result = tryCatch_1.default(resultSelector)(outerValue, innerValue, outerIndex, innerIndex);
                if (result === errorObject_1.errorObject) {
                    destination.error(errorObject_1.errorObject.e);
                }
                else {
                    destination.next(result);
                }
            }
            else {
                destination.next(innerValue);
            }
        };
        MergeMapSubscriber.prototype.notifyComplete = function (innerSub) {
            var buffer = this.buffer;
            this.remove(innerSub);
            this.active--;
            if (buffer.length > 0) {
                this._next(buffer.shift());
            }
            else if (this.active === 0 && this.hasCompleted) {
                this.destination.complete();
            }
        };
        return MergeMapSubscriber;
    })(OuterSubscriber_1.default);
    exports.MergeMapSubscriber = MergeMapSubscriber;
});
//# sourceMappingURL=mergeMap-support.js.map