const fileName_1 = "(@effect/core) _src/io/Schedule/operations/modifyDelayEffect.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_3 from "@effect/core/io/Schedule/Interval";
import * as tsplus_module_4 from "@effect/core/io/Schedule/Decision";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/flatMap";
import { makeWithState } from "@effect/core/io/Schedule/operations/_internal/makeWithState";
/**
 * Returns a new schedule that modifies the delay using the specified
 * effectual function.
 *
 * @tsplus fluent ets/Schedule modifyDelayEffect
 * @tsplus fluent ets/Schedule/WithState modifyDelayEffect
 */

export function modifyDelayEffect_(self, f) {
  return makeWithState(self._initial, (now, input, state) => tsplus_module_6.flatMap_(self._step(now, input, state), ({
    tuple: [state, out, decision]
  }) => {
    if (decision._tag === "Done") {
      return tsplus_module_2.succeedNow(tsplus_module_1.make(state, out, decision), fileName_1 + ":21:35");
    }

    const delay = tsplus_module_3.size(tsplus_module_3.fromStartEndMillis(now, decision.interval.startMillis));
    return tsplus_module_5.map_(f(out, delay), duration => {
      const oldStart = decision.interval.startMillis;
      const newStart = now + duration.millis;
      const delta = newStart - oldStart;
      const newEnd = decision.interval.endMillis + delta;
      const newInterval = tsplus_module_3.fromStartEndMillis(newStart, newEnd);
      return tsplus_module_1.make(state, out, tsplus_module_4._continue(newInterval));
    }, fileName_1 + ":26:33");
  }, fileName_1 + ":19:44"));
}
/**
 * Returns a new schedule that modifies the delay using the specified
 * effectual function.
 *
 * @tsplus static ets/Schedule/Aspects modifyDelayEffect
 */

export const modifyDelayEffect = f => self => modifyDelayEffect_(self, f);
//# sourceMappingURL=modifyDelayEffect.mjs.map