"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.focusOnLastDescendent = exports.focusOnFirstDescendent = void 0;
exports.isHTMLElement = isHTMLElement;

function attemptFocus(node) {
  try {
    node.focus();
  } catch (exception) {
    return false;
  }

  return document.activeElement === node;
}

function isElement(node) {
  return (node === null || node === void 0 ? void 0 : node.children) !== undefined;
}

function isHTMLElement(node) {
  return (node === null || node === void 0 ? void 0 : node.focus) !== undefined;
}

var focusOnFirstDescendent = function focusOnFirstDescendent(node) {
  if (isHTMLElement(node) && attemptFocus(node)) {
    return true;
  }

  return isElement(node) && Array.from(node.children).some(function (childNode) {
    return focusOnFirstDescendent(childNode);
  });
};

exports.focusOnFirstDescendent = focusOnFirstDescendent;

var focusOnLastDescendent = function focusOnLastDescendent(node) {
  if (isHTMLElement(node) && attemptFocus(node)) {
    return true;
  }

  return isElement(node) && Array.from(node.children).reverse().some(function (childNode) {
    return focusOnLastDescendent(childNode);
  });
};

exports.focusOnLastDescendent = focusOnLastDescendent;