/* skip-links-container.tsx generated by @compiled/babel-plugin v0.38.1 */
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "./skip-links-container.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
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; }
import { useMemo } from 'react';
import { Text } from '@atlaskit/primitives/compiled';
import { SkipLink } from './skip-link';
var styles = {
  root: "_zulp1b66 _2rko12b0 _1rjcutpp _18zrutpp _1e0c1txw _2lx21bp4 _kqsw1n9t _1e02v47k _152tv47k _bfhk1bhr _16qs1cd0 _1pby1mrw _tzy4idpf _lcxvglyw _1digjh3g _1mygkb7n _18eu1wug",
  skipLinkList: "_zulp1b66 _1e0c1txw _2lx21bp4 _1x8w11lx _2mzuglyw _1pfhze3t _bozgze3t"
};

/**
 * Keydown handler to hide the skip links when escape is pressed.
 *
 * TODO: consider removing this, it doesn't make a lot of sense.
 * The skip links look like a popup but in reality they aren't.
 * Even though we hide them when the user presses Escape, when
 * they press TAB it will reopen the list and move to the next one...
 *
 * One potential option is to use a tree walker to focus on the
 * next focusable element in the DOM (outside the skip links
 * container).
 */
var closeOnEscape = function closeOnEscape(event) {
  if (event.key !== 'Escape') {
    return;
  }
  var activeElement = document.activeElement;
  if (activeElement instanceof HTMLElement) {
    activeElement.blur();
  }
};
var _assignIndex = function assignIndex(num, arr) {
  if (!arr.includes(num)) {
    return num;
  }
  return _assignIndex(num + 1, arr);
};

/**
 * Sorts an array of skip links by list indexes.
 *
 * Skip links with custom list indexes are positioned first, followed by regular skip links,
 * which are automatically assigned available index positions to avoid conflicts.
 */
function sortSkipLinks(arr) {
  var customLinks = arr.filter(function (link) {
    return Number.isInteger(link.listIndex);
  });
  if (customLinks.length === 0) {
    return arr;
  }
  var usedIndexes = customLinks.map(function (a) {
    return a.listIndex;
  });
  var regularLinksWithIndex = arr.filter(function (link) {
    return link.listIndex === undefined;
  }).map(function (link, index) {
    var listIndex = _assignIndex(index, usedIndexes);
    usedIndexes.push(listIndex);
    return _objectSpread(_objectSpread({}, link), {}, {
      listIndex: listIndex
    });
  });
  return [].concat(_toConsumableArray(customLinks), _toConsumableArray(regularLinksWithIndex)).sort(function (a, b) {
    return a.listIndex - b.listIndex;
  });
}
var isOnlyWhitespaceRegex = /^\s*$/;

/**
 * A container element for the skip links.
 *
 * The container element is always rendered in the DOM, but is only visible when
 * focus is within the container.
 *
 * The label is used as the heading of the skip links container. If the provided label is a string
 * comprised only of only whitespace (e.g. '' or ' '), the skip link heading element will be removed.
 *
 * The links prop is only used when the feature flag is enabled.
 */
export function SkipLinksContainer(_ref) {
  var label = _ref.label,
    testId = _ref.testId,
    links = _ref.links;
  var sortedLinks = useMemo(function () {
    return sortSkipLinks(links);
  }, [links]);
  if (sortedLinks.length === 0) {
    return null;
  }
  var isEmptyLabel = isOnlyWhitespaceRegex.test(label);
  return /*#__PURE__*/React.createElement("div", {
    onKeyDown: closeOnEscape,
    "data-testid": testId ? "".concat(testId, "--skip-links-container") : undefined,
    className: ax([styles.root])
  }, !isEmptyLabel && /*#__PURE__*/React.createElement(Text, {
    weight: "bold",
    testId: testId ? "".concat(testId, "--skip-links-container--label") : undefined
  }, label), /*#__PURE__*/React.createElement("ol", {
    className: ax([styles.skipLinkList])
  }, sortedLinks.map(function (_ref2) {
    var id = _ref2.id,
      label = _ref2.label,
      onBeforeNavigate = _ref2.onBeforeNavigate;
    return /*#__PURE__*/React.createElement(SkipLink, {
      key: id,
      id: id,
      onBeforeNavigate: onBeforeNavigate
    }, label);
  })));
}