"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.joinFiberRefs = joinFiberRefs;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/empty"));

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

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

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

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/FiberRef/operations/diff"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/definition"));

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

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/reverse"));

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

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

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

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/ImmutableMap/set"));

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

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

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

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/FiberRef/operations/initital"));

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

var tsplus_module_18 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/structure/Equals"));

var tsplus_module_19 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/prepend"));

var _FiberRefsInternal = /*#__PURE__*/require("@effect/core/io/FiberRefs/operations/_internal/FiberRefsInternal");

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; }

function joinFiberRefs(self, that) {
  void 0;
  void 0;
  const parentFiberRefs = self.fiberRefLocals;
  const childFiberRefs = that.fiberRefLocals;
  const fiberRefLocals = tsplus_module_15.reduce_(tsplus_module_14.from(childFiberRefs), parentFiberRefs, (parentFiberRefs, {
    tuple: [fiberRef, childStack]
  }) => {
    const parentStack = tsplus_module_3.getOrElse_(tsplus_module_2.get_(parentFiberRefs, fiberRef), () => tsplus_module_1.empty());
    const values = combine(fiberRef, parentStack, childStack);
    const patches = tsplus_module_8.reverse(tsplus_module_7.reduce_(values.tail, tsplus_module_4.make(values.head, tsplus_module_1.empty()), ({
      tuple: [oldValue, patches]
    }, newValue) => tsplus_module_4.make(newValue, tsplus_module_6.cons(tsplus_module_5.diff_(fiberRef, oldValue, newValue), patches))).get(1));

    if (tsplus_module_6.isNil(patches)) {
      return parentFiberRefs;
    }

    let newStack;

    if (tsplus_module_6.isNil(parentStack)) {
      newStack = tsplus_module_9.none;
    } else {
      const patch = tsplus_module_7.reduce_(patches.tail, patches.head, (a, b) => tsplus_module_10.combine_(fiberRef, a, b));
      const {
        tuple: [fiberId, oldValue]
      } = parentStack.head;
      const tail = parentStack.tail;
      newStack = tsplus_module_9.some(tsplus_module_6.cons(tsplus_module_4.make(fiberId, tsplus_module_11.patch_(fiberRef, patch)(oldValue)), tail));
    }

    return tsplus_module_13.fold_(newStack, () => parentFiberRefs, stack => tsplus_module_12.set_(parentFiberRefs, fiberRef, stack));
  });
  return new _FiberRefsInternal.FiberRefsInternal(fiberRefLocals);
}

function combine(fiberRef, parentStack, childStack) {
  return combineLoop(tsplus_module_8.reverse(parentStack), tsplus_module_8.reverse(childStack), tsplus_module_16.initial(fiberRef), tsplus_module_16.initial(fiberRef));
}
/**
 * @tsplus tailRec
 */


function combineLoop(parentStack, childStack, lastParentValue, lastChildValue) {
  var parentStack_1 = parentStack,
      childStack_1 = childStack,
      lastParentValue_1 = lastParentValue,
      lastChildValue_1 = lastChildValue;
  var parentStack_2 = parentStack,
      childStack_2 = childStack,
      lastParentValue_2 = lastParentValue,
      lastChildValue_2 = lastChildValue;

  while (1) {
    if (tsplus_module_6.isNil(parentStack_1) || tsplus_module_6.isNil(childStack_1)) {
      return tsplus_module_6.cons(lastChildValue_1, tsplus_module_17.map_(childStack_1, tuple => tuple.get(1)));
    }

    const {
      tuple: [parentId, parentValue]
    } = parentStack_1.head;
    const parentTail = parentStack_1.tail;
    const {
      tuple: [childId, childValue]
    } = childStack_1.head;
    const childTail = childStack_1.tail;

    if (tsplus_module_18.equals(parentId, childId)) {
      parentStack_2 = parentTail;
      childStack_2 = childTail;
      lastParentValue_2 = parentValue;
      lastChildValue_2 = childValue;
      parentStack_1 = parentStack_2;
      childStack_1 = childStack_2;
      lastParentValue_1 = lastParentValue_2;
      lastChildValue_1 = lastChildValue_2;
      continue;
    }

    if (parentId.id < childId.id) {
      return tsplus_module_19.prepend_(tsplus_module_19.prepend_(tsplus_module_19.prepend_(tsplus_module_17.map_(childStack_1, tuple => tuple.get(1)), childValue), lastChildValue_1), lastParentValue_1);
    }

    return tsplus_module_19.prepend_(tsplus_module_19.prepend_(tsplus_module_17.map_(childStack_1, tuple => tuple.get(1)), childValue), lastChildValue_1);
  }
}
//# sourceMappingURL=join.js.map