import { WithElement as S, OnDispose as W, prop as z, attr as g, computedOf as A, Fragment as R, WithBrowserCtx as J, Value as U, render as Q, html as k, dataAttr as F, aria as O, on as X, When as q, Use as H, coalesce as I } from "@tempots/dom";
import { g as V, T as j } from "./timer-DzWb416P.js";
import { s as Y, C as Z } from "./notice-CQZseCQs.js";
import { a as K, B as _ } from "./translations-djDSPqdb.js";
const ee = [
  "a[href]",
  "button:not([disabled])",
  "input:not([disabled])",
  "select:not([disabled])",
  "textarea:not([disabled])",
  '[tabindex]:not([tabindex="-1"])',
  '[contenteditable="true"]',
  "audio[controls]",
  "video[controls]",
  "details > summary:first-of-type",
  "details[open]"
].join(", ");
function te(o) {
  return Array.from(o.querySelectorAll(ee)).filter(
    (t) => {
      const s = t;
      return s.offsetWidth > 0 && s.offsetHeight > 0 && !s.hasAttribute("inert") && window.getComputedStyle(s).visibility !== "hidden";
    }
  );
}
function $(o = {}) {
  const {
    active: t = !0,
    escapeDeactivates: s = !0,
    onEscape: e,
    initialFocus: n,
    returnFocus: a,
    clickOutsideDeactivates: i = !1,
    onClickOutside: d
  } = o;
  return S((f) => {
    let m = t, p = null, c = [];
    typeof document < "u" && (p = document.activeElement);
    const v = () => {
      c = te(f);
    }, b = (l) => {
      if (m) {
        if (s && l.key === "Escape") {
          l.preventDefault(), e?.();
          return;
        }
        if (l.key === "Tab") {
          if (v(), c.length === 0) {
            l.preventDefault();
            return;
          }
          const r = c.indexOf(
            document.activeElement
          );
          l.shiftKey ? r <= 0 && (l.preventDefault(), c[c.length - 1]?.focus()) : r >= c.length - 1 && (l.preventDefault(), c[0]?.focus());
        }
      }
    }, h = (l) => {
      if (!m || !i) return;
      const r = l.target;
      f.contains(r) || d?.();
    }, y = () => {
      if (!m) return;
      v();
      let l = null;
      typeof n == "function" ? l = n() : n ? l = n : c.length > 0 && (l = c[0]), l && setTimeout(() => {
        l?.focus();
      }, 50), document.addEventListener("keydown", b, !0), i && document.addEventListener("click", h, !0);
    }, C = () => {
      m = !1, document.removeEventListener("keydown", b, !0), document.removeEventListener("click", h, !0);
      let l = null;
      typeof a == "function" ? l = a() : a ? l = a : p && (l = p), l && document.body.contains(l) && setTimeout(() => {
        l?.focus();
      }, 0);
    };
    return setTimeout(() => y(), 0), W(() => {
      C();
    });
  });
}
function me(o = {}) {
  return {
    activate: () => $({ ...o, active: !0 }),
    deactivate: () => $({ ...o, active: !1 })
  };
}
function N({
  initialStatus: o = "closed",
  openedAfter: t,
  closedAfter: s
}) {
  const e = z(o), n = () => {
    e.set("start-opening");
  }, a = () => {
    e.set("start-closing");
  }, i = e.map(
    (r) => r !== "closed" && r !== "start-closing" && r !== "closing"
  ), d = (r) => {
    r ? n() : a();
  }, f = () => {
    d(!i.value);
  }, m = [], p = (r) => {
    m.push(
      e.on((x) => {
        x === "closed" && r();
      })
    );
  };
  let c = () => {
  };
  e.on((r) => {
    switch (c(), r) {
      case "start-opening":
        c = V(() => e.set("opening"));
        break;
      case "opening":
        c = t(() => e.set("opened"));
        break;
      case "start-closing":
        c = V(() => e.set("closing"));
        break;
      case "closing":
        c = s(() => e.set("closed"));
        break;
      case "closed":
        c = () => {
        };
        break;
    }
  }), e.onDispose(() => {
    m.forEach((r) => r()), c();
  });
  const v = e.map((r) => r === "closed"), b = e.map((r) => r === "start-opening"), h = e.map((r) => r === "opening"), y = e.map((r) => r === "opened"), C = e.map((r) => r === "closing"), l = e.map((r) => r === "start-closing");
  return {
    status: e,
    open: n,
    close: a,
    toggle: f,
    setOpen: d,
    display: i,
    isClosed: v,
    isStartOpening: b,
    isOpening: h,
    isOpened: y,
    isClosing: C,
    isStartClosing: l,
    dispose: () => e.dispose(),
    listenOnClosed: p
  };
}
function fe({
  initialStatus: o = "closed",
  duration: t,
  openDuration: s = t ?? 500,
  closeDuration: e = t ?? 500
} = {}) {
  return N({
    initialStatus: o,
    openedAfter: (n) => j(n, s),
    closedAfter: (n) => j(n, e)
  });
}
function P(o, t) {
  let s = !1, e = null;
  function n() {
    s || (s = !0, o.removeEventListener("transitionend", a), o.removeEventListener("animationend", i), e && (clearTimeout(e), e = null), t());
  }
  function a(m) {
    m.target === o && n();
  }
  function i(m) {
    m.target === o && n();
  }
  const f = V(() => {
    if (typeof o.getAnimations > "u") {
      e = setTimeout(n, 16);
      return;
    }
    o.addEventListener("transitionend", a), o.addEventListener("animationend", i), o.getAnimations().length === 0 && (e = setTimeout(n, 1e3));
  });
  return () => {
    s = !0, f(), e && (clearTimeout(e), e = null), o.removeEventListener("transitionend", a), o.removeEventListener("animationend", i);
  };
}
function G({
  initialStatus: o = "closed",
  element: t
} = {}) {
  let s = t;
  return {
    setElement: (e) => {
      s = e;
    },
    ...N({
      initialStatus: o,
      openedAfter: (e) => s == null ? (e(), () => {
      }) : P(s, e),
      closedAfter: (e) => s == null ? (e(), () => {
      }) : P(s, e)
    })
  };
}
function se({
  animation: o = "fade",
  status: t
}) {
  return g.class(
    A(
      o,
      t
    )(
      (s, e) => `bc-animated-toggle bc-animated-toggle--${s} bc-animated-toggle--${e}`
    )
  );
}
function pe({
  initialStatus: o = "closed",
  animation: t = "fade"
}, s) {
  return S((e) => {
    const { setElement: n, dispose: a, ...i } = G({
      initialStatus: o,
      element: e
    });
    return n(e), R(
      se({ status: i.status, animation: t }),
      W(a),
      s(i)
    );
  });
}
function ne(o) {
  return {
    "~standard": {
      version: 1,
      vendor: "beatui-custom",
      validate: (s) => {
        const e = o.validate(s);
        return e.success ? { value: e.data } : {
          issues: e.errors.map((n) => ({
            message: n.message,
            path: n.path
          }))
        };
      },
      types: void 0
    },
    safeParse: (s) => {
      const e = o.validate(s);
      return e.success ? { success: !0, data: e.data } : {
        success: !1,
        error: {
          errors: e.errors.map((n) => ({
            message: n.message,
            path: n.path?.map(
              (a) => typeof a == "object" && "key" in a ? a.key.toString() : a.toString()
            ) || []
          }))
        }
      };
    }
  };
}
class T {
  optional() {
    return new oe(this);
  }
  default(t) {
    return new ae(this, t);
  }
  schema() {
    return ne(this);
  }
}
class w extends T {
  minLength;
  maxLength;
  pattern;
  customValidations = [];
  validate(t) {
    if (typeof t != "string")
      return { success: !1, errors: [{ message: "Expected string" }] };
    const s = [];
    this.minLength !== void 0 && t.length < this.minLength && s.push({ message: `Must be at least ${this.minLength} characters` }), this.maxLength !== void 0 && t.length > this.maxLength && s.push({ message: `Must be at most ${this.maxLength} characters` }), this.pattern && !this.pattern.test(t) && s.push({ message: "Invalid format" });
    for (const e of this.customValidations) {
      const n = e(t);
      n && s.push({ message: n });
    }
    return s.length > 0 ? { success: !1, errors: s } : { success: !0, data: t };
  }
  min(t, s) {
    const e = new w();
    return e.minLength = t, e.maxLength = this.maxLength, e.pattern = this.pattern, e.customValidations = [...this.customValidations], s && e.customValidations.push(
      (n) => n.length < t ? s : null
    ), e;
  }
  max(t, s) {
    const e = new w();
    return e.minLength = this.minLength, e.maxLength = t, e.pattern = this.pattern, e.customValidations = [...this.customValidations], s && e.customValidations.push(
      (n) => n.length > t ? s : null
    ), e;
  }
  regex(t, s) {
    const e = new w();
    return e.minLength = this.minLength, e.maxLength = this.maxLength, e.pattern = t, e.customValidations = [...this.customValidations], s && e.customValidations.push(
      (n) => t.test(n) ? null : s
    ), e;
  }
  email(t = "Please enter a valid email address") {
    const s = /^[^\s@]+@[^\s@]+\.[^\s@]+$/, e = new w();
    return e.minLength = this.minLength, e.maxLength = this.maxLength, e.pattern = this.pattern, e.customValidations = [
      ...this.customValidations,
      (n) => s.test(n) ? null : t
    ], e;
  }
  refine(t) {
    const s = new w();
    return s.minLength = this.minLength, s.maxLength = this.maxLength, s.pattern = this.pattern, s.customValidations = [...this.customValidations, t], s;
  }
}
class B extends T {
  mustBeTrue = !1;
  trueMessage;
  validate(t) {
    return typeof t != "boolean" ? { success: !1, errors: [{ message: "Expected boolean" }] } : this.mustBeTrue && t !== !0 ? {
      success: !1,
      errors: [{ message: this.trueMessage || "Must be true" }]
    } : { success: !0, data: t };
  }
  refine(t, s) {
    const e = new B();
    e.mustBeTrue = this.mustBeTrue, e.trueMessage = this.trueMessage;
    const n = e.validate.bind(e);
    return e.validate = (a) => {
      const i = n(a);
      return i.success ? t(i.data) ? i : { success: !1, errors: [{ message: s }] } : i;
    }, e;
  }
  literal(t, s) {
    const e = new B();
    return e.mustBeTrue = !0, e.trueMessage = s, e;
  }
}
class oe extends T {
  constructor(t) {
    super(), this.inner = t;
  }
  validate(t) {
    if (t === void 0)
      return { success: !0, data: void 0 };
    const s = this.inner.validate(t);
    return s.success ? { success: !0, data: s.data } : { success: !1, errors: s.errors };
  }
}
class ae extends T {
  constructor(t, s) {
    super(), this.inner = t, this.defaultValue = s;
  }
  validate(t) {
    return t === void 0 ? { success: !0, data: this.defaultValue } : this.inner.validate(t);
  }
}
class M extends T {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  constructor(t) {
    super(), this.shape = t;
  }
  validate(t) {
    if (typeof t != "object" || t === null || Array.isArray(t))
      return { success: !1, errors: [{ message: "Expected object" }] };
    const s = t, e = {}, n = [];
    for (const [a, i] of Object.entries(this.shape)) {
      const d = i.validate(s[a]);
      if (d.success)
        e[a] = d.data;
      else
        for (const f of d.errors)
          n.push({
            message: f.message,
            path: [a, ...f.path || []]
          });
    }
    return n.length > 0 ? { success: !1, errors: n } : { success: !0, data: e };
  }
  refine(t, s) {
    const e = new M(this.shape), n = e.validate.bind(e);
    return e.validate = (a) => {
      const i = n(a);
      if (!i.success) return i;
      const d = t(i.data);
      return d ? {
        success: !1,
        errors: [
          {
            message: d,
            path: s?.path
          }
        ]
      } : { success: !0, data: i.data };
    }, e;
  }
}
const he = () => new w(), ge = () => new B(), be = (o) => new M(o);
function re(o) {
  return J((t) => {
    const s = [], e = () => s.forEach((a) => a());
    return o(({
      effect: a = "opaque",
      mode: i = "capturing",
      onClickOutside: d,
      onEscape: f,
      content: m,
      container: p = "body"
    }) => {
      p === "body" && (t = t.makePortal("body"));
      const c = G();
      c.listenOnClosed(e);
      let v = () => {
      }, b = () => {
      };
      const h = (E) => {
        E.key === "Escape" && (f?.(), c.close());
      }, y = () => {
        d?.(), c.close();
      }, C = (E) => {
        v(), b(), E === "capturing" ? (document.addEventListener("keydown", h), v = () => document.removeEventListener("keydown", h), t.element.addEventListener("mousedown", y), b = () => t.element.removeEventListener("mousedown", y)) : (v = () => {
        }, b = () => {
        });
      }, l = U.on(i, C);
      s.push(l), s.push(() => {
        c.dispose(), v(), b();
      });
      const x = Q((() => {
        const E = /* @__PURE__ */ new Set();
        for (const u of t.element.querySelectorAll(
          ":scope > :not([data-overlay])"
        ))
          u.hasAttribute("inert") ? E.add(u) : u.setAttribute("inert", "");
        return s.push(() => {
          for (const u of t.element.querySelectorAll(
            ":scope > :not([data-overlay])"
          ))
            E.has(u) || u.removeAttribute("inert");
          E.clear();
        }), document.activeElement?.blur?.(), k.div(
          S((u) => c.setElement(u)),
          F.status(c.status.map(String)),
          F.overlay("true"),
          g.class(
            A(
              a ?? "opaque",
              i
            )(
              (u, L) => `bc-overlay bc-overlay--effect-${u} bc-overlay--mode-${L}`
            )
          ),
          m
        );
      })(), t.element, {
        disposeWithParent: !0,
        clear: !1,
        providers: t.providers
      });
      s.push(x), V(() => c.open());
    }, e);
  });
}
function ie(o, t) {
  const {
    size: s = "md",
    dismissable: e = !0,
    showCloseButton: n = !0,
    onClose: a,
    overlayEffect: i = "opaque",
    container: d = "body",
    position: f = "center"
  } = o;
  return re((m, p) => {
    let c = () => {
    };
    return t((h) => {
      c = p;
      const y = z("capturing");
      U.on(e, (u) => {
        y.set(u ? "capturing" : "non-capturing");
      });
      const C = A(
        h.header != null,
        n
      )((u, L) => u || L), l = Y("modal"), r = `${l}-header`, x = `${l}-body`, E = k.div(
        g.class(
          A(
            s,
            f
          )(
            (u, L) => `bc-modal bc-modal--size-${u} bc-modal--container-${d} bc-modal--position-${L}`
          )
        ),
        // Essential ARIA attributes for modal dialog
        g.role("dialog"),
        O.modal(!0),
        ...h.header ? [O.labelledby(r)] : [],
        O.describedby(x),
        g.tabindex(-1),
        // Make modal focusable for initial focus
        g.id(l),
        F.focusTrap("true"),
        // Mark as focus trap container
        X.mousedown((u) => u.stopPropagation()),
        // Prevent overlay click-outside when clicking modal content
        // Focus trap implementation
        $({
          escapeDeactivates: !1,
          // Let Overlay handle escape key
          initialFocus: () => {
            const u = document.getElementById(l);
            if (!u) return null;
            const L = u.querySelector(
              '[aria-label="Close modal"]'
            );
            if (L) return L;
            const D = u.querySelector(
              'button, input, select, textarea, [tabindex]:not([tabindex="-1"])'
            );
            return D || u;
          }
        }),
        // Modal content container
        k.div(
          g.class("bc-modal__content"),
          // Header section
          q(
            C,
            () => k.div(
              g.class("bc-modal__header"),
              k.div(
                g.class("bc-modal__title"),
                ...h.header ? [g.id(r)] : [],
                h.header
              ),
              q(
                n,
                () => H(
                  K,
                  (u) => Z({
                    size: "sm",
                    label: u.$.closeModal,
                    onClick: () => {
                      c(), p();
                    }
                  })
                )
              )
            )
          ),
          // Body section
          k.div(g.class("bc-modal__body"), g.id(x), h.body),
          // Footer section
          h.footer && k.div(g.class("bc-modal__footer"), h.footer)
        )
      );
      m({
        mode: y,
        effect: i ?? "opaque",
        container: d,
        content: E,
        onClickOutside: () => {
          a?.(), p();
        },
        onEscape: () => {
          a?.(), p();
        }
      });
    }, () => {
      c(), p();
    });
  });
}
function ve(o, t) {
  const { confirmText: s, cancelText: e, onConfirm: n, onCancel: a, ...i } = o;
  return H(
    K,
    (d) => ie({ showCloseButton: !1, ...i }, (f, m) => {
      const p = () => {
        n?.(), m();
      }, c = () => {
        a?.(), m();
      };
      return t((b) => {
        f({
          body: b,
          footer: R(
            g.class("bc-modal__actions"),
            _(
              {
                variant: "outline",
                onClick: c
              },
              I(e, d.$.cancel)
            ),
            _(
              {
                color: "primary",
                variant: "filled",
                onClick: p
              },
              I(s, d.$.confirm)
            )
          )
        });
      }, m);
    })
  );
}
export {
  se as A,
  B,
  ve as C,
  $ as F,
  ie as M,
  re as O,
  w as S,
  pe as a,
  ge as b,
  M as c,
  ne as d,
  me as e,
  N as f,
  fe as g,
  be as o,
  he as s,
  G as u
};
