"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.rechunk = exports.rechunk_ = void 0;
// ets_tracing: off
const CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const L = __importStar(require("../../../../Collections/Immutable/List"));
const T = __importStar(require("../../../../Effect"));
const CH = __importStar(require("../../Channel"));
const C = __importStar(require("../core"));
const Unwrap = __importStar(require("./unwrap"));
class Rechunker {
    constructor(n) {
        this.n = n;
        this.builder = CK.builder();
        this.pos = 0;
    }
    write(elem) {
        this.builder.append(elem);
        this.pos += 1;
        if (this.pos === this.n) {
            const result = this.builder.build();
            this.builder = CK.builder();
            this.pos = 0;
            return result;
        }
        return null;
    }
    emitOfNotEmpty() {
        if (this.pos !== 0) {
            return CH.write(this.builder.build());
        }
        else {
            return CH.unit;
        }
    }
}
/**
 * Re-chunks the elements of the stream into chunks of
 * `n` elements each.
 * The last chunk might contain less than `n` elements
 */
function rechunk_(self, n) {
    return Unwrap.unwrap(T.succeedWith(() => {
        const rechunker = new Rechunker(n);
        const process = CH.readWithCause((chunk) => {
            const chunkSize = CK.size(chunk);
            if (chunkSize > 0) {
                let chunks = L.empty();
                let result = null;
                let i = 0;
                while (i < chunkSize) {
                    while (i < chunkSize && result === null) {
                        result = rechunker.write(CK.unsafeGet_(chunk, i));
                        i += 1;
                    }
                    if (result !== null) {
                        chunks = L.prepend_(chunks, result);
                        result = null;
                    }
                }
                return CH.zipRight_(CH.writeAll(...L.toArray(L.reverse(chunks))), process);
            }
            return process;
        }, (cause) => CH.zipRight_(rechunker.emitOfNotEmpty(), CH.failCause(cause)), (_) => rechunker.emitOfNotEmpty());
        return new C.Stream(self.channel[">>>"](process));
    }));
}
exports.rechunk_ = rechunk_;
/**
 * Re-chunks the elements of the stream into chunks of
 * `n` elements each.
 * The last chunk might contain less than `n` elements
 *
 * @ets_data_first rechunk_
 */
function rechunk(n) {
    return (self) => rechunk_(self, n);
}
exports.rechunk = rechunk;
//# sourceMappingURL=rechunk.js.map