'use strict';

var DTR5TSDJ = require('../__chunks/DTR5TSDJ.cjs');
var _3YLGPPWQ = require('../__chunks/3YLGPPWQ.cjs');

// src/utils/focus.ts
var selector = "input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], button:not([disabled]), [tabindex], summary, iframe, object, embed, area[href], audio[controls], video[controls], [contenteditable]:not([contenteditable='false'])";
function isFocusable(element) {
  if (!element.matches(selector)) return false;
  if (!DTR5TSDJ.isVisible(element)) return false;
  if (element.closest("[inert]")) return false;
  return true;
}
function getClosestFocusable(element) {
  while (element && !isFocusable(element)) {
    element = element.closest(selector);
  }
  return element || null;
}
function hasFocus(element) {
  const activeElement = DTR5TSDJ.getActiveElement(element);
  if (!activeElement) return false;
  if (activeElement === element) return true;
  const activeDescendant = activeElement.getAttribute("aria-activedescendant");
  if (!activeDescendant) return false;
  return activeDescendant === element.id;
}
function hasFocusWithin(element) {
  const activeElement = DTR5TSDJ.getActiveElement(element);
  if (!activeElement) return false;
  if (DTR5TSDJ.contains(element, activeElement)) return true;
  const activeDescendant = activeElement.getAttribute("aria-activedescendant");
  if (!activeDescendant) return false;
  if (!("id" in element)) return false;
  if (activeDescendant === element.id) return true;
  return !!element.querySelector(`#${CSS.escape(activeDescendant)}`);
}
function focusIfNeeded(element) {
  if (!hasFocusWithin(element) && isFocusable(element)) {
    element.focus();
  }
}
function focusIntoView(element, options) {
  if (!("scrollIntoView" in element)) {
    element.focus();
  } else {
    element.focus({ preventScroll: true });
    element.scrollIntoView(_3YLGPPWQ.__spreadValues({ block: "nearest", inline: "nearest" }, options));
  }
}

exports.focusIfNeeded = focusIfNeeded;
exports.focusIntoView = focusIntoView;
exports.getClosestFocusable = getClosestFocusable;
exports.hasFocus = hasFocus;
exports.hasFocusWithin = hasFocusWithin;
exports.isFocusable = isFocusable;
//# sourceMappingURL=focus.cjs.map
