"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.foldWeightedDecomposeEffect = void 0;
// ets_tracing: off
const CK = __importStar(require("../../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../../Effect"));
const Function_1 = require("../../../Function");
const CH = __importStar(require("../Channel"));
const C = __importStar(require("./core"));
/**
 * Creates a sink that effectfully folds elements of type `In` into a structure
 * of type `S`, until `max` worth of elements (determined by the `costFn`) have
 * been folded.
 *
 * The `decompose` function will be used for decomposing elements that
 * cause an `S` aggregate to cross `max` into smaller elements. Be vigilant with
 * this function, it has to generate "simpler" values or the fold may never end.
 * A value is considered indivisible if `decompose` yields the empty chunk or a
 * single-valued chunk. In these cases, there is no other choice than to yield
 * a value that will cross the threshold.
 *
 * See `foldWeightedDecompose` for an example.
 */
function foldWeightedDecomposeEffect(z, costFn, max, decompose, f) {
    const go = (s, cost, dirty) => CH.readWith((in_) => {
        const fold = (in_, s, dirty, cost, idx) => {
            if (idx === CK.size(in_)) {
                return T.succeed(Tp.tuple(s, cost, dirty, CK.empty()));
            }
            else {
                const elem = CK.unsafeGet_(in_, idx);
                return (T.chain_(T.map_(costFn(s, elem), (_) => cost + _), (total) => {
                    if (total <= max) {
                        return T.chain_(f(s, elem), (_) => fold(in_, _, true, total, idx + 1));
                    }
                    else {
                        return T.chain_(decompose(elem), (decomposed) => {
                            if (CK.size(decomposed) <= 1 && !dirty) {
                                return T.map_(f(s, elem), (_) => Tp.tuple(_, total, true, CK.drop_(in_, idx + 1)));
                            }
                            else if (CK.size(decomposed) <= 1 && dirty) {
                                return T.succeed(Tp.tuple(s, cost, dirty, CK.drop_(in_, idx)));
                            }
                            else {
                                return fold(CK.concat_(decomposed, CK.drop_(in_, idx + 1)), s, dirty, cost, 0);
                            }
                        });
                    }
                }));
            }
        };
        return (CH.chain_(CH.fromEffect(fold(in_, s, dirty, cost, 0)), ({ tuple: [nextS, nextCost, nextDirty, leftovers] }) => {
            if (!CK.isEmpty(leftovers)) {
                return CH.zipRight_(CH.write(leftovers), CH.end(nextS));
            }
            else if (cost > max) {
                return CH.end(nextS);
            }
            else {
                return go(nextS, nextCost, nextDirty);
            }
        }));
    }, (err) => CH.fail(err), (_) => CH.end(s));
    return new C.Sink(go(z, 0, false));
}
exports.foldWeightedDecomposeEffect = foldWeightedDecomposeEffect;
//# sourceMappingURL=foldWeightedDecomposeEffect.js.map