"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fixed = fixed;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/definition"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Schedule/Interval"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Schedule/Decision"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Duration"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/fold"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeed"));

var _makeWithState = /*#__PURE__*/require("@effect/core/io/Schedule/operations/_internal/makeWithState");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const fileName_1 = "(@effect/core) _src/io/Schedule/operations/fixed.ts";

/**
 * A schedule that recurs on a fixed interval. Returns the number of
 * repetitions of the schedule so far.
 *
 * If the action run between updates takes longer than the interval, then the
 * action will be run immediately, but re-runs will not "pile up".
 *
 * ```text
 * |-----interval-----|-----interval-----|-----interval-----|
 * |---------action--------||action|-----|action|-----------|
 * ```
 *
 * @tsplus static ets/Schedule/Ops fixed
 */
function fixed(interval) {
  return (0, _makeWithState.makeWithState)(tsplus_module_2.make(tsplus_module_1.emptyOf(), 0), (now, _, {
    tuple: [option, n]
  }) => tsplus_module_7.succeed(() => {
    const interval0 = interval();
    const intervalMillis = interval0.millis;
    return tsplus_module_6.fold_(option, () => tsplus_module_2.make(tsplus_module_2.make(tsplus_module_1.some(tsplus_module_2.make(now, now + intervalMillis)), n + 1), n, tsplus_module_4._continue(tsplus_module_3.after(now + intervalMillis))), ({
      tuple: [startMillis, lastRun]
    }) => {
      const runningBehind = now > lastRun + intervalMillis;
      const boundary = tsplus_module_5.equals_(interval0, tsplus_module_5.millis(0)) ? interval0 : new tsplus_module_5.Duration(intervalMillis - (now - startMillis) % intervalMillis);
      const sleepTime = tsplus_module_5.equals_(boundary, tsplus_module_5.millis(0)) ? interval0 : boundary;
      const nextRun = runningBehind ? now : now + sleepTime.millis;
      return tsplus_module_2.make(tsplus_module_2.make(tsplus_module_1.some(tsplus_module_2.make(startMillis, nextRun)), n + 1), n, tsplus_module_4._continue(tsplus_module_3.after(nextRun)));
    });
  }, fileName_1 + ":28:19"));
}
//# sourceMappingURL=fixed.js.map