"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.TaskWithDelayAndMinimalDuration = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _globalObject = require("@skbkontur/global-object");var








TaskWithDelayAndMinimalDuration = exports.TaskWithDelayAndMinimalDuration =





function TaskWithDelayAndMinimalDuration(initialValues) {var _this = this;this.isTaskActive = false;this.



  setTimeoutBeforeTaskStart = function () {
    _this.timeoutBeforeTaskStart = _globalObject.globalObject.setTimeout(function () {
      _this.isTaskActive && _this.taskParams.taskStartCallback();
      _this.clearTimeoutBeforeTaskStart();
      _this.setTimeoutBeforeTaskStop();
    }, _this.taskParams.delayBeforeTaskStart);
  };this.

  setTimeoutBeforeTaskStop = function () {
    _this.timeoutBeforeTaskStop = _globalObject.globalObject.setTimeout(function () {
      !_this.isTaskActive && _this.taskParams.taskStopCallback();
      _this.clearTimeoutBeforeTaskStop();
    }, _this.taskParams.durationOfTask);
  };this.

  clearTimeoutBeforeTaskStart = function () {
    _this.timeoutBeforeTaskStart && _globalObject.globalObject.clearTimeout(_this.timeoutBeforeTaskStart);
    _this.timeoutBeforeTaskStart = null;
  };this.

  clearTimeoutBeforeTaskStop = function () {
    _this.timeoutBeforeTaskStop && _globalObject.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 = (0, _extends2.default)({}, _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;};