const fileName_1 = "(@effect/core) _src/stream/Stream/operations/scheduleWith.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_3 from "@effect/core/io/Schedule/operations/driver";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/fromEffect";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/flatMap";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/unsafeGet";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/Chunk/make";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/orDie";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/zipLeft";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/foldEffect";
import * as tsplus_module_15 from "@effect/core/stream/Channel/operations/unwrap";
import * as tsplus_module_16 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_17 from "@effect/core/stream/Channel/operations/succeedNow";
import * as tsplus_module_18 from "@effect/core/stream/Channel/operations/readWithCause";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Schedules the output of the stream using the provided `schedule` and emits
 * its output at the end (if `schedule` is finite). Uses the provided function
 * to align the stream and schedule outputs on the same type.
 *
 * @tsplus fluent ets/Stream scheduleWith
 */

export function scheduleWith_(self, schedule, f, g, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_5.flatMap_(tsplus_module_4.fromEffect(() => tsplus_module_3.driver(schedule())), driver => tsplus_module_2.pipeTo_(self.channel, () => loop(driver, tsplus_module_1.empty(), f, g, 0))));
}
/**
 * Schedules the output of the stream using the provided `schedule` and emits
 * its output at the end (if `schedule` is finite). Uses the provided function
 * to align the stream and schedule outputs on the same type.
 *
 * @tsplus static ets/Stream/Aspects scheduleWith
 */

export const scheduleWith = (schedule, f, g, __tsplusTrace) => self => scheduleWith_(self, schedule, f, g, __tsplusTrace);

function loop(driver, chunk, f, g, index) {
  if (index < chunk.length) {
    return tsplus_module_15.unwrap(() => {
      const a = tsplus_module_6.unsafeGet_(chunk, index);
      return tsplus_module_14.foldEffect_(driver.next(a), () => tsplus_module_12.zipLeft_(tsplus_module_11.map_(tsplus_module_10.orDie(driver.last, fileName_1 + ":48:19"), b => tsplus_module_9.zipRight_(tsplus_module_8.write(() => tsplus_module_7.make(f(a), g(b))), () => loop(driver, chunk, f, g, index + 1)), fileName_1 + ":49:17"), () => driver.reset, fileName_1 + ":53:16"), () => tsplus_module_13.succeedNow(tsplus_module_9.zipRight_(tsplus_module_8.write(() => tsplus_module_7.make(f(a))), () => loop(driver, chunk, f, g, index + 1)), fileName_1 + ":55:28"), fileName_1 + ":45:39");
    });
  }

  return tsplus_module_18.readWithCause(chunk => loop(driver, chunk, f, g, 0), cause => tsplus_module_16.failCause(() => cause), tsplus_module_17.succeedNow);
}
//# sourceMappingURL=scheduleWith.mjs.map