"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.scheduleWith = void 0;
exports.scheduleWith_ = scheduleWith_;

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

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/pipeTo"));

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

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/fromEffect"));

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

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/unsafeGet"));

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

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/write"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/zipRight"));

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

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

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

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

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

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/unwrap"));

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/failCause"));

var tsplus_module_17 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/succeedNow"));

var tsplus_module_18 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/readWithCause"));

var _StreamInternal = /*#__PURE__*/require("@effect/core/stream/Stream/operations/_internal/StreamInternal");

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/stream/Stream/operations/scheduleWith.ts";

/**
 * 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
 */
function scheduleWith_(self, schedule, f, g, __tsplusTrace) {
  void 0;
  return new _StreamInternal.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
 */


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

exports.scheduleWith = scheduleWith;

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.js.map