import * as tsplus_module_1 from "@tsplus/stdlib/collections/List/empty";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/ImmutableMap/get";
import * as tsplus_module_3 from "@tsplus/stdlib/data/Maybe/getOrElse";
import * as tsplus_module_4 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_5 from "@effect/core/io/FiberRef/operations/diff";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/List/definition";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/List/reduce";
import * as tsplus_module_8 from "@tsplus/stdlib/collections/List/reverse";
import * as tsplus_module_9 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_10 from "@effect/core/io/FiberRef/operations/combine";
import * as tsplus_module_11 from "@effect/core/io/FiberRef/operations/patch";
import * as tsplus_module_12 from "@tsplus/stdlib/collections/ImmutableMap/set";
import * as tsplus_module_13 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_14 from "@tsplus/stdlib/collections/Chunk/definition";
import * as tsplus_module_15 from "@tsplus/stdlib/collections/Chunk/reduce";
import * as tsplus_module_16 from "@effect/core/io/FiberRef/operations/initital";
import * as tsplus_module_17 from "@tsplus/stdlib/collections/List/map";
import * as tsplus_module_18 from "@tsplus/stdlib/structure/Equals";
import * as tsplus_module_19 from "@tsplus/stdlib/collections/List/prepend";
import { concreteFiberRefs, FiberRefsInternal } from "@effect/core/io/FiberRefs/operations/_internal/FiberRefsInternal";
export 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(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.mjs.map