const fileName_1 = "(@effect/core) _src/stream/Stream/operations/mergeWith.ts";
import * as tsplus_module_1 from "@effect/core/stream/Stream/TerminationStrategy";
import * as tsplus_module_2 from "@effect/core/stream/Stream/operations/map";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/mergeWith";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/succeed";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/flatMap";
import * as tsplus_module_6 from "@effect/core/io/Exit/operations/isSuccess";
import * as tsplus_module_7 from "@effect/core/io/Effect/operations/done";
import * as tsplus_module_8 from "@effect/core/stream/Channel/MergeDecision";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Merges this stream and the specified stream together to a common element
 * type with the specified mapping functions.
 *
 * New produced stream will terminate when both specified stream terminate if
 * no termination strategy is specified.
 *
 * @tsplus fluent ets/Stream mergeWith
 */

export function mergeWith_(self, that, left, right, strategy = () => tsplus_module_1.both, __tsplusTrace) {
  return new StreamInternal(tsplus_module_5.flatMap_(tsplus_module_4.succeed(strategy), strategy => {
    const leftStream = tsplus_module_2.map_(self, left, __tsplusTrace);
    const rightStream = tsplus_module_2.map_(that(), right, __tsplusTrace);
    void 0;
    void 0;
    return tsplus_module_3.mergeWith_(leftStream.channel, () => rightStream.channel, handler(strategy._tag === "Either" || strategy._tag === "Left"), handler(strategy._tag === "Either" || strategy._tag === "Right"));
  }));
}
/**
 * Merges this stream and the specified stream together to a common element
 * type with the specified mapping functions.
 *
 * New produced stream will terminate when both specified stream terminate if
 * no termination strategy is specified.
 *
 * @tsplus static ets/Stream/Aspects mergeWith
 */

export const mergeWith = (that, left, right, strategy = () => TerminationStrategy.Both, __tsplusTrace) => self => mergeWith_(self, that, left, right, strategy, __tsplusTrace);

function handler(terminate) {
  return (exit, __tsplusTrace) => {
    return terminate || !tsplus_module_6.isSuccess(exit) ? tsplus_module_8.done(tsplus_module_7.done(() => exit, fileName_1 + ":54:39")) : tsplus_module_8._await(exit => tsplus_module_7.done(() => exit, fileName_1 + ":55:50"));
  };
}
//# sourceMappingURL=mergeWith.mjs.map