/* eslint-disable no-console */
// Monkey-patches Node.prototype.removeChild and insertBefore to prevent
// crashes caused by external DOM mutations conflicting with React's virtual
// DOM reconciliation.
//
// Problem:
// React maintains references to DOM nodes in its virtual DOM. When external
// libraries or browser features modify or remove those nodes before React's
// own cleanup runs, React calls removeChild/insertBefore on nodes that no
// longer exist in the expected parent, causing a crash:
//   - WebKit/iOS: "NotFoundError: The object can not be found here"
//   - Blink: "Failed to execute 'removeChild' on 'Node'"
//
// Known triggers:
// 1. @tippyjs/react portals — @tippyjs/react uses a hybrid DOM model:
//    React renders content into a container via createPortal(), but
//    tippy.js owns the container element and manages its attachment to
//    document.body. On unmount (e.g., route navigation via history.push),
//    tippy.js's destroy() removes the container from the DOM via
//    removeChild(), while React's reconciler is also trying to clean up
//    the portal children inside that same container. If tippy.js removes
//    the container first, React's subsequent removeChild calls fail
//    because the nodes are no longer under the expected parent.
// 2. Browser translation — Google Translate replaces text nodes with <font>
//    elements. React still holds references to the original text nodes and
//    crashes when trying to remove them.
//
// What this patch does:
// Before calling removeChild/insertBefore, it checks if the node actually
// belongs to the expected parent. If not, it logs a console.error and
// returns early instead of crashing the app.
//
// Side effect:
// If application code has a legitimate bug where removeChild is called on
// the wrong parent, this patch will log an error instead of throwing. The
// bug will still be visible in the console but won't crash the page. In
// practice this is rare — removeChild is called by React's internals, not
// application code, and the only time the parent mismatch occurs is when
// something external has moved or removed the node.
//
// Recommended by Dan Abramov (React core team):
// https://github.com/facebook/react/issues/11538#issuecomment-417504600

function patchDomForExternalMutations() {
  if (typeof Node !== "function" || !Node.prototype) return;
  if (Node.prototype.__neetoPatched) return;
  Object.defineProperty(Node.prototype, "__neetoPatched", {
    value: true,
    enumerable: false,
    configurable: false,
    writable: false
  });
  var originalRemoveChild = Node.prototype.removeChild;
  Node.prototype.removeChild = function (child) {
    if (child.parentNode !== this) {
      if (console) {
        console.error("Cannot remove a child from a different parent", child, this);
      }
      return child;
    }
    return originalRemoveChild.apply(this, arguments);
  };
  var originalInsertBefore = Node.prototype.insertBefore;
  Node.prototype.insertBefore = function (newNode, referenceNode) {
    if (referenceNode && referenceNode.parentNode !== this) {
      if (console) {
        console.error("Cannot insert before a reference node from a different parent", referenceNode, this);
      }
      return newNode;
    }
    return originalInsertBefore.apply(this, arguments);
  };
}

export { patchDomForExternalMutations as default };
//# sourceMappingURL=domPatch.js.map
