"use strict";

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

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

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

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

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

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

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

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

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

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Ref/operations/make"));

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

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

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

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

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Clock/operations/currentTime"));

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

var _Driver = /*#__PURE__*/require("@effect/core/io/Schedule/Driver");

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/driver.ts";

/**
 * Returns a driver that can be used to step the schedule, appropriately
 * handling sleeping.
 *
 * @tsplus getter ets/Schedule driver
 * @tsplus fluent ets/Schedule/WithState driver
 */
function driver(self) {
  return tsplus_module_8.map_(tsplus_module_9.makeRef(() => tsplus_module_7.make(tsplus_module_6.none, self._initial), fileName_1 + ":13:18"), ref => {
    const last = tsplus_module_5.flatMap_(ref.get(fileName_1 + ":14:56"), ({
      tuple: [element, _]
    }) => tsplus_module_4.fold_(element, () => tsplus_module_2.fail(() => new tsplus_module_1.NoSuchElement(), fileName_1 + ":15:31"), out => tsplus_module_3.succeed(() => out, fileName_1 + ":15:77")), fileName_1 + ":14:66");
    const reset = ref.set(tsplus_module_7.make(tsplus_module_6.none, self._initial), fileName_1 + ":18:54");
    const state = tsplus_module_8.map_(ref.get(fileName_1 + ":20:55"), tuple => tuple.get(1), fileName_1 + ":20:61");
    return new _Driver.Driver(next(self, ref), last, reset, state);
  }, fileName_1 + ":13:84");
}

function next(self, ref) {
  return input => tsplus_module_5.flatMap_(tsplus_module_15.bind_(tsplus_module_15.bind_(tsplus_module_15.bind_(tsplus_module_15.Do(), "state", () => tsplus_module_8.map_(ref.get(fileName_1 + ":32:35"), tuple => tuple.get(1), fileName_1 + ":32:41"), fileName_1 + ":32:12"), "now", () => tsplus_module_14.currentTime, fileName_1 + ":33:12"), "decision", ({
    now,
    state
  }) => self._step(now, input, state), fileName_1 + ":34:12"), ({
    now,
    decision: {
      tuple: [state, out, decision]
    }
  }) => decision._tag === "Done" ? tsplus_module_10.zipRight_(ref.set(tsplus_module_7.make(tsplus_module_6.some(out), state), fileName_1 + ":37:20"), () => tsplus_module_2.fail(() => tsplus_module_6.none, fileName_1 + ":37:65"), fileName_1 + ":37:53") : tsplus_module_10.zipRight_(ref.set(tsplus_module_7.make(tsplus_module_6.some(out), state), fileName_1 + ":38:20"), () => tsplus_module_13.as_(tsplus_module_12.sleep(() => new tsplus_module_11.Duration(decision.interval.startMillis - now), fileName_1 + ":39:25"), () => out, fileName_1 + ":39:79"), fileName_1 + ":38:53"), fileName_1 + ":35:15");
}
//# sourceMappingURL=driver.js.map