const fileName_1 = "(@effect/core) _src/io/Schedule/operations/ensuring.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/as";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/flatMap";
import { makeWithState } from "@effect/core/io/Schedule/operations/_internal/makeWithState";
/**
 * Returns a new schedule that will run the specified finalizer as soon as the
 * schedule is complete. Note that unlike `Effect.ensuring`, this method does not
 * guarantee the finalizer will be run. The `Schedule` may not initialize or
 * the driver of the schedule may not run to completion. However, if the
 * `Schedule` ever decides not to continue, then the finalizer will be run.
 *
 * @tsplus fluent ets/Schedule ensuring
 * @tsplus fluent ets/Schedule/WithState ensuring
 */

export function ensuring_(self, finalizer) {
  return makeWithState(self._initial, (now, input, state) => tsplus_module_4.flatMap_(self._step(now, input, state), ({
    tuple: [state, out, decision]
  }) => decision._tag === "Done" ? tsplus_module_2.as_(finalizer, () => tsplus_module_1.make(state, out, decision), fileName_1 + ":23:25") : tsplus_module_3.succeedNow(tsplus_module_1.make(state, out, decision), fileName_1 + ":24:30"), fileName_1 + ":21:15"));
}
/**
 * Returns a new schedule that will run the specified finalizer as soon as the
 * schedule is complete. Note that unlike `Effect.ensuring`, this method does not
 * guarantee the finalizer will be run. The `Schedule` may not initialize or
 * the driver of the schedule may not run to completion. However, if the
 * `Schedule` ever decides not to continue, then the finalizer will be run.
 *
 * @tsplus static ets/Schedule/Aspects ensuring
 */

export const ensuring = finalizer => self => ensuring_(self, finalizer);
//# sourceMappingURL=ensuring.mjs.map