"use strict";

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

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

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

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

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

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

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

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

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

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

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

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

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

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/fold"));

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

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

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

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

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; }

/**
 * Repeats each element of the stream using the provided schedule. When the
 * schedule is finished, then the output of the schedule will be emitted into
 * the stream. Repetitions are done in addition to the first execution, which
 * means using `Schedule.recurs(1)` actually results in the original effect,
 * plus an additional recurrence, for a total of two repetitions of each value
 * in the stream.
 *
 * This function accepts two conversion functions, which allow the output of
 * this stream and the output of the provided schedule to be unified into a
 * single type. For example, `Either` or similar data type.
 *
 * @tsplus fluent ets/Stream repeatElementsWith
 */
function repeatElementsWith_(self, schedule, f, g, __tsplusTrace) {
  void 0;
  return new _StreamInternal.StreamInternal(tsplus_module_7.pipeTo_(self.channel, () => tsplus_module_6.unwrap(() => tsplus_module_5.map_(tsplus_module_4.driver(schedule()), driver => {
    const loop = tsplus_module_3.readWith(chunk => feed(loop, driver, f, g, chunk, __tsplusTrace), err => tsplus_module_1.fail(() => err), () => tsplus_module_2.unit);
    return loop;
  }, __tsplusTrace))));
}
/**
 * Repeats each element of the stream using the provided schedule. When the
 * schedule is finished, then the output of the schedule will be emitted into
 * the stream. Repetitions are done in addition to the first execution, which
 * means using `Schedule.recurs(1)` actually results in the original effect,
 * plus an additional recurrence, for a total of two repetitions of each value
 * in the stream.
 *
 * This function accepts two conversion functions, which allow the output of
 * this stream and the output of the provided schedule to be unified into a
 * single type. For example, `Either` or similar data type.
 *
 * @tsplus static ets/Stream/Aspects repeatElementsWith
 */


const repeatElementsWith = (schedule, f, g, __tsplusTrace) => self => repeatElementsWith_(self, schedule, f, g, __tsplusTrace);

exports.repeatElementsWith = repeatElementsWith;

function feed(loop, driver, f, g, input, __tsplusTrace) {
  return tsplus_module_13.fold_(tsplus_module_12.head(input), () => loop, a => tsplus_module_11.zipRight_(tsplus_module_9.write(() => tsplus_module_8.single(f(a))), () => step(loop, driver, f, g, tsplus_module_10.drop_(input, 1), a, __tsplusTrace)));
}

function step(loop, driver, f, g, input, value, __tsplusTrace) {
  const advance = tsplus_module_14.as_(driver.next(value), () => tsplus_module_11.zipRight_(tsplus_module_9.write(() => tsplus_module_8.single(f(value))), () => step(loop, driver, f, g, input, value, __tsplusTrace)), __tsplusTrace);
  const reset = tsplus_module_5.map_(tsplus_module_16.tap_(tsplus_module_15.orDie(driver.last, __tsplusTrace), () => driver.reset, __tsplusTrace), b => tsplus_module_11.zipRight_(tsplus_module_9.write(() => tsplus_module_8.single(g(b))), () => feed(loop, driver, f, g, input, __tsplusTrace)), __tsplusTrace);
  return tsplus_module_6.unwrap(() => tsplus_module_17.orElse_(advance, () => reset, __tsplusTrace));
}
//# sourceMappingURL=repeatElementsWith.js.map