"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.filter = exports.filter_ = exports.first = exports.first_ = exports.GroupBy = void 0;
// ets_tracing: off
require("../../Operator");
const Function_1 = require("../../Function");
const Q = __importStar(require("../../Queue"));
const T = __importStar(require("../_internal/effect"));
const chainPar_1 = require("../Stream/chainPar");
const filterM_1 = require("../Stream/filterM");
const flattenExitOption_1 = require("../Stream/flattenExitOption");
const fromQueueWithShutdown_1 = require("../Stream/fromQueueWithShutdown");
const map_1 = require("../Stream/map");
const zipWithIndex_1 = require("../Stream/zipWithIndex");
/**
 * Representation of a grouped stream.
 * This allows to filter which groups will be processed.
 * Once merge is used all groups will be processed in parallel and the results will
 * be merged in arbitrary order.
 */
class GroupBy {
    constructor(grouped, buffer) {
        this.grouped = grouped;
        this.buffer = buffer;
        this.merge = this.merge.bind(this);
    }
    merge(f) {
        return ((0, chainPar_1.chainPar)(Number.MAX_SAFE_INTEGER, this.buffer)(({ tuple: [k, q] }) => f(k, (0, flattenExitOption_1.flattenExitOption)((0, fromQueueWithShutdown_1.fromQueueWithShutdown)(q))))(this.grouped));
    }
}
exports.GroupBy = GroupBy;
/**
 * Only consider the first n groups found in the stream.
 */
function first_(self, n) {
    const g1 = ((0, map_1.map)((_) => _.get(0))((0, filterM_1.filterM)((elem) => {
        const { tuple: [{ tuple: [, q] }, i] } = elem;
        if (i < n) {
            return T.as_(T.succeed(elem), true);
        }
        else {
            return T.as_(Q.shutdown(q), false);
        }
    })((0, zipWithIndex_1.zipWithIndex)(self.grouped))));
    return new GroupBy(g1, self.buffer);
}
exports.first_ = first_;
/**
 * Only consider the first n groups found in the stream.
 */
function first(n) {
    return (self) => first_(self, n);
}
exports.first = first;
/**
 * Filter the groups to be processed.
 */
function filter_(self, f) {
    const g1 = ((0, filterM_1.filterM)((elem) => {
        const { tuple: [k, q] } = elem;
        if (f(k)) {
            return T.as_(T.succeed(elem), true);
        }
        else {
            return T.as_(Q.shutdown(q), false);
        }
    })(self.grouped));
    return new GroupBy(g1, self.buffer);
}
exports.filter_ = filter_;
/**
 * Filter the groups to be processed.
 */
function filter(f) {
    return (self) => filter_(self, f);
}
exports.filter = filter;
//# sourceMappingURL=index.js.map