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 "@tsplus/stdlib/collections/Chunk/builder";
import * as tsplus_module_4 from "@tsplus/stdlib/data/Maybe/getOrElse";
import * as tsplus_module_5 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/definition";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/Chunk/zipWithIndex";
import * as tsplus_module_8 from "@tsplus/stdlib/collections/Chunk/drop";
import * as tsplus_module_9 from "@tsplus/stdlib/collections/Chunk/reduce";
import * as tsplus_module_10 from "@tsplus/stdlib/collections/Chunk/forEach";
import * as tsplus_module_11 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_12 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_13 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_14 from "@tsplus/stdlib/collections/Chunk/single";
import * as tsplus_module_15 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_16 from "@effect/core/stream/Channel/operations/succeed";
import * as tsplus_module_17 from "@effect/core/stream/Channel/operations/readWithCause";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Splits strings on newlines. Handles both Windows newlines (`\r\n`) and UNIX
 * newlines (`\n`).
 *
 * @tsplus static ets/Stream/Ops splitLines
 */

export function splitLines(self, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_2.pipeTo_(self.channel, () => next(tsplus_module_1.none, false, __tsplusTrace)));
}

function next(leftover, wasSplitCRLF, __tsplusTrace) {
  return tsplus_module_17.readWithCause(incomingChunk => {
    const buffer = tsplus_module_3.builder();
    let inCRLF = wasSplitCRLF;
    let carry = tsplus_module_4.getOrElse_(leftover, () => "");
    tsplus_module_10.forEach_(incomingChunk, string => {
      const concatenated = carry + string;

      if (concatenated.length > 0) {
        // If we had a split CRLF, start reading from the last character of
        // the leftover, which was the '\r'. Otherwise we just skip over the
        // entire previous leftover, as it doesn't contain a newline.
        const continueFrom = inCRLF && carry.length > 0 ? carry.length - 1 : carry.length;
        const {
          tuple: [sliceStart, _, midCRLF]
        } = tsplus_module_9.reduce_(tsplus_module_8.drop_(tsplus_module_7.zipWithIndex(tsplus_module_6.from(concatenated)), continueFrom), tsplus_module_5.make(0, false, inCRLF), ({
          tuple: [sliceStart, skipNext, midCRLF]
        }, {
          tuple: [char, index]
        }) => {
          if (skipNext) {
            return tsplus_module_5.make(sliceStart, false, false);
          }

          switch (char) {
            case "\n":
              {
                buffer.append(concatenated.substring(sliceStart, index));
                return tsplus_module_5.make(index + 1, false, midCRLF);
              }

            case "\r":
              {
                if (index + 1 < concatenated.length && concatenated[index + 1] === "\n") {
                  buffer.append(concatenated.substring(sliceStart, index));
                  return tsplus_module_5.make(index + 2, true, false);
                }

                if (index === concatenated.length - 1) {
                  return tsplus_module_5.make(sliceStart, false, true);
                }

                return tsplus_module_5.make(sliceStart, false, false);
              }

            default:
              {
                return tsplus_module_5.make(sliceStart, false, midCRLF);
              }
          }
        });
        carry = concatenated.slice(sliceStart);
        inCRLF = midCRLF;
      }
    });
    return tsplus_module_12.zipRight_(tsplus_module_11.write(() => buffer.build()), () => next(carry.length > 0 ? tsplus_module_1.some(carry) : tsplus_module_1.none, inCRLF, __tsplusTrace));
  }, cause => tsplus_module_15.fold_(leftover, () => tsplus_module_13.failCause(() => cause), value => tsplus_module_12.zipRight_(tsplus_module_11.write(() => tsplus_module_14.single(value)), () => tsplus_module_13.failCause(() => cause))), done => tsplus_module_15.fold_(leftover, () => tsplus_module_16.succeed(() => done), value => tsplus_module_12.zipRight_(tsplus_module_11.write(() => tsplus_module_14.single(value)), () => tsplus_module_16.succeed(() => done))));
}
//# sourceMappingURL=splitLines.mjs.map