/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { isServer } from "lit";
import { n as nodeListToArray } from "./dom.js";
import { A as AllComboboxChildrenSelector, a as ComboboxItemSelector, b as ComboboxItemGroupSelector } from "./resources7.js";
function getAncestors(element) {
  const parent = element.parentElement?.closest(AllComboboxChildrenSelector);
  const grandparent = parent?.parentElement?.closest(AllComboboxChildrenSelector);
  return [parent, grandparent].filter((el) => !!el);
}
function getItemAncestors(item) {
  return item.ancestors?.filter((el) => el.nodeName === "CALCITE-COMBOBOX-ITEM") || [];
}
function getItemChildren(item) {
  return nodeListToArray(item.querySelectorAll("calcite-combobox-item"));
}
function hasActiveChildren(node) {
  const items = nodeListToArray(node.querySelectorAll("calcite-combobox-item"));
  return items.filter((item) => item.selected).length > 0;
}
function getDepth(element) {
  if (isServer) {
    return 0;
  }
  const result = document.evaluate(
    "ancestor::calcite-combobox-item | ancestor::calcite-combobox-item-group",
    element,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null
  );
  const depth = result.snapshotLength;
  if (depth > 0 && element.nodeName === ComboboxItemSelector) {
    for (let i = 0; i < depth; i++) {
      const parent = result.snapshotItem(i);
      if (parent.nodeName === ComboboxItemGroupSelector) {
        return depth;
      }
    }
  } else if (element.nodeName === ComboboxItemGroupSelector) {
    return depth;
  }
  return depth + 1;
}
function isSingleLike(selectionMode) {
  return selectionMode.includes("single");
}
function getLabel(item) {
  return item.shortHeading || item.heading;
}
function orderByPrevious(items, previousItems) {
  if (items.length < 2 || previousItems.length === 0) {
    return items;
  }
  const previousItemOrder = new Map(previousItems.map((item, index) => [item, index]));
  return [...items].sort((a, b) => {
    const aOrder = previousItemOrder.get(a);
    const bOrder = previousItemOrder.get(b);
    if (aOrder !== void 0 && bOrder !== void 0) {
      return aOrder - bOrder;
    }
    if (aOrder !== void 0) {
      return -1;
    }
    if (bOrder !== void 0) {
      return 1;
    }
    return 0;
  });
}
function consumeValue(counts, value) {
  const count = counts.get(value) ?? 0;
  if (count === 0) {
    return false;
  }
  if (count === 1) {
    counts.delete(value);
  } else {
    counts.set(value, count - 1);
  }
  return true;
}
function orderValuesByPrevious(selectedValues, previousValues) {
  if (selectedValues.length < 2 || previousValues.length === 0) {
    return selectedValues;
  }
  const selectedValueCounts = /* @__PURE__ */ new Map();
  selectedValues.forEach((value) => {
    selectedValueCounts.set(value, (selectedValueCounts.get(value) ?? 0) + 1);
  });
  const orderedSelectedValues = previousValues.filter((value) => consumeValue(selectedValueCounts, value));
  if (orderedSelectedValues.length === 0) {
    return selectedValues;
  }
  selectedValues.forEach((value) => {
    if (consumeValue(selectedValueCounts, value)) {
      orderedSelectedValues.push(value);
    }
  });
  return orderedSelectedValues;
}
export {
  orderByPrevious as a,
  getItemAncestors as b,
  getItemChildren as c,
  getAncestors as d,
  getDepth as e,
  getLabel as g,
  hasActiveChildren as h,
  isSingleLike as i,
  orderValuesByPrevious as o
};
