"use strict";

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

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("@effect/core/io/Exit/operations/fail"));

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

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

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/foldEffect"));

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

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

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

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

var _zipChunks = /*#__PURE__*/require("@effect/core/stream/Stream/operations/_internal/zipChunks");

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/zipAllWith.ts";

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

}

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

}

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. The provided functions will be
 * used to create elements for the composed stream.
 *
 * The functions `left` and `right` will be used if the streams have different
 * lengths and one of the streams has ended before the other.
 *
 * @tsplus fluent ets/Stream zipAllWith
 */


function zipAllWith_(self, that, left, right, both, __tsplusTrace) {
  return tsplus_module_1.combineChunks_(self, that, () => new PullBoth(), pull(left, right, both), __tsplusTrace);
}
/**
 * Zips this stream with another point-wise. The provided functions will be
 * used to create elements for the composed stream.
 *
 * The functions `left` and `right` will be used if the streams have different
 * lengths and one of the streams has ended before the other.
 *
 * @tsplus static ets/Stream/Aspects zipAllWith
 */


const zipAllWith = (that, left, right, both, __tsplusTrace) => self => zipAllWith_(self, that, left, right, both, __tsplusTrace);

exports.zipAllWith = zipAllWith;

function zipWithChunks(leftChunk, rightChunk, f, __tsplusTrace) {
  const {
    tuple: [out, either]
  } = (0, _zipChunks.zipChunks)(leftChunk, rightChunk, f);
  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(left, right, both) {
  return (state, pullLeft, pullRight, __tsplusTrace) => {
    switch (state._tag) {
      case "DrainLeft":
        {
          return tsplus_module_9.foldEffect_(pullLeft, err => tsplus_module_6.succeedNow(tsplus_module_5.fail(err), fileName_1 + ":97:37"), leftChunk => tsplus_module_6.succeedNow(tsplus_module_8.succeed(tsplus_module_3.make(tsplus_module_7.map_(leftChunk, left), new DrainLeft())), fileName_1 + ":98:43"), fileName_1 + ":96:35");
        }

      case "DrainRight":
        {
          return tsplus_module_9.foldEffect_(pullRight, err => tsplus_module_6.succeedNow(tsplus_module_5.fail(err), fileName_1 + ":103:37"), rightChunk => tsplus_module_6.succeedNow(tsplus_module_8.succeed(tsplus_module_3.make(tsplus_module_7.map_(rightChunk, right), new DrainRight())), fileName_1 + ":105:30"), fileName_1 + ":102:36");
        }

      case "PullBoth":
        {
          return tsplus_module_9.foldEffect_(tsplus_module_12.zipPar_(tsplus_module_11.unsome(pullLeft, fileName_1 + ":112:18"), () => tsplus_module_11.unsome(pullRight, fileName_1 + ":113:35"), fileName_1 + ":113:18"), err => tsplus_module_6.succeedNow(tsplus_module_5.fail(tsplus_module_10.some(err)), fileName_1 + ":115:39"), ({
            tuple: [l, r]
          }) => {
            if (l._tag === "Some" && r._tag === "Some") {
              const leftChunk = l.value;
              const rightChunk = r.value;

              if (tsplus_module_2.isEmpty(leftChunk) && tsplus_module_2.isEmpty(rightChunk)) {
                return pull(left, right, both)(new PullBoth(), pullLeft, pullRight, fileName_1 + ":121:49");
              } else if (tsplus_module_2.isEmpty(leftChunk)) {
                return pull(left, right, both)(new PullLeft(rightChunk), pullLeft, pullRight, fileName_1 + ":123:49");
              } else if (tsplus_module_2.isEmpty(rightChunk)) {
                return pull(left, right, both)(new PullRight(leftChunk), pullLeft, pullRight, fileName_1 + ":129:49");
              } else {
                return tsplus_module_6.succeedNow(tsplus_module_8.succeed(zipWithChunks(leftChunk, rightChunk, both, fileName_1 + ":136:47")), fileName_1 + ":135:43");
              }
            } else if (l._tag === "Some" && r._tag === "None") {
              return tsplus_module_6.succeedNow(tsplus_module_8.succeed(tsplus_module_3.make(tsplus_module_7.map_(l.value, left), new DrainLeft())), fileName_1 + ":140:41");
            } else if (l._tag === "None" && r._tag === "Some") {
              return tsplus_module_6.succeedNow(tsplus_module_8.succeed(tsplus_module_3.make(tsplus_module_7.map_(r.value, right), new DrainRight())), fileName_1 + ":144:41");
            } else {
              return tsplus_module_6.succeedNow(tsplus_module_5.fail(tsplus_module_10.none), fileName_1 + ":148:41");
            }
          }, fileName_1 + ":114:22");
        }

      case "PullLeft":
        {
          return tsplus_module_9.foldEffect_(pullLeft, option => tsplus_module_13.fold_(option, () => tsplus_module_6.succeedNow(tsplus_module_8.succeed(tsplus_module_3.make(tsplus_module_7.map_(state.rightChunk, right), new DrainRight())), fileName_1 + ":157:32"), err => tsplus_module_6.succeedNow(tsplus_module_5.fail(tsplus_module_10.some(err)), fileName_1 + ":161:34")), leftChunk => tsplus_module_2.isEmpty(leftChunk) ? pull(left, right, both)(new PullLeft(state.rightChunk), pullLeft, pullRight, fileName_1 + ":167:40") : tsplus_module_2.isEmpty(state.rightChunk) ? pull(left, right, both)(new PullRight(leftChunk), pullLeft, pullRight, fileName_1 + ":173:40") : tsplus_module_6.succeedNow(tsplus_module_8.succeed(zipWithChunks(leftChunk, state.rightChunk, both, fileName_1 + ":175:43")), fileName_1 + ":174:34"), fileName_1 + ":154:35");
        }

      case "PullRight":
        {
          return tsplus_module_9.foldEffect_(pullRight, option => tsplus_module_13.fold_(option, () => tsplus_module_6.succeedNow(tsplus_module_8.succeed(tsplus_module_3.make(tsplus_module_7.map_(state.leftChunk, left), new DrainLeft())), fileName_1 + ":183:32"), err => tsplus_module_6.succeedNow(tsplus_module_5.fail(tsplus_module_10.some(err)), fileName_1 + ":187:34")), rightChunk => tsplus_module_2.isEmpty(rightChunk) ? pull(left, right, both)(new PullRight(state.leftChunk), pullLeft, pullRight, fileName_1 + ":191:40") : tsplus_module_2.isEmpty(state.leftChunk) ? pull(left, right, both)(new PullLeft(rightChunk), pullLeft, pullRight, fileName_1 + ":197:40") : tsplus_module_6.succeedNow(tsplus_module_8.succeed(zipWithChunks(state.leftChunk, rightChunk, both, fileName_1 + ":199:43")), fileName_1 + ":198:34"), fileName_1 + ":180:36");
        }
    }
  };
}
//# sourceMappingURL=zipAllWith.js.map