/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { isServer } from "lit";
const listSelector = "calcite-list";
const listItemGroupSelector = "calcite-list-item-group";
const listItemSelector = "calcite-list-item";
function expandedAncestors(el) {
  const ancestor = el.parentElement?.closest(listItemSelector);
  if (!ancestor) {
    return;
  }
  ancestor.open = true;
  expandedAncestors(ancestor);
}
function getListItemChildren(slotEl) {
  const assignedElements = slotEl.assignedElements({ flatten: true });
  const groupChildren = assignedElements.filter((el) => el?.matches(listItemGroupSelector)).map((group) => Array.from(group.querySelectorAll(listItemSelector))).flat();
  const listItemChildren = assignedElements.filter((el) => el?.matches(listItemSelector));
  const listChildren = assignedElements.filter((el) => el?.matches(listSelector));
  return {
    lists: listChildren,
    items: groupChildren.concat(listItemChildren)
  };
}
function updateListItemChildren(slotEl) {
  const listItemChildren = slotEl.assignedElements({ flatten: true }).filter((el) => el.matches(listItemSelector));
  const filteredListItemChildren = listItemChildren.filter((listItem) => !listItem.filterHidden);
  listItemChildren.forEach((listItem) => {
    const index = filteredListItemChildren.indexOf(listItem);
    listItem.setPosition = index === -1 ? void 0 : index + 1;
    listItem.setSize = index === -1 ? void 0 : filteredListItemChildren.length;
  });
}
function getDepth(element, includeGroup = false) {
  if (isServer) {
    return 0;
  }
  const expression = includeGroup ? "ancestor::calcite-list-item | ancestor::calcite-list-item-group" : "ancestor::calcite-list-item";
  const result = document.evaluate(expression, element, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  return result.snapshotLength;
}
function isListItem(element) {
  return element.tagName === "CALCITE-LIST-ITEM";
}
export {
  listSelector as a,
  listItemGroupSelector as b,
  getListItemChildren as c,
  expandedAncestors as e,
  getDepth as g,
  isListItem as i,
  listItemSelector as l,
  updateListItemChildren as u
};
