"use strict";

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

var C = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Collections/Immutable/Chunk"));

var Tuple = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Collections/Immutable/Tuple"));

var T = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Effect"));

var M = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Effect/Managed"));

var Ref = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Effect/Ref"));

var _Transducer = /*#__PURE__*/require("@effect-ts/core/Effect/Stream/Transducer");

var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect-ts/core/Option"));

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

// ets_tracing: off

/**
 * Splits strings on newlines. Handles both Windows newlines (`\r\n`) and UNIX
 * newlines (`\n`).
 */
const splitLines = /*#__PURE__*/new _Transducer.Transducer( /*#__PURE__*/M.map_( /*#__PURE__*/Ref.makeManagedRef( /*#__PURE__*/Tuple.tuple( /*#__PURE__*/O.emptyOf(), false)), stateRef => O.fold(() => T.chain_(Ref.getAndSet_(stateRef, Tuple.tuple(O.emptyOf(), false)), state => O.fold_(Tuple.get_(state, 0), () => T.succeed(C.empty()), str => T.succeed(C.single(str)))), strings => Ref.modify_(stateRef, state => {
  const buffer = new Array();
  let inCRLF = Tuple.get_(state, 1);
  let carry = O.getOrElse_(Tuple.get_(state, 0), () => "");
  C.toArray(strings).forEach(str => {
    const concat = carry + str;

    if (concat.length > 0) {
      let i = // If we had a split CRLF, start reading from the last character of
      // the leftover (which was the `\r`)
      inCRLF && carry.length > 0 ? carry.length - 1 : // Otherwise we just skip over the entire previous leftover as it
      // does not contain a newline
      carry.length;
      let sliceStart = 0;

      while (i < concat.length) {
        if (concat[i] == "\n") {
          buffer.push(concat.substring(sliceStart, i));
          i += 1;
          sliceStart = i;
        } else if (concat[i] == "\r" && i + 1 < concat.length && concat[i + 1] == "\n") {
          buffer.push(concat.substring(sliceStart, i));
          i += 2;
          sliceStart = i;
        } else if (concat[i] == "\r" && i == concat.length - 1) {
          inCRLF = true;
          i += 1;
        } else {
          i += 1;
        }
      }

      carry = concat.substring(sliceStart, concat.length);
    }
  });
  return Tuple.tuple(C.from(buffer), Tuple.tuple(carry.length > 0 ? O.some(carry) : O.emptyOf(), inCRLF));
}))));
exports.splitLines = splitLines;
//# sourceMappingURL=splitLines.js.map