"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.zipWithChunks = void 0;
exports.zipWithChunks_ = zipWithChunks_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/combineChunks"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/isEmpty"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Either/fold"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/definition"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/fail"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeedNow"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/succeed"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/unsome"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/zipPar"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/foldEffect"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const fileName_1 = "(@effect/core) _src/stream/Stream/operations/zipWithChunks.ts";

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
 */


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
 */


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

exports.zipWithChunks = zipWithChunks;

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.js.map