var _defineProperty = require("@babel/runtime/helpers/defineProperty");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
// This is a copy of 'react-dom-instance' library - https://www.npmjs.com/package/react-dom-instance
// The library is dead, so the only way to push the changes is by copying it
var optionsDefault = {
  maxIteration: 4
};
function findInstance(node, opts) {
  var options = _objectSpread(_objectSpread({}, optionsDefault), opts);
  var fiber = getFiberFromNode(node);
  if (!fiber) return false;
  var instance = getInstanceFromFiber(fiber, options.maxIteration);
  if (!instance) return false;
  var target = getTargetInstance(instance, instance, options.componentName, options.maxIteration);
  return target;
}
function getFiberFromNode(node) {
  var key = getFiberKey(node);
  if (!key) {
    node = node.children[0];
    key = node && getFiberKey(node);
  }
  return key ? node[key] : false;
}
function getFiberKey(node) {
  return Object.keys(node).find(key => key.startsWith('__reactInternalInstance$') || key.startsWith('__reactFiber$'));
}
function getInstanceFromFiber(fiber, i) {
  var f = fiber;
  while (!isInstanceFiber(f) && i-- > 0) {
    f = f && f.return;
  }
  return isInstanceFiber(f) ? f.stateNode : false;
}
function isInstanceFiber(fiber) {
  return fiber && fiber.type && typeof fiber.type !== 'string' && fiber.stateNode;
}
function getTargetInstance(childInstance, parentInstance, componentName, i) {
  if (!childInstance && !parentInstance) return false;
  if (childInstance && isTarget(childInstance, componentName)) {
    return childInstance;
  }
  if (parentInstance && childInstance !== parentInstance && isTarget(parentInstance, componentName)) {
    return parentInstance;
  }
  if (i <= 0) {
    warn('maxIteration exceeded and not ' + componentName + ' instance found.');
    return false;
  }
  var childFiber = childInstance && childInstance._reactInternalFiber.child;
  var parentFiber = parentInstance && parentInstance._reactInternalFiber.return;
  return getTargetInstance(isInstanceFiber(childFiber) && childFiber.stateNode, isInstanceFiber(parentFiber) && parentFiber.stateNode, componentName, i - 1);
}
function isTarget(instance, componentName) {
  if (!componentName) return true;
  return instance.constructor.name === componentName;
}
function warn(msg) {
  typeof console !== undefined && console.warn('react-dom-instance: ' + msg);
}
module.exports.findInstance = findInstance;
module.exports = {
  findInstance
};
//# sourceMappingURL=findInstance.js.map