"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitOnChunk = exports.splitOnChunk_ = void 0;
// ets_tracing: off
const CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const O = __importStar(require("../../../../Option"));
const CH = __importStar(require("../../Channel"));
const C = __importStar(require("../core"));
/**
 * Performs a filter and map in a single step.
 */
function splitOnChunk_(self, delimiter) {
    const next = (leftover, delimiterIndex) => CH.readWithCause((inputChunk) => {
        const buffer = CK.builder();
        const { tuple: [carry, delimiterCursor] } = CK.reduce_(inputChunk, Tp.tuple(O.getOrElse_(leftover, () => CK.empty()), delimiterIndex), ({ tuple: [carry, delimiterCursor] }, a) => {
            const concatenated = CK.append_(carry, a);
            if (delimiterCursor < CK.size(delimiter) &&
                a === CK.unsafeGet_(delimiter, delimiterCursor)) {
                if (delimiterCursor + 1 === CK.size(delimiter)) {
                    buffer.append(CK.take_(concatenated, CK.size(concatenated) - CK.size(delimiter)));
                    return Tp.tuple(CK.empty(), 0);
                }
                else {
                    return Tp.tuple(concatenated, delimiterCursor + 1);
                }
            }
            else {
                return Tp.tuple(concatenated, a === CK.unsafeGet_(delimiter, 0) ? 1 : 0);
            }
        });
        return CH.zipRight_(CH.write(buffer.build()), next(!CK.isEmpty(carry) ? O.some(carry) : O.none, delimiterCursor));
    }, (halt) => O.fold_(leftover, () => CH.failCause(halt), (chunk) => CH.zipRight_(CH.write(CK.single(chunk)), CH.failCause(halt))), (done) => O.fold_(leftover, () => CH.succeed(done), (chunk) => CH.zipRight_(CH.write(CK.single(chunk)), CH.succeed(done))));
    return new C.Stream(self.channel[">>>"](next(O.none, 0)));
}
exports.splitOnChunk_ = splitOnChunk_;
/**
 * Performs a filter and map in a single step.
 *
 * @ets_data_first splitOnChunk_
 */
function splitOnChunk(delimiter) {
    return (self) => splitOnChunk_(self, delimiter);
}
exports.splitOnChunk = splitOnChunk;
//# sourceMappingURL=splitOnChunk.js.map