"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const solver = require("./solver.js");
const DOMutils = require("./utils/DOMutils.js");
const allAffected = require("./utils/all-affected.js");
const autoFocus = require("./utils/auto-focus.js");
const getActiveElement = require("./utils/getActiveElement.js");
const is = require("./utils/is.js");
const parenting = require("./utils/parenting.js");
var reorderNodes = function(srcNodes, dstNodes) {
  var remap = /* @__PURE__ */ new Map();
  dstNodes.forEach(function(entity) {
    return remap.set(entity.node, entity);
  });
  return srcNodes.map(function(node) {
    return remap.get(node);
  }).filter(is.isDefined);
};
var getFocusMerge = function(topNode, lastNode) {
  var activeElement = document && getActiveElement.getActiveElement();
  var entries = allAffected.getAllAffectedNodes(topNode).filter(is.isNotAGuard);
  var commonParent = parenting.getTopCommonParent(activeElement || topNode, topNode, entries);
  var visibilityCache = /* @__PURE__ */ new Map();
  var anyFocusable = DOMutils.getAllTabbableNodes(entries, visibilityCache);
  var innerElements = DOMutils.getTabbableNodes(entries, visibilityCache).filter(function(_a) {
    var node = _a.node;
    return is.isNotAGuard(node);
  });
  if (!innerElements[0]) {
    innerElements = anyFocusable;
    if (!innerElements[0]) {
      return void 0;
    }
  }
  var outerNodes = DOMutils.getAllTabbableNodes([commonParent], visibilityCache).map(function(_a) {
    var node = _a.node;
    return node;
  });
  var orderedInnerElements = reorderNodes(outerNodes, innerElements);
  var innerNodes = orderedInnerElements.map(function(_a) {
    var node = _a.node;
    return node;
  });
  var newId = solver.newFocus(innerNodes, outerNodes, activeElement, lastNode);
  if (newId === solver.NEW_FOCUS) {
    return { node: autoFocus.pickAutofocus(anyFocusable, innerNodes, parenting.allParentAutofocusables(entries, visibilityCache)) };
  }
  if (newId === void 0) {
    return newId;
  }
  return orderedInnerElements[newId];
};
exports.getFocusMerge = getFocusMerge;
//# sourceMappingURL=focusMerge.js.map
