// import { CSS_UTILITY } from './resources';
// import { guid } from './guid';
/** Determines if element contains text */
export const hasText = (element) => element.textContent.trim() !== '';
// export const getThemeName = (element: HTMLElement): Theme =>
//   closestElementCrossShadowBoundary(element, `.${CSS_UTILITY.darkTheme}`) ? 'dark' : 'light';
export const getElementDir = (element) => { var _a; return ((_a = closestElementCrossShadowBoundary(element, '[dir]')) === null || _a === void 0 ? void 0 : _a.getAttribute('dir')) || 'ltr'; };
// export const getElementProp = (element: Element, prop: string, fallbackValue: any): any => {
//   const closest = element.closest(`[${prop}]`);
//   return closest ? closest.getAttribute(prop) : fallbackValue;
// };
export const getRootNode = (element) => element.getRootNode();
export const getHost = (root) => root.host || null;
// // Queries an element's rootNode and any ancestor rootNodes.
// // based on https://stackoverflow.com/q/54520554/194216
// export const queryElementsRoots = <T extends Element = Element>(element: Element, selector: string): T[] => {
//   // Gets the rootNode and any ancestor rootNodes (shadowRoot or document) of an element and queries them for a selector.
//   const queryFromAll = <T extends Element = Element>(element: Element, allResults: T[]): T[] => {
//     if (!element) return allResults;
//     if ((element as Slottable).assignedSlot) element = (element as Slottable).assignedSlot;
//     const rootNode = getRootNode(element);
//     const results = [...rootNode.querySelectorAll(selector)] as T[];
//     const uniqueResults = results.filter((result) => !allResults.includes(result));
//     allResults = [...allResults, ...uniqueResults];
//     const host = getHost(rootNode);
//     return host ? queryFromAll(host, allResults) : allResults;
//   };
//   return queryFromAll(element, []);
// };
// Queries element's rootNode and ancestor rootNodes.
// Based on https://stackoverflow.com/q/54520554/194216
export const queryElementRoots = (element, selector) => {
  // Gets the rootNode and any ancestor rootNodes (shadowRoot or document) of an element and queries them for a selector.
  const queryFrom = (element) => {
    if (!element)
      return null;
    if (element.assignedSlot)
      element = element.assignedSlot;
    const rootNode = getRootNode(element);
    const found = rootNode.querySelector(selector);
    const host = getHost(rootNode);
    return found ? found : host ? queryFrom(host) : null;
  };
  return queryFrom(element);
};
export const closestElementCrossShadowBoundary = (element, selector) => {
  // based on https://stackoverflow.com/q/54520554/194216
  const closestFrom = (element) => element ? element.closest(selector) || closestFrom(getHost(getRootNode(element))) : null;
  return closestFrom(element);
};
// export interface AbdsFocusableElement extends HTMLElement {
//   setFocus?: () => Promise<void>;
// }
// export const isAbdsFocusable = (element: AbdsFocusableElement): boolean => typeof element?.setFocus === 'function';
// export const focusElement = async (element: AbdsFocusableElement): Promise<void> => {
//   if (!element) return;
//   return isAbdsFocusable(element) ? element.setFocus() : element.focus();
// };
// interface GetSlottedOptions {
//   all?: boolean;
//   direct?: boolean;
//   selector?: string;
// }
// export function getSlotted<T extends Element = Element>(
//   element: Element,
//   slotName: string,
//   options: GetSlottedOptions & { all: true }
// ): T[];
// export function getSlotted<T extends Element = Element>(
//   element: Element,
//   slotName: string,
//   options?: GetSlottedOptions
// ): T | null;
// export function getSlotted<T extends Element = Element>(
//   element: Element,
//   slotName: string,
//   options?: GetSlottedOptions
// ): (T | null) | T[] {
//   return options?.all
//     ? queryMultiple<T>(element, `[slot="${slotName}"]`, options)
//     : querySingle<T>(element, `[slot="${slotName}"]`, options);
// }
// const queryMultiple = <T extends Element = Element>(
//   element: Element,
//   slotSelector: string,
//   { direct, selector }: GetSlottedOptions
// ): T[] => {
//   let matches = [...element.querySelectorAll<T>(slotSelector)];
//   matches = direct === false ? matches : matches.filter((el) => el.parentElement === element);
//   return selector
//     ? matches
//         .map((item) => Array.from(item.querySelectorAll<T>(selector)))
//         .reduce((previousValue, currentValue) => [...previousValue, ...currentValue], [])
//         .filter((match) => !!match)
//     : matches;
// };
// const querySingle = <T extends Element = Element>(
//   element: Element,
//   slotSelector: string,
//   options?: GetSlottedOptions
// ): T | null => {
//   let match = element.querySelector<T>(slotSelector);
//   match = options && options.direct === false ? match : match?.parentElement === element ? match : null;
//   const selector = options?.selector;
//   return selector ? match.querySelector<T>(selector) : match;
// };
// export const filterDirectChildren = <T extends Element>(element: Element, selector: string): T[] =>
//   [...element.children].filter((child): child is T => child.matches(selector));
// export const hasLabel(labelEl: HTMLCalciteLabelElement, element: HTMLElement): boolean => labelEl.contains(element);
// // set a default icon from a defined set or allow an override with an icon name string
// export const setRequestedIcon = (iconObject: Record<string, string>, iconValue: string, matchedValue: string): string =>
//   typeof iconValue === 'string' && iconValue ? iconValue : typeof iconValue === 'string' && iconObject[matchedValue];
// export const intersects = (rect1: DOMRect, rect2: DOMRect): boolean =>
//   (rect1.right < rect2.left ||  rect1.left > rect2.right || rect1.bottom < rect2.top ||  rect1.top > rect2.bottom);
export default { closestElementCrossShadowBoundary, getElementDir, getHost, getRootNode, hasText, queryElementRoots };
//# sourceMappingURL=dom.js.map