var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var TaskWithDelayAndMinimalDuration = /** @class */ (function () {
    function TaskWithDelayAndMinimalDuration(initialValues) {
        var _this = this;
        this.isTaskActive = false;
        this.setTimeoutBeforeTaskStart = function () {
            _this.timeoutBeforeTaskStart = setTimeout(function () {
                _this.isTaskActive && _this.taskParams.taskStartCallback();
                _this.clearTimeoutBeforeTaskStart();
                _this.setTimeoutBeforeTaskStop();
            }, _this.taskParams.delayBeforeTaskStart);
        };
        this.setTimeoutBeforeTaskStop = function () {
            _this.timeoutBeforeTaskStop = setTimeout(function () {
                !_this.isTaskActive && _this.taskParams.taskStopCallback();
                _this.clearTimeoutBeforeTaskStop();
            }, _this.taskParams.durationOfTask);
        };
        this.clearTimeoutBeforeTaskStart = function () {
            _this.timeoutBeforeTaskStart && clearTimeout(_this.timeoutBeforeTaskStart);
            _this.timeoutBeforeTaskStart = null;
        };
        this.clearTimeoutBeforeTaskStop = function () {
            _this.timeoutBeforeTaskStop && clearTimeout(_this.timeoutBeforeTaskStop);
            _this.timeoutBeforeTaskStop = null;
        };
        this.start = function () {
            if (_this.isTaskActive) {
                return;
            }
            _this.isTaskActive = true;
            if (_this.timeoutBeforeTaskStop) {
                return;
            }
            if (_this.timeoutBeforeTaskStart) {
                _this.clearTimeoutBeforeTaskStart();
            }
            _this.setTimeoutBeforeTaskStart();
        };
        this.update = function (updateData) {
            _this.taskParams = __assign(__assign({}, _this.taskParams), updateData);
        };
        this.stop = function () {
            if (!_this.isTaskActive) {
                return;
            }
            _this.isTaskActive = false;
            if (!_this.timeoutBeforeTaskStart && !_this.timeoutBeforeTaskStop) {
                _this.taskParams.taskStopCallback();
            }
            if (_this.timeoutBeforeTaskStart) {
                _this.clearTimeoutBeforeTaskStart();
            }
        };
        this.clearTask = function () {
            _this.isTaskActive = false;
            _this.clearTimeoutBeforeTaskStart();
            _this.clearTimeoutBeforeTaskStop();
        };
        this.taskParams = initialValues;
    }
    return TaskWithDelayAndMinimalDuration;
}());
export { TaskWithDelayAndMinimalDuration };
//# sourceMappingURL=taskWithDelayAndMinimalDuration.js.map