"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.zipWith = exports.zipWith_ = void 0;
// ets_tracing: off
const C = __importStar(require("../../Cause"));
const A = __importStar(require("../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../Collections/Immutable/Tuple"));
const E = __importStar(require("../../Either"));
const Ex = __importStar(require("../../Exit/api"));
const Function_1 = require("../../Function");
const O = __importStar(require("../../Option"));
const T = __importStar(require("../_internal/effect"));
const utils_1 = require("../_internal/utils");
const combineChunks_1 = require("./combineChunks");
/**
 * Zips this stream with another point-wise and applies the function to the paired elements.
 *
 * The new stream will end when one of the sides ends.
 *
 * By default pull is executed in parallel to preserve async semanthics, see `zipWithSeq` for
 * a sequential alternative
 */
function zipWith_(self, that, f) {
    const handleSuccess = (leftUpd, rightUpd, excess) => {
        const [leftExcess, rightExcess] = (E.fold_(excess, (l) => (0, Function_1.tuple)(l, A.empty()), (r) => (0, Function_1.tuple)(A.empty(), r)));
        const [left, right] = [
            (O.fold_(leftUpd, () => leftExcess, (upd) => A.concat_(leftExcess, upd))),
            (O.fold_(rightUpd, () => rightExcess, (upd) => A.concat_(rightExcess, upd)))
        ];
        const [emit, newExcess] = (0, utils_1.zipChunks_)(left, right, f);
        if (O.isSome(leftUpd) && O.isSome(rightUpd)) {
            return Ex.succeed(Tp.tuple(emit, {
                _tag: "Running",
                excess: newExcess
            }));
        }
        else if (O.isNone(leftUpd) && O.isNone(rightUpd)) {
            return Ex.fail(O.none);
        }
        else {
            return Ex.succeed(Tp.tuple(emit, (E.fold_(newExcess, (l) => !A.isEmpty(l)
                ? {
                    _tag: "LeftDone",
                    excessL: l
                }
                : { _tag: "End" }, (r) => !A.isEmpty(r)
                ? {
                    _tag: "RightDone",
                    excessR: r
                }
                : { _tag: "End" }))));
        }
    };
    return (0, combineChunks_1.combineChunks_)(self, that, {
        _tag: "Running",
        excess: E.left(A.empty())
    }, (st, p1, p2) => {
        switch (st._tag) {
            case "End": {
                return T.succeed(Ex.fail(O.none));
            }
            case "Running": {
                return (T.catchAllCause_(T.zipWithPar(T.optional(p2), (l, r) => handleSuccess(l, r, st.excess))(T.optional(p1)), (e) => T.succeed(Ex.halt((C.map(O.some)(e))))));
            }
            case "LeftDone": {
                return (T.catchAllCause_(T.map_(T.optional(p2), (r) => handleSuccess(O.none, r, E.left(st.excessL))), (e) => T.succeed(Ex.halt((C.map(O.some)(e))))));
            }
            case "RightDone": {
                return (T.catchAllCause_(T.map_(T.optional(p1), (l) => handleSuccess(l, O.none, E.right(st.excessR))), (e) => T.succeed(Ex.halt((C.map(O.some)(e))))));
            }
        }
    });
}
exports.zipWith_ = zipWith_;
/**
 * Zips this stream with another point-wise and applies the function to the paired elements.
 *
 * The new stream will end when one of the sides ends.
 *
 * By default pull is executed in parallel to preserve async semanthics, see `zipWithSeq` for
 * a sequential alternative
 */
function zipWith(that, f) {
    return (self) => zipWith_(self, that, f);
}
exports.zipWith = zipWith;
//# sourceMappingURL=zipWith.js.map