import * as tsplus_module_1 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_3 from "@effect/core/stream/Stream/operations/succeed";
import * as tsplus_module_4 from "@effect/core/stream/Stream/operations/flatMap";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/Chunk/builder";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_7 from "@tsplus/stdlib/data/Maybe/getOrElse";
import * as tsplus_module_8 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_9 from "@tsplus/stdlib/collections/Chunk/append";
import * as tsplus_module_10 from "@tsplus/stdlib/collections/Chunk/unsafeGet";
import * as tsplus_module_11 from "@tsplus/stdlib/collections/Chunk/take";
import * as tsplus_module_12 from "@tsplus/stdlib/collections/Chunk/unsafeHead";
import * as tsplus_module_13 from "@tsplus/stdlib/collections/Chunk/reduce";
import * as tsplus_module_14 from "@effect/core/stream/Channel/operations/writeChunk";
import * as tsplus_module_15 from "@tsplus/stdlib/collections/Chunk/isNonEmpty";
import * as tsplus_module_16 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_17 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_18 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_19 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_20 from "@effect/core/stream/Channel/operations/succeed";
import * as tsplus_module_21 from "@effect/core/stream/Channel/operations/readWithCause";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Splits elements on a delimiter and transforms the splits into desired
 * output, flattening the resulting chunks into the stream.
 *
 * @tsplus fluent ets/Stream splitOnChunkFlatten
 */

export function splitOnChunkFlatten_(self, delimiter, __tsplusTrace) {
  void 0;
  return tsplus_module_4.flatMap_(tsplus_module_3.succeed(delimiter, __tsplusTrace), delimiter => new 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, flattening the resulting chunks into the stream.
 *
 * @tsplus static ets/Stream/Aspects splitOnChunkFlatten
 */

export const splitOnChunkFlatten = (delimiter, __tsplusTrace) => self => splitOnChunkFlatten_(self, delimiter, __tsplusTrace);

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.writeChunk(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_18.write(() => chunk), () => tsplus_module_17.failCause(() => cause))), done => tsplus_module_19.fold_(leftover, () => tsplus_module_20.succeed(() => done), chunk => tsplus_module_16.zipRight_(tsplus_module_18.write(() => chunk), () => tsplus_module_20.succeed(() => done))));
}
//# sourceMappingURL=splitOnChunkFlatten.mjs.map