const fileName_1 = "(@effect/core) _src/stream/Stream/operations/zipWithChunks.ts";
import * as tsplus_module_1 from "@effect/core/stream/Stream/operations/combineChunks";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/isEmpty";
import * as tsplus_module_3 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_4 from "@tsplus/stdlib/data/Either/fold";
import * as tsplus_module_5 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_6 from "@effect/core/io/Exit/operations/fail";
import * as tsplus_module_7 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_8 from "@effect/core/io/Exit/operations/succeed";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/unsome";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/zipPar";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/foldEffect";

class PullBoth {
  constructor() {
    this._tag = "PullBoth";
  }

}

class PullLeft {
  constructor(rightChunk) {
    this.rightChunk = rightChunk;
    this._tag = "PullLeft";
  }

}

class PullRight {
  constructor(leftChunk) {
    this.leftChunk = leftChunk;
    this._tag = "PullRight";
  }

}
/**
 * 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.
 *
 * @tsplus fluent ets/Stream zipWithChunks
 */


export function zipWithChunks_(self, that, f, __tsplusTrace) {
  return tsplus_module_1.combineChunks_(self, that, () => new PullBoth(), pull(f), __tsplusTrace);
}
/**
 * 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.
 *
 * @tsplus static ets/Stream/Aspects zipWithChunks
 */

export const zipWithChunks = (that, f, __tsplusTrace) => self => zipWithChunks_(self, that, f, __tsplusTrace);

function zipWithChunksInternal(leftChunk, rightChunk, f, __tsplusTrace) {
  const {
    tuple: [out, either]
  } = f(leftChunk, rightChunk);
  return tsplus_module_4.fold_(either, leftChunk => tsplus_module_2.isEmpty(leftChunk) ? tsplus_module_3.make(out, new PullBoth()) : tsplus_module_3.make(out, new PullRight(leftChunk)), rightChunk => tsplus_module_2.isEmpty(rightChunk) ? tsplus_module_3.make(out, new PullBoth()) : tsplus_module_3.make(out, new PullLeft(rightChunk)));
}

function pull(f) {
  return (state, pullLeft, pullRight, __tsplusTrace) => {
    switch (state._tag) {
      case "PullBoth":
        {
          return tsplus_module_11.foldEffect_(tsplus_module_10.zipPar_(tsplus_module_9.unsome(pullLeft, fileName_1 + ":85:31"), () => tsplus_module_9.unsome(pullRight, fileName_1 + ":86:35"), fileName_1 + ":86:18"), err => tsplus_module_7.succeedNow(tsplus_module_6.fail(tsplus_module_5.some(err)), fileName_1 + ":88:39"), ({
            tuple: [left, right]
          }) => {
            if (tsplus_module_5.isSome(left) && tsplus_module_5.isSome(right)) {
              const leftChunk = left.value;
              const rightChunk = right.value;

              if (tsplus_module_2.isEmpty(leftChunk) && tsplus_module_2.isEmpty(rightChunk)) {
                return pull(f)(new PullBoth(), pullLeft, pullRight, fileName_1 + ":94:33");
              } else if (tsplus_module_2.isEmpty(leftChunk)) {
                return pull(f)(new PullLeft(rightChunk), pullLeft, pullRight, fileName_1 + ":96:33");
              } else if (tsplus_module_2.isEmpty(rightChunk)) {
                return pull(f)(new PullRight(leftChunk), pullLeft, pullRight, fileName_1 + ":98:33");
              } else {
                return tsplus_module_7.succeedNow(tsplus_module_8.succeed(zipWithChunksInternal(leftChunk, rightChunk, f, fileName_1 + ":101:55")), fileName_1 + ":100:43");
              }
            }

            return tsplus_module_7.succeedNow(tsplus_module_6.fail(tsplus_module_5.none), fileName_1 + ":105:39");
          }, fileName_1 + ":87:22");
        }

      case "PullLeft":
        {
          return tsplus_module_11.foldEffect_(pullLeft, err => tsplus_module_7.succeedNow(tsplus_module_6.fail(err), fileName_1 + ":111:37"), leftChunk => tsplus_module_2.isEmpty(leftChunk) ? pull(f)(new PullLeft(state.rightChunk), pullLeft, pullRight, fileName_1 + ":114:24") : tsplus_module_2.isEmpty(state.rightChunk) ? pull(f)(new PullRight(leftChunk), pullLeft, pullRight, fileName_1 + ":116:24") : tsplus_module_7.succeedNow(tsplus_module_8.succeed(zipWithChunksInternal(leftChunk, state.rightChunk, f, fileName_1 + ":118:51")), fileName_1 + ":117:34"), fileName_1 + ":110:35");
        }

      case "PullRight":
        {
          return tsplus_module_11.foldEffect_(pullRight, err => tsplus_module_7.succeedNow(tsplus_module_6.fail(err), fileName_1 + ":124:37"), rightChunk => tsplus_module_2.isEmpty(rightChunk) ? pull(f)(new PullRight(state.leftChunk), pullLeft, pullRight, fileName_1 + ":127:24") : tsplus_module_2.isEmpty(state.leftChunk) ? pull(f)(new PullLeft(rightChunk), pullLeft, pullRight, fileName_1 + ":129:24") : tsplus_module_7.succeedNow(tsplus_module_8.succeed(zipWithChunksInternal(state.leftChunk, rightChunk, f, fileName_1 + ":131:51")), fileName_1 + ":130:34"), fileName_1 + ":123:36");
        }
    }
  };
}
//# sourceMappingURL=zipWithChunks.mjs.map