import { prop as H, WithElement as q, Fragment as v, aria as u, OneOfValue as j, OnDispose as z, on as p, Value as E, attr as s, html as D, Empty as W, computedOf as G, dataAttr as J } from "@tempots/dom";
import { PopOver as Q } from "@tempots/ui";
import { g as R } from "./timer-Rd2sKnvH.js";
import { u as X, A as Y } from "./use-animated-toggle-89fKlXxy.js";
import { s as B } from "./session-id-3KiilioY.js";
function Z(m) {
  const l = { fade: !0, scale: !0 };
  return m.startsWith("top") ? (l.slide = "down", l.transformOrigin = "bottom") : m.startsWith("bottom") ? (l.slide = "up", l.transformOrigin = "top") : m.startsWith("left") ? (l.slide = "right", l.transformOrigin = "right") : m.startsWith("right") && (l.slide = "left", l.transformOrigin = "left"), l;
}
function U(m) {
  const {
    content: l,
    placement: P = "top",
    showDelay: x = 250,
    hideDelay: C = 500,
    mainAxisOffset: I = 8,
    crossAxisOffset: V = 0,
    showOn: b = "hover-focus",
    closable: y = !0,
    arrow: L,
    role: g,
    hasPopup: S = "dialog"
  } = m;
  return Q((w, h) => {
    const o = X({
      initialStatus: "closed"
    }), T = B("flyout"), f = H(!1);
    let k = null, d = null, $ = !1, A;
    function _() {
      e != null && (clearTimeout(e), e = null), n != null && (clearTimeout(n), n = null), d && (d(), d = null), k && (document.removeEventListener("keydown", k), k = null), f.set(!1), $ = !1;
    }
    function K() {
      E.get(y) && (k = (i) => {
        i.key === "Escape" && a();
      }, document.addEventListener("keydown", k)), $ = !0, f.set(!0), w({
        placement: P ?? "top",
        mainAxisOffset: I,
        crossAxisOffset: V,
        arrow: L,
        content: q((i) => {
          o.setElement(i);
          const r = typeof b == "function" ? null : E.get(b);
          (r === "hover" || r === "hover-focus") && (i.addEventListener("mouseenter", () => t()), i.addEventListener("mouseleave", () => a())), d = R(() => {
            o.open(), d = null;
          });
          const c = (M) => {
            E.get(y) && M.key === "Escape" && (M.preventDefault(), M.stopPropagation(), a(), f.set(!1));
          };
          document.addEventListener("keydown", c, !0);
          let O = null;
          return E.get(y) && (O = (M) => {
            const F = M.target;
            !i.contains(F) && !A?.contains(F) && a();
          }, document.addEventListener("click", O)), v(
            z(() => {
              O && (document.removeEventListener("click", O), O = null), _(), document.removeEventListener("keydown", c, !0);
            }),
            s.class("bc-flyout-container"),
            Y({
              animation: E.map(P, Z),
              status: o.status
            }),
            s.id(T),
            s.tabindex(-1),
            // Make focusable for screen readers
            D.div(
              s.class("bc-flyout"),
              g ? s.role(g) : s.role("dialog"),
              // Default to dialog role
              l()
            )
          );
        })
      });
    }
    let e = null;
    function t() {
      if (e != null && (clearTimeout(e), e = null), n != null && (clearTimeout(n), n = null), o.isOpened.value || o.isOpening.value || o.isStartOpening.value)
        return;
      if (o.isClosing.value || o.isStartClosing.value) {
        o.open();
        return;
      }
      const i = E.get(x);
      e = setTimeout(() => {
        e = null, K();
      }, i);
    }
    let n = null;
    function a() {
      if (e != null && (clearTimeout(e), e = null), n != null && (clearTimeout(n), n = null), f.set(!1), d && (d(), d = null, $)) {
        h(), _();
        return;
      }
      const i = E.get(C);
      n = setTimeout(() => {
        n = null, o.close(), o.listenOnClosed(() => {
          h(), _();
        });
      }, i);
    }
    return q((i) => {
      A = i;
      const r = () => {
        t(), f.set(!0);
      }, c = () => {
        a(), f.set(!1);
      }, O = v(
        u.expanded(f),
        u.controls(T),
        u.haspopup(
          S
        )
      );
      return typeof b == "function" ? v(
        O,
        b(r, c)
      ) : v(
        O,
        j(b, {
          "hover-focus": () => v(
            p.mouseenter(() => r()),
            p.mouseleave(() => c()),
            p.focus(() => r()),
            p.blur(() => c())
          ),
          hover: () => v(
            p.mouseenter(() => r()),
            p.mouseleave(() => c())
          ),
          focus: () => v(
            p.focus(() => r()),
            p.blur(() => c())
          ),
          click: () => {
            function F() {
              document.removeEventListener("click", N);
            }
            function N() {
              F(), c();
            }
            return v(
              z(F),
              p.click(() => {
                r(), R(() => {
                  document.addEventListener("click", N, {
                    once: !0
                  });
                });
              })
            );
          },
          never: () => null
        })
      );
    });
  });
}
function ae(m) {
  const {
    items: l,
    placement: P = "bottom-start",
    showDelay: x = 0,
    hideDelay: C = 100,
    mainAxisOffset: I = 4,
    crossAxisOffset: V = 0,
    showOn: b = "click",
    closable: y = !0,
    onClose: L,
    onAction: g,
    ariaLabel: S,
    ariaLabelledBy: w
  } = m, h = B("menu"), o = H(-1), T = H([]);
  let f = null;
  return U({
    content: () => q((k) => {
      f = document.activeElement;
      const d = (e, t, n) => {
        if (t.length === 0) return -1;
        let a = e + n, i = 0;
        for (; i < t.length; ) {
          a >= t.length && (a = 0), a < 0 && (a = t.length - 1);
          const r = t[a];
          if (r && r.getAttribute("aria-disabled") !== "true")
            return a;
          a += n, i++;
        }
        return e;
      }, $ = (e) => {
        const t = T.value, n = o.value;
        switch (e.key) {
          case "ArrowDown":
            e.preventDefault(), e.stopPropagation();
            const a = d(n, t, 1);
            A(a, t);
            break;
          case "ArrowUp":
            e.preventDefault(), e.stopPropagation();
            const i = d(n, t, -1);
            A(i, t);
            break;
          case "Enter":
          case " ":
            if (e.preventDefault(), e.stopPropagation(), n >= 0 && t[n]) {
              const r = t[n];
              if (r.getAttribute("aria-disabled") === "true")
                return;
              const c = r.getAttribute("data-key");
              c && g && g(c), r.click(), L?.();
            }
            break;
          case "Escape":
            L?.();
            break;
          case "Home":
            e.preventDefault(), e.stopPropagation(), t.length > 0 && A(0, t);
            break;
          case "End":
            e.preventDefault(), e.stopPropagation(), t.length > 0 && A(t.length - 1, t);
            break;
          case "ArrowRight":
            if (e.preventDefault(), e.stopPropagation(), n >= 0 && t[n]) {
              const r = t[n];
              r.classList.contains(
                "bc-menu-item--has-submenu"
              ) && r.dispatchEvent(new Event("mouseenter"));
            }
            break;
          case "ArrowLeft":
            e.preventDefault(), e.stopPropagation(), L?.();
            break;
        }
      }, A = (e, t) => {
        o.value >= 0 && t[o.value] && (t[o.value].classList.remove(
          "bc-menu-item--focused"
        ), t[o.value].removeAttribute("aria-selected")), e >= 0 && t[e] && (t[e].classList.add("bc-menu-item--focused"), t[e].setAttribute("aria-selected", "true"), o.set(e), typeof t[e].scrollIntoView == "function" && t[e].scrollIntoView({ block: "nearest" }));
      }, _ = () => {
        const e = Array.from(
          k.querySelectorAll('[role="menuitem"]')
        );
        if (T.set(e), e.length > 0) {
          const t = e.findIndex(
            (n) => n.getAttribute("aria-disabled") !== "true"
          );
          t >= 0 && A(t, e);
        }
      }, K = new MutationObserver(_);
      return K.observe(k, { childList: !0, subtree: !0 }), setTimeout(() => {
        _(), k.focus();
      }, 0), document.addEventListener("keydown", $, !0), v(
        z(() => {
          K.disconnect(), document.removeEventListener("keydown", $, !0), f && f.focus();
        }),
        s.class("bc-menu"),
        s.id(h),
        s.role("menu"),
        s.tabindex(-1),
        u.orientation("vertical"),
        S ? u.label(S) : W,
        w ? u.labelledby(w) : W,
        u.activedescendant(
          o.map((e) => {
            const t = T.value;
            return e >= 0 && t[e] ? t[e].id || `${h}-item-${e}` : "";
          })
        ),
        p.click((e) => {
          const n = e.target.closest('[role="menuitem"]');
          if (n && n.getAttribute("aria-disabled") !== "true") {
            const a = n.getAttribute("data-key");
            a && g && g(a);
          }
        }),
        // Live region for screen reader announcements
        D.div(
          s.class("sr-only"),
          u.live("polite"),
          u.atomic(!0),
          o.map((e) => {
            const t = T.value;
            if (e >= 0 && t[e]) {
              const n = t[e].textContent || "";
              return t[e].getAttribute("aria-disabled") === "true" ? `${n}, disabled` : `${n}, ${e + 1} of ${t.length}`;
            }
            return "";
          })
        ),
        ...l()
      );
    }),
    placement: P,
    showDelay: x,
    hideDelay: C,
    mainAxisOffset: I,
    crossAxisOffset: V,
    showOn: b,
    closable: y,
    role: "menu"
  });
}
function re(m) {
  const {
    key: l,
    content: P,
    startContent: x,
    endContent: C,
    disabled: I = !1,
    onClick: V,
    ariaLabel: b,
    submenu: y,
    submenuPlacement: L = "right-start"
  } = m, g = l ?? B("menu-item"), S = `menu-item-${g}`, w = y != null;
  return D.div(
    s.class(
      G(I)(
        (h) => `bc-menu-item ${h ? "bc-menu-item--disabled" : ""} ${w ? "bc-menu-item--has-submenu" : ""}`
      )
    ),
    s.id(S),
    s.role("menuitem"),
    s.tabindex(-1),
    J.key(g),
    u.disabled(I),
    u.selected(!1),
    // Will be updated by focus management
    w ? u.expanded(!1) : W,
    b ? u.label(b) : W,
    p.click((h) => {
      if (E.get(I)) {
        h.preventDefault(), h.stopPropagation();
        return;
      }
      V?.();
    }),
    // Start content
    x && D.span(s.class("bc-menu-item__start"), x),
    // Main content
    D.span(s.class("bc-menu-item__content"), P),
    // End content
    C && D.span(s.class("bc-menu-item__end"), C),
    // Submenu (if present)
    w && y ? U({
      content: () => v(
        s.class("bc-menu bc-submenu"),
        s.role("menu"),
        ...y()
      ),
      placement: L,
      showOn: "hover",
      hasPopup: "menu",
      showDelay: 100,
      hideDelay: 300,
      mainAxisOffset: 0,
      crossAxisOffset: 0,
      role: "menu"
    }) : W
  );
}
function le(m = {}) {
  const { label: l } = m;
  return D.div(
    s.class("bc-menu-separator"),
    s.role("separator"),
    u.orientation("horizontal"),
    l && D.span(s.class("bc-menu-separator__label"), l)
  );
}
export {
  U as F,
  ae as M,
  le as a,
  re as b
};
