const fileName_1 = "(@effect/core) _src/io/Schedule/operations/driver.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/exceptions";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/fail";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_4 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_6 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_7 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_9 from "@effect/core/io/Ref/operations/make";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_11 from "@tsplus/stdlib/data/Duration";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/sleep";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/as";
import * as tsplus_module_14 from "@effect/core/io/Clock/operations/currentTime";
import * as tsplus_module_15 from "@effect/core/io/Effect/operations/do";
import { Driver } from "@effect/core/io/Schedule/Driver";
/**
 * 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
 */

export 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(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.mjs.map