"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var promise_serial_1 = require("./promise-serial");
var logger_1 = require("../logger");
var logger = logger_1.default('StepOperations');
var StepOperations = /** @class */ (function () {
    function StepOperations(operations) {
        this.operations = operations;
        this._isCancelled = false;
        this._ignoreError = false;
    }
    Object.defineProperty(StepOperations.prototype, "isCancelled", {
        // private _isPaused: boolean = false;
        // private _isUnsubscribed: boolean = false;
        get: function () {
            return this._isCancelled;
        },
        enumerable: true,
        configurable: true
    });
    StepOperations.sequential = function (operations) {
        return new StepOperations(operations);
    };
    StepOperations.prototype.asObservable = function () {
        var _this = this;
        return rxjs_1.Observable.create(function (observer) {
            logger.debug('Start');
            promise_serial_1.promiseSerial(_this.operations, function (task, index) {
                _this.checkIsCancelled();
                // if (this._isPaused){
                //     return this.pause.
                // }
                // else {
                return task(observer, index);
                // }
            })
                .then(function () {
                logger.debug('complete');
                observer.complete();
            })
                .catch(function (err) {
                logger.debug('error', err);
                if (_this._ignoreError) {
                    observer.complete();
                }
                else {
                    observer.error(err);
                }
            });
            return function () {
                logger.debug('unsubsribed');
                _this._isCancelled = true;
                _this._ignoreError = true;
            };
        });
    };
    StepOperations.prototype.cancel = function () {
        this._isCancelled = true;
    };
    StepOperations.prototype.subscribe = function (observer) {
        return this.asObservable().subscribe(observer);
    };
    // pause(): void {
    //     this._isPaused = true;
    // }
    // private checkIsUnsubscribe() {
    //     if (this._isUnsubscribed) {
    //         throw '__operation_unsubscribed__';
    //     }
    // }
    StepOperations.prototype.checkIsCancelled = function () {
        if (this._isCancelled) {
            throw new Error('Operation cancelled');
        }
    };
    return StepOperations;
}());
exports.StepOperations = StepOperations;
