"use strict";

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

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

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

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Schedule/Decision"));

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

var _makeWithState = /*#__PURE__*/require("@effect/core/io/Schedule/operations/_internal/makeWithState");

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/io/Schedule/operations/unionWith.ts";

/**
 * Returns a new schedule that combines this schedule with the specified
 * schedule, continuing as long as either schedule wants to continue and
 * merging the next intervals according to the specified merge function.
 *
 * @tsplus fluent ets/Schedule unionWith
 * @tsplus fluent ets/Schedule/WithState unionWith
 */
function unionWith_(self, that, f) {
  return (0, _makeWithState.makeWithState)(tsplus_module_1.make(self._initial, that._initial), (now, input, state) => {
    const left = self._step(now, input, state.get(0));

    const right = that._step(now, input, state.get(1));

    return tsplus_module_4.zipWith_(left, () => right, ({
      tuple: [lState, l, lDecision]
    }, {
      tuple: [rState, r, rDecision]
    }) => {
      if (lDecision._tag === "Done" && rDecision._tag === "Done") {
        return tsplus_module_1.make(tsplus_module_1.make(lState, rState), tsplus_module_2.mergeTuple(l, r), tsplus_module_3.done);
      }

      if (lDecision._tag === "Done" && rDecision._tag === "Continue") {
        return tsplus_module_1.make(tsplus_module_1.make(lState, rState), tsplus_module_2.mergeTuple(l, r), tsplus_module_3._continue(rDecision.interval));
      }

      if (lDecision._tag === "Continue" && rDecision._tag === "Done") {
        return tsplus_module_1.make(tsplus_module_1.make(lState, rState), tsplus_module_2.mergeTuple(l, r), tsplus_module_3._continue(lDecision.interval));
      }

      if (lDecision._tag === "Continue" && rDecision._tag === "Continue") {
        const combined = f(lDecision.interval, rDecision.interval);
        return tsplus_module_1.make(tsplus_module_1.make(lState, rState), tsplus_module_2.mergeTuple(l, r), tsplus_module_3._continue(combined));
      }

      throw new Error("bug");
    }, fileName_1 + ":27:24");
  });
}
/**
 * Returns a new schedule that combines this schedule with the specified
 * schedule, continuing as long as either schedule wants to continue and
 * merging the next intervals according to the specified merge function.
 *
 * @tsplus static ets/Schedule/Aspects unionWith
 */


const unionWith = (that, f) => self => unionWith_(self, that, f);

exports.unionWith = unionWith;
//# sourceMappingURL=unionWith.js.map