"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.validateExec = exports.validateParN = exports.validatePar = exports.validate = exports.validateExec_ = exports.validateParN_ = exports.validatePar_ = exports.validate_ = void 0;
// ets_tracing: off
const Chunk = __importStar(require("../Collections/Immutable/Chunk"));
const E = __importStar(require("../Either"));
const absolve_1 = require("./absolve");
const either_1 = require("./either");
const excl_forEach_1 = require("./excl-forEach");
const map_1 = require("./map");
/**
 * Feeds elements of type `A` to `f` and accumulates all errors in error
 * channel or successes in success channel.
 *
 * This combinator is lossy meaning that if there are errors all successes
 * will be lost.
 */
function validate_(as, f, __trace) {
    return (0, absolve_1.absolve)((0, map_1.map_)((0, excl_forEach_1.forEach_)(as, (a) => (0, either_1.either)(f(a))), mergeExits()), __trace);
}
exports.validate_ = validate_;
/**
 * Feeds elements of type `A` to `f` and accumulates all errors in error
 * channel or successes in success channel.
 *
 * This combinator is lossy meaning that if there are errors all successes
 * will be lost.
 */
function validatePar_(as, f, __trace) {
    return (0, absolve_1.absolve)((0, map_1.map_)((0, excl_forEach_1.forEachPar_)(as, (a) => (0, either_1.either)(f(a))), mergeExits()), __trace);
}
exports.validatePar_ = validatePar_;
/**
 * Feeds elements of type `A` to `f` and accumulates all errors in error
 * channel or successes in success channel.
 *
 * This combinator is lossy meaning that if there are errors all successes
 * will be lost.
 */
function validateParN_(as, n, f, __trace) {
    return (0, absolve_1.absolve)((0, map_1.map_)((0, excl_forEach_1.forEachParN_)(as, n, (a) => (0, either_1.either)(f(a))), mergeExits()), __trace);
}
exports.validateParN_ = validateParN_;
function mergeExits() {
    return (exits) => {
        let errors = Chunk.empty();
        let results = Chunk.empty();
        for (const e of exits) {
            if (e._tag === "Left") {
                errors = Chunk.append_(errors, e.left);
            }
            else {
                results = Chunk.append_(results, e.right);
            }
        }
        if (!Chunk.isEmpty(errors)) {
            return E.left(errors);
        }
        else {
            return E.right(results);
        }
    };
}
/**
 * Feeds elements of type `A` to `f` and accumulates all errors in error
 * channel or successes in success channel.
 *
 * This combinator is lossy meaning that if there are errors all successes
 * will be lost.
 */
function validateExec_(as, es, f, __trace) {
    return (0, absolve_1.absolve)((0, map_1.map_)((0, excl_forEach_1.forEachExec_)(as, es, (a) => (0, either_1.either)(f(a))), mergeExits()), __trace);
}
exports.validateExec_ = validateExec_;
/**
 * Feeds elements of type `A` to `f` and accumulates all errors in error
 * channel or successes in success channel.
 *
 * This combinator is lossy meaning that if there are errors all successes
 * will be lost.
 *
 * @ets_data_first validate_
 */
function validate(f, __trace) {
    return (as) => validate_(as, f, __trace);
}
exports.validate = validate;
/**
 * Feeds elements of type `A` to `f` and accumulates all errors in error
 * channel or successes in success channel.
 *
 * This combinator is lossy meaning that if there are errors all successes
 * will be lost.
 *
 * @ets_data_first validatePar_
 */
function validatePar(f, __trace) {
    return (as) => validatePar_(as, f, __trace);
}
exports.validatePar = validatePar;
/**
 * Feeds elements of type `A` to `f` and accumulates all errors in error
 * channel or successes in success channel.
 *
 * This combinator is lossy meaning that if there are errors all successes
 * will be lost.
 *
 * @ets_data_first validateParN_
 */
function validateParN(n, f, __trace) {
    return (as) => validateParN_(as, n, f, __trace);
}
exports.validateParN = validateParN;
/**
 * Feeds elements of type `A` to `f` and accumulates all errors in error
 * channel or successes in success channel.
 *
 * This combinator is lossy meaning that if there are errors all successes
 * will be lost.
 *
 * @ets_data_first validateExec_
 */
function validateExec(es, f, __trace) {
    return (as) => validateExec_(as, es, f, __trace);
}
exports.validateExec = validateExec;
//# sourceMappingURL=validate.js.map