"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.flattenExitMaybe = flattenExitMaybe;

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

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/readWithCause"));

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

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

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

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Cause/operations/flipCauseMaybe"));

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

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/fold"));

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

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

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

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

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

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

/**
 * Unwraps `Exit` values that also signify end-of-stream by failing with `None`.
 *
 * For `Exit<E, A>` values that do not signal end-of-stream, prefer:
 *
 * ```typescript
 * stream.mapEffect((exit) => Effect.done(exit))
 * ```
 *
 * @tsplus fluent ets/Stream flattenExitMaybe
 */
function flattenExitMaybe(self, __tsplusTrace) {
  const process = tsplus_module_3.readWithCause(chunk => processChunk(chunk, process), cause => tsplus_module_1.failCause(() => cause), () => tsplus_module_2.unit);
  void 0;
  return new _StreamInternal.StreamInternal(tsplus_module_4.pipeTo_(self.channel, () => process));
}

function processChunk(chunk, cont) {
  const {
    tuple: [toEmit, rest]
  } = tsplus_module_6.splitWhere_(chunk, exit => !tsplus_module_5.isSuccess(exit));
  const next = tsplus_module_8.fold_(tsplus_module_10.head(rest), () => cont, exit => tsplus_module_9.fold_(exit, cause => tsplus_module_8.fold_(tsplus_module_7.flipCauseMaybe(cause), () => tsplus_module_2.unit, cause => tsplus_module_1.failCause(() => cause)), () => tsplus_module_2.unit));
  return tsplus_module_14.zipRight_(tsplus_module_13.write(() => tsplus_module_12.collect_(toEmit, exit => tsplus_module_5.isSuccess(exit) ? tsplus_module_11.some(exit.value) : tsplus_module_11.none)), () => next);
}
//# sourceMappingURL=flattenExitMaybe.js.map