import _extends from "@babel/runtime/helpers/esm/extends";
import { globalObject } from '@skbkontur/global-object';
export var TaskWithDelayAndMinimalDuration = function TaskWithDelayAndMinimalDuration(initialValues) {
  var _this = this;
  this.isTaskActive = false;
  this.setTimeoutBeforeTaskStart = function () {
    _this.timeoutBeforeTaskStart = globalObject.setTimeout(function () {
      _this.isTaskActive && _this.taskParams.taskStartCallback();
      _this.clearTimeoutBeforeTaskStart();
      _this.setTimeoutBeforeTaskStop();
    }, _this.taskParams.delayBeforeTaskStart);
  };
  this.setTimeoutBeforeTaskStop = function () {
    _this.timeoutBeforeTaskStop = globalObject.setTimeout(function () {
      !_this.isTaskActive && _this.taskParams.taskStopCallback();
      _this.clearTimeoutBeforeTaskStop();
    }, _this.taskParams.durationOfTask);
  };
  this.clearTimeoutBeforeTaskStart = function () {
    _this.timeoutBeforeTaskStart && globalObject.clearTimeout(_this.timeoutBeforeTaskStart);
    _this.timeoutBeforeTaskStart = null;
  };
  this.clearTimeoutBeforeTaskStop = function () {
    _this.timeoutBeforeTaskStop && globalObject.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 = _extends({}, _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;
};