/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { ref } from "lit/directives/ref.js";
import { nothing, html } from "lit";
import { i as isActionGroup, S as SLOTS } from "./resources3.js";
import { S as SLOTS$1 } from "./resources2.js";
const queryActions = (el) => {
  return Array.from(el.querySelectorAll("calcite-action")).filter(
    (action) => action.closest("calcite-action-menu") ? action.slot === SLOTS$1.trigger : true
  );
};
const overflowActions = ({
  actionGroups,
  expanded,
  overflowCount
}) => {
  let needToSlotCount = overflowCount;
  actionGroups.reverse().forEach((group) => {
    let slottedWithinGroupCount = 0;
    const directGroupActions = queryActions(group).filter((action) => isActionGroup(action.parentElement)).reverse();
    directGroupActions.forEach((groupAction) => {
      if (groupAction.slot === SLOTS.menuActions) {
        groupAction.removeAttribute("slot");
        groupAction.textEnabled = expanded;
      }
    });
    if (needToSlotCount > 0 && !group.overflowActionsDisabled) {
      directGroupActions.some((groupAction) => {
        const unslottedActions = directGroupActions.filter((action) => !action.slot);
        if (unslottedActions.length > 1 && directGroupActions.length > 2 && !groupAction.closest("calcite-action-menu") && !groupAction.overflowDisabled) {
          groupAction.textEnabled = true;
          groupAction.setAttribute("slot", SLOTS.menuActions);
          slottedWithinGroupCount++;
          if (slottedWithinGroupCount > 1) {
            needToSlotCount--;
          }
        }
        return needToSlotCount < 1;
      });
    }
    group.manager.component.requestUpdate();
  });
};
const ICONS = {
  chevronsLeft: "chevrons-left",
  chevronsRight: "chevrons-right"
};
function getCalcitePosition(el, position) {
  return position || el.closest("calcite-shell-panel")?.position || "start";
}
function toggleChildActionText({ el, expanded }) {
  queryActions(el).filter((el2) => el2.slot !== SLOTS.menuActions).forEach((action) => action.textEnabled = expanded);
  el.querySelectorAll("calcite-action-group, calcite-action-menu").forEach((el2) => el2.expanded = expanded);
}
const setTooltipReference = ({ tooltip, referenceElement, expanded, ref: ref2 }) => {
  if (tooltip) {
    tooltip.referenceElement = !expanded && referenceElement ? referenceElement : null;
  }
  if (ref2) {
    ref2(referenceElement);
  }
  return referenceElement;
};
const ExpandToggle = ({ collapseText, collapseLabel, direction, expanded, expandText, expandLabel, toggle, el, position, tooltip, ref: ref$1, scale }) => {
  const rtl = direction === "rtl";
  const text = expanded ? collapseText : expandText;
  const label = expanded ? collapseLabel : expandLabel;
  const icons = [ICONS.chevronsLeft, ICONS.chevronsRight];
  if (rtl) {
    icons.reverse();
  }
  const end = getCalcitePosition(el, position) === "end";
  const expandIcon = end ? icons[1] : icons[0];
  const collapseIcon = end ? icons[0] : icons[1];
  const actionNode = html`<calcite-action .aria=${{ expanded }} .icon=${expanded ? expandIcon : collapseIcon} id=expand-toggle .label=${label} @click=${toggle} .scale=${scale} .text=${text} .textEnabled=${expanded} title=${(!expanded && !tooltip ? text : void 0) ?? nothing} ${ref((referenceElement) => setTooltipReference({ tooltip, referenceElement, expanded, ref: ref$1 }))}></calcite-action>`;
  return actionNode;
};
export {
  ExpandToggle as E,
  overflowActions as o,
  queryActions as q,
  toggleChildActionText as t
};
