"use strict";

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

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("@tsplus/stdlib/collections/Chunk/concat"));

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

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

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

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

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/stream/Channel/operations/fail"));

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

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

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

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

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

/**
 * Splits elements based on a predicate.
 *
 * @tsplus fluent ets/Stream split
 */
function split_(self, f, __tsplusTrace) {
  void 0;
  return new _StreamInternal.StreamInternal(tsplus_module_2.pipeTo_(self.channel, () => loop(tsplus_module_1.empty(), f, __tsplusTrace)));
}
/**
 * Splits elements based on a predicate.
 *
 * @tsplus static ets/Stream/Aspects split
 */


const split = (f, __tsplusTrace) => self => split_(self, f, __tsplusTrace);

exports.split = split;

function splitInternal(leftovers, input, f, __tsplusTrace) {
  const {
    tuple: [chunk, remaining]
  } = tsplus_module_4.splitWhere_(tsplus_module_3.concat_(leftovers, input), f);
  return tsplus_module_5.isEmpty(chunk) || tsplus_module_5.isEmpty(remaining) ? loop(tsplus_module_3.concat_(chunk, tsplus_module_6.drop_(remaining, 1)), f, __tsplusTrace) : tsplus_module_9.zipRight_(tsplus_module_8.write(() => tsplus_module_7.single(chunk)), () => splitInternal(tsplus_module_1.empty(), tsplus_module_6.drop_(remaining, 1), f, __tsplusTrace));
}

function loop(leftovers, f, __tsplusTrace) {
  return tsplus_module_14.readWith(input => splitInternal(leftovers, input, f, __tsplusTrace), err => tsplus_module_10.fail(() => err), () => tsplus_module_5.isEmpty(leftovers) ? tsplus_module_11.unit : tsplus_module_13.isNone(tsplus_module_12.find_(leftovers, f)) ? tsplus_module_9.zipRight_(tsplus_module_8.write(() => tsplus_module_7.single(leftovers)), () => tsplus_module_11.unit) : tsplus_module_9.zipRight_(splitInternal(tsplus_module_1.empty(), leftovers, f, __tsplusTrace), () => tsplus_module_11.unit));
}
//# sourceMappingURL=split.js.map