"use strict";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

var tsplus_module_21 = /*#__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; }

/**
 * Splits elements on a delimiter and transforms the splits into desired
 * output.
 *
 * @tsplus fluent ets/Stream splitOnChunk
 */
function splitOnChunk_(self, delimiter, __tsplusTrace) {
  void 0;
  return tsplus_module_4.flatMap_(tsplus_module_3.succeed(delimiter, __tsplusTrace), delimiter => new _StreamInternal.StreamInternal(tsplus_module_2.pipeTo_(self.channel, () => next(delimiter, tsplus_module_1.none, 0, __tsplusTrace))), __tsplusTrace);
}
/**
 * Splits elements on a delimiter and transforms the splits into desired
 * output.
 *
 * @tsplus static ets/Stream/Aspects splitOnChunk
 */


const splitOnChunk = (delimiter, __tsplusTrace) => self => splitOnChunk_(self, delimiter, __tsplusTrace);

exports.splitOnChunk = splitOnChunk;

function next(delimiter, leftover, delimiterIndex, __tsplusTrace) {
  return tsplus_module_21.readWithCause(inputChunk => {
    const buffer = tsplus_module_5.builder();
    const {
      tuple: [carry, delimiterCursor]
    } = tsplus_module_13.reduce_(inputChunk, tsplus_module_8.make(tsplus_module_7.getOrElse_(leftover, () => tsplus_module_6.empty()), delimiterIndex), ({
      tuple: [carry, delimiterCursor]
    }, a) => {
      const concatenated = tsplus_module_9.append_(carry, a);

      if (delimiterCursor < delimiter.length && a === tsplus_module_10.unsafeGet_(delimiter, delimiterCursor)) {
        if (delimiterCursor + 1 === delimiter.length) {
          buffer.append(tsplus_module_11.take_(concatenated, concatenated.length - delimiter.length));
          return tsplus_module_8.make(tsplus_module_6.empty(), 0);
        }

        return tsplus_module_8.make(concatenated, delimiterCursor + 1);
      }

      return tsplus_module_8.make(concatenated, a === tsplus_module_12.unsafeHead(delimiter) ? 1 : 0);
    });
    return tsplus_module_16.zipRight_(tsplus_module_14.write(() => buffer.build()), () => next(delimiter, tsplus_module_15.isNonEmpty(carry) ? tsplus_module_1.some(carry) : tsplus_module_1.none, delimiterCursor, __tsplusTrace));
  }, cause => tsplus_module_19.fold_(leftover, () => tsplus_module_17.failCause(() => cause), chunk => tsplus_module_16.zipRight_(tsplus_module_14.write(() => tsplus_module_18.single(chunk)), () => tsplus_module_17.failCause(() => cause))), done => tsplus_module_19.fold_(leftover, () => tsplus_module_20.succeed(() => done), chunk => tsplus_module_16.zipRight_(tsplus_module_14.write(() => tsplus_module_18.single(chunk)), () => tsplus_module_20.succeed(() => done))));
}
//# sourceMappingURL=splitOnChunk.js.map