import { Value as p, prop as rt, computedOf as D, html as k, attr as u, svg as tt, svgAttr as Y, on as M, Empty as A, When as W, Fragment as j, aria as N, emitValue as Z, Use as lt, input as ot, emitValueAsNullableDate as et, emitValueAsNullableDateTime as nt, Repeat as ut, style as pt, emit as dt, emitValueAsNumber as V } from "@tempots/dom";
import { F as ht, N as z, T as gt, U as ft, a as mt } from "./deep-merge-BRqRzEQs.js";
import { $ as bt } from "./string-DYyMxBl-.js";
import { I as E } from "./input-container-DhUiLmXE.js";
import { C as U, T as wt } from "./text-input-BZmAB3_o.js";
import { B as yt, I as q, b as st, a as It } from "./translations-BGblCUBj.js";
import { AutoSelect as Mt } from "@tempots/ui";
import { E as $t, P as xt } from "./notice-BmkckDsD.js";
import { s as _t } from "./session-id-3KiilioY.js";
import { r as kt } from "./utils-DEbsp9Q9.js";
function it(t) {
  if (typeof t != "string") return !1;
  const e = t.startsWith("#") ? t.slice(1) : t;
  return /^[0-9A-Fa-f]{3}$|^[0-9A-Fa-f]{6}$/.test(e);
}
function Ft(t) {
  if (typeof t != "string") return !1;
  const e = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/i, s = t.match(e);
  if (!s) return !1;
  const [, n, r, a] = s, o = parseInt(n, 10), i = parseInt(r, 10), c = parseInt(a, 10);
  return o >= 0 && o <= 255 && i >= 0 && i <= 255 && c >= 0 && c <= 255;
}
function Ct(t) {
  if (typeof t != "string") return !1;
  const e = /^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0?\.\d+)\s*\)$/i, s = t.match(e);
  if (!s) return !1;
  const [, n, r, a, o] = s, i = parseInt(n, 10), c = parseInt(r, 10), l = parseInt(a, 10), d = parseFloat(o);
  return i >= 0 && i <= 255 && c >= 0 && c <= 255 && l >= 0 && l <= 255 && d >= 0 && d <= 1;
}
function Tt(t) {
  if (typeof t != "string") return !1;
  const e = /^hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)$/i, s = t.match(e);
  if (!s) return !1;
  const [, n, r, a] = s, o = parseInt(n, 10), i = parseInt(r, 10), c = parseInt(a, 10);
  return o >= 0 && o <= 360 && i >= 0 && i <= 100 && c >= 0 && c <= 100;
}
function re(t) {
  return it(t) || Ft(t) || Ct(t) || Tt(t);
}
function At(t) {
  if (!it(t)) return null;
  let e = t.startsWith("#") ? t.slice(1) : t;
  return e.length === 3 && (e = e.split("").map((s) => s + s).join("")), `#${e.toLowerCase()}`;
}
function Q(t, e, s) {
  const n = (r) => {
    const a = Math.round(Math.max(0, Math.min(255, r))).toString(16);
    return a.length === 1 ? "0" + a : a;
  };
  return `#${n(t)}${n(e)}${n(s)}`;
}
function H(t) {
  const e = At(t);
  if (!e) return null;
  const s = /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);
  return s ? {
    r: parseInt(s[1], 16),
    g: parseInt(s[2], 16),
    b: parseInt(s[3], 16)
  } : null;
}
function oe(t, e) {
  const s = H(t), n = H(e);
  if (!s || !n) return null;
  const r = (l, d, f) => {
    const [m, h, w] = [l, d, f].map((I) => (I = I / 255, I <= 0.03928 ? I / 12.92 : Math.pow((I + 0.055) / 1.055, 2.4)));
    return 0.2126 * m + 0.7152 * h + 0.0722 * w;
  }, a = r(s.r, s.g, s.b), o = r(n.r, n.g, n.b), i = Math.max(a, o), c = Math.min(a, o);
  return (i + 0.05) / (c + 0.05);
}
function St(t) {
  if (!t) return [0, 0, 0, 1];
  const s = t.trim().match(
    /^#?([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/
  );
  if (s) {
    const l = s[1];
    if (l.length === 8) {
      const d = parseInt(l.slice(0, 2), 16), f = parseInt(l.slice(2, 4), 16), m = parseInt(l.slice(4, 6), 16), h = parseInt(l.slice(6, 8), 16) / 255;
      return [d, f, m, h];
    }
    if (l.length === 6)
      return [
        parseInt(l.slice(0, 2), 16),
        parseInt(l.slice(2, 4), 16),
        parseInt(l.slice(4, 6), 16),
        1
      ];
    if (l.length === 4) {
      const d = parseInt(l[0] + l[0], 16), f = parseInt(l[1] + l[1], 16), m = parseInt(l[2] + l[2], 16), h = parseInt(l[3] + l[3], 16) / 255;
      return [d, f, m, h];
    }
    if (l.length === 3) {
      const d = parseInt(l[0] + l[0], 16), f = parseInt(l[1] + l[1], 16), m = parseInt(l[2] + l[2], 16);
      return [d, f, m, 1];
    }
  }
  const n = t.match(
    /^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0?\.\d+)\s*\)$/i
  );
  if (n)
    return [
      parseInt(n[1], 10),
      parseInt(n[2], 10),
      parseInt(n[3], 10),
      parseFloat(n[4])
    ];
  const r = t.match(
    /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/i
  );
  if (r)
    return [parseInt(r[1], 10), parseInt(r[2], 10), parseInt(r[3], 10), 1];
  const a = t.match(
    /^hsla?\(\s*([+-]?[\d.]+)(?:deg)?\s*[ ,]?\s*([\d.]+)%\s*[ ,]?\s*([\d.]+)%\s*(?:[/,]\s*(\d?(?:\.\d+)?))?\s*\)$/i
  );
  if (a) {
    const l = parseFloat(a[1]), d = parseFloat(a[2]), f = parseFloat(a[3]), m = a[4] != null ? parseFloat(a[4]) : 1, [h, w, I] = ct(l, d / 100, f / 100);
    return [h, w, I, m];
  }
  const o = t.match(
    /^hwb\(\s*([+-]?[\d.]+)(?:deg)?\s*[, ]\s*([\d.]+)%\s*[, ]\s*([\d.]+)%\s*(?:[/]\s*(\d?(?:\.\d+)?))?\s*\)$/i
  );
  if (o) {
    const l = parseFloat(o[1]), d = parseFloat(o[2]) / 100, f = parseFloat(o[3]) / 100, m = o[4] != null ? parseFloat(o[4]) : 1, [h, w, I] = Nt(l, d, f);
    return [h, w, I, m];
  }
  const i = t.match(
    /^oklch\(\s*([+-]?[\d.]+%?)\s+([\d.]+)\s+([+-]?[\d.]+)(?:deg)?(?:\s*\/\s*(\d?(?:\.\d+)?))?\s*\)$/i
  );
  if (i) {
    const l = i[1], d = parseFloat(i[2]), f = parseFloat(i[3]), m = i[4] != null ? parseFloat(i[4]) : 1, h = l.endsWith("%") ? Math.max(0, Math.min(1, parseFloat(l) / 100)) : Math.max(0, Math.min(1, parseFloat(l))), [w, I, x] = Bt(h, d, f);
    return [w, I, x, m];
  }
  const c = H(t);
  return c ? [c.r, c.g, c.b, 1] : [0, 0, 0, 1];
}
function vt(t, e, s, n) {
  return `rgba(${Math.round(t)}, ${Math.round(e)}, ${Math.round(s)}, ${Math.max(
    0,
    Math.min(1, Math.round(n * 100) / 100)
  )})`;
}
function Rt(t) {
  let e = t + 1831565813;
  return function() {
    return e = Math.imul(e ^ e >>> 15, e | 1), e ^= e + Math.imul(e ^ e >>> 7, e | 61), ((e ^ e >>> 14) >>> 0) / 4294967296;
  };
}
function ct(t, e, s) {
  t = (t % 360 + 360) % 360;
  const n = (1 - Math.abs(2 * s - 1)) * e, r = n * (1 - Math.abs(t / 60 % 2 - 1)), a = s - n / 2;
  let o = 0, i = 0, c = 0;
  return 0 <= t && t < 60 ? [o, i, c] = [n, r, 0] : 60 <= t && t < 120 ? [o, i, c] = [r, n, 0] : 120 <= t && t < 180 ? [o, i, c] = [0, n, r] : 180 <= t && t < 240 ? [o, i, c] = [0, r, n] : 240 <= t && t < 300 ? [o, i, c] = [r, 0, n] : [o, i, c] = [n, 0, r], [
    Math.round((o + a) * 255),
    Math.round((i + a) * 255),
    Math.round((c + a) * 255)
  ];
}
function Nt(t, e, s) {
  t = (t % 360 + 360) % 360;
  const n = e + s;
  n > 1 && (e /= n, s /= n);
  const [r, a, o] = ct(t, 1, 0.5).map((f) => f / 255), i = 1 - e - s, c = r * i + e, l = a * i + e, d = o * i + e;
  return [Math.round(c * 255), Math.round(l * 255), Math.round(d * 255)];
}
function J(t, e, s) {
  t /= 255, e /= 255, s /= 255;
  const n = Math.max(t, e, s), r = Math.min(t, e, s);
  let a = 0, o = 0;
  const i = (n + r) / 2, c = n - r;
  if (c !== 0) {
    switch (o = i > 0.5 ? c / (2 - n - r) : c / (n + r), n) {
      case t:
        a = (e - s) / c + (e < s ? 6 : 0);
        break;
      case e:
        a = (s - t) / c + 2;
        break;
      default:
        a = (t - e) / c + 4;
    }
    a *= 60;
  }
  return [Math.round(a), Math.round(o * 100), Math.round(i * 100)];
}
function Dt(t, e, s) {
  const [n] = J(t, e, s), r = t / 255, a = e / 255, o = s / 255, i = Math.min(r, a, o), c = 1 - Math.max(r, a, o);
  return [n, Math.round(i * 100), Math.round(c * 100)];
}
function G(t) {
  const e = t / 255;
  return e <= 0.04045 ? e / 12.92 : Math.pow((e + 0.055) / 1.055, 2.4);
}
function K(t) {
  const e = t <= 31308e-7 ? 12.92 * t : 1.055 * Math.pow(t, 0.4166666666666667) - 0.055;
  return Math.round(Math.max(0, Math.min(1, e)) * 255);
}
function Bt(t, e, s) {
  const n = s * Math.PI / 180, r = Math.cos(n) * e, a = Math.sin(n) * e, o = t + 0.3963377774 * r + 0.2158037573 * a, i = t - 0.1055613458 * r - 0.0638541728 * a, c = t - 0.0894841775 * r - 1.291485548 * a, l = o * o * o, d = i * i * i, f = c * c * c, m = 4.0767416621 * l - 3.3077115913 * d + 0.2309699292 * f, h = -1.2684380046 * l + 2.6097574011 * d - 0.3413193965 * f, w = -0.0041960863 * l - 0.7034186147 * d + 1.707614701 * f;
  return [K(m), K(h), K(w)];
}
function Vt(t, e, s) {
  const n = G(t), r = G(e), a = G(s), o = 0.4122214708 * n + 0.5363325363 * r + 0.0514459929 * a, i = 0.2119034982 * n + 0.6806995451 * r + 0.1073969566 * a, c = 0.0883024619 * n + 0.2817188376 * r + 0.6299787005 * a, l = Math.cbrt(o), d = Math.cbrt(i), f = Math.cbrt(c), m = 0.2104542553 * l + 0.793617785 * d - 0.0040720468 * f, h = 1.9779984951 * l - 2.428592205 * d + 0.4505937099 * f, w = 0.0259040371 * l + 0.7827717662 * d - 0.808675766 * f, I = Math.sqrt(h * h + w * w);
  let x = Math.atan2(w, h) * 180 / Math.PI;
  return x < 0 && (x += 360), [m, I, x];
}
function P(t, e, s, n, r, a) {
  switch (r) {
    case "hex":
      if (a) {
        const o = (c) => c.toString(16).padStart(2, "0"), i = Math.max(0, Math.min(255, Math.round(n * 255)));
        return `#${o(t)}${o(e)}${o(s)}${o(i)}`;
      }
      return Q(t, e, s);
    case "rgb":
      return `rgb(${t}, ${e}, ${s})`;
    case "rgba":
      return `rgba(${t}, ${e}, ${s}, ${Math.round(n * 100) / 100})`;
    case "hsl": {
      const [o, i, c] = J(t, e, s);
      return `hsl(${o}, ${i}%, ${c}%)`;
    }
    case "hsla": {
      const [o, i, c] = J(t, e, s);
      return `hsla(${o}, ${i}%, ${c}%, ${Math.round(n * 100) / 100})`;
    }
    case "hwb": {
      const [o, i, c] = Dt(t, e, s);
      return n < 1 ? `hwb(${o} ${i}% ${c}% / ${Math.round(n * 100) / 100})` : `hwb(${o} ${i}% ${c}%)`;
    }
    case "oklch": {
      const [o, i, c] = Vt(t, e, s), l = (Math.round(o * 1e3) / 1e3).toFixed(3), d = (Math.round(i * 1e3) / 1e3).toFixed(3), f = (Math.round(c * 10) / 10).toFixed(1), m = Math.round(n * 100) / 100;
      return a || n < 1 ? `oklch(${l} ${d} ${f} / ${m})` : `oklch(${l} ${d} ${f})`;
    }
  }
}
function O(t, e) {
  return e ? t === "rgb" ? "rgba" : t === "hsl" ? "hsla" : t : t === "rgba" ? "rgb" : t === "hsla" ? "hsl" : t;
}
async function Et(t) {
  return new Promise((e, s) => {
    const n = new FileReader();
    n.readAsDataURL(t), n.onload = () => {
      const r = n.result;
      e(r.split(",")[1]);
    }, n.onerror = (r) => s(r);
  });
}
function Ut(t) {
  return Promise.all(t.map(Et));
}
function Lt(t) {
  if (t.length >= 4) {
    if (t[0] === 137 && t[1] === 80 && t[2] === 78 && t[3] === 71)
      return "image/png";
    if (t[0] === 255 && t[1] === 216) return "image/jpeg";
    if (t[0] === 71 && t[1] === 73 && t[2] === 70)
      return "image/gif";
    if (t[0] === 82 && t[1] === 73 && t[2] === 70 && t[3] === 70 && t.length >= 12 && t[8] === 87 && t[9] === 69 && t[10] === 66 && t[11] === 80)
      return "image/webp";
  }
  try {
    if (new TextDecoder("utf-8").decode(t.slice(0, 256)).includes("<svg")) return "image/svg+xml";
  } catch {
  }
  return "application/octet-stream";
}
function Pt(t) {
  const e = bt(t ?? ""), s = e.length, n = new Uint8Array(s);
  for (let r = 0; r < s; r++) n[r] = e.charCodeAt(r);
  return n;
}
function Ot(t) {
  const {
    value: e,
    onChange: s,
    onInput: n,
    ...r
  } = t, a = /* @__PURE__ */ new Map(), o = p.toSignal(e).map((d) => d.map((f, m) => {
    const h = a.get(f), w = Pt(f ?? ""), I = h?.type || Lt(w), x = h?.name ?? `file-${m}`, y = w.buffer.slice(
      w.byteOffset,
      w.byteOffset + w.byteLength
    ), b = new Blob([y], { type: I });
    return new File([b], x, { type: I });
  })), i = (d) => (f) => {
    d && Ut(f).then((m) => {
      for (const [h, w] of m.entries())
        a.set(w, { name: f[h].name, type: f[h].type });
      d(m);
    });
  }, c = i(s), l = i(n);
  return ht({
    ...r,
    value: o,
    onChange: c,
    onInput: l
  });
}
function ie(t) {
  const {
    value: e,
    onInput: s,
    onChange: n,
    ...r
  } = t;
  return Ot({
    ...r,
    maxFiles: 1,
    value: p.map(e, (a) => a == null ? [] : [a]),
    onChange: (a) => {
      n?.(a[0]);
    },
    onInput: (a) => {
      s?.(a[0]);
    }
  });
}
function Wt(t, e) {
  const [s, n, r] = t, a = s << 16 ^ n << 8 ^ r, o = Rt(a), i = 6 + Math.floor(o() * 5), c = 0.18 + o() * 0.06, l = [];
  for (let h = 0; h < i; h++) {
    const w = h / i * Math.PI * 2, I = e * (1 + (o() * 2 - 1) * c), x = Math.cos(w) * I, y = Math.sin(w) * I;
    l.push({ x, y });
  }
  const d = (l[0].x + l[i - 1].x) / 2, f = (l[0].y + l[i - 1].y) / 2;
  let m = `M ${d.toFixed(3)} ${f.toFixed(3)}`;
  for (let h = 0; h < i; h++) {
    const w = l[h], I = l[(h + 1) % i], x = (w.x + I.x) / 2, y = (w.y + I.y) / 2;
    m += ` Q ${w.x.toFixed(3)} ${w.y.toFixed(3)} ${x.toFixed(3)} ${y.toFixed(3)}`;
  }
  return m += " Z", m;
}
const ce = (t) => {
  const { value: e, onBlur: s, onChange: n, onInput: r, displayValue: a, size: o, withAlpha: i } = t, c = p.map(o ?? 32, (g) => g), l = p.map(e, (g) => St(g ?? "#000000")), d = p.map(
    l,
    ([g, $, F]) => [g, $, F]
  ), f = p.map(l, ([, , , g]) => g), m = rt(p.get(f) ?? 1), h = p.map(i ?? !1, (g) => g), w = p.map(
    t.colorTextFormat ?? "rgb",
    (g) => g
  ), I = D(
    d,
    m,
    w,
    h
  )(
    ([g, $, F], S, v, R) => P(g, $, F, S ?? 1, O(v, R), R)
  ), x = p.map(
    t.colorTextFormat ?? "hex",
    (g) => g
  ), y = p.map(c, (g) => `${-g / 2} ${-g / 2} ${g} ${g}`), b = D(
    d,
    c
  )((g, $) => Wt(g, $ / 2)), _ = D(
    d,
    m,
    h
  )(
    ([g, $, F], S, v) => v || S < 1 ? vt(g, $, F, S) : Q(g, $, F)
  ), T = k.div(
    u.class("bc-color-swatch-input__control"),
    u.class(
      p.map(
        h,
        (g) => g ? "bc-color-swatch-input__control--alpha" : ""
      )
    ),
    u.style(
      D(c)((g) => `min-width:${g + 2}px;height:${g + 2}px`)
    ),
    // The SVG blob preview
    tt.svg(
      u.class("bc-color-swatch-input__svg"),
      Y.viewBox(y),
      tt.path(Y.d(b), Y.fill(_))
    ),
    // Invisible native input overlays the blob for picker and accessibility
    k.input(
      u.type("color"),
      U(t),
      // Native color input needs hex without alpha
      u.value(p.map(d, ([g, $, F]) => Q(g, $, F))),
      u.class(
        "bc-input bc-color-swatch-input bc-color-swatch-input__native"
      ),
      s != null ? M.blur(s) : A,
      n != null ? M.change((g) => {
        const $ = g.target.value;
        if (!n) return;
        const { r: F, g: S, b: v } = H($) ?? { r: 0, g: 0, b: 0 }, R = p.get(m) ?? 1, B = O(
          p.get(x),
          p.get(h)
        ), X = P(F, S, v, R, B, p.get(h));
        n(X);
      }) : A,
      r != null ? M.input((g) => {
        const $ = g.target.value;
        if (!r) return;
        const { r: F, g: S, b: v } = H($) ?? { r: 0, g: 0, b: 0 }, R = p.get(m) ?? 1, B = O(
          p.get(x),
          p.get(h)
        ), X = P(F, S, v, R, B, p.get(h));
        r(X);
      }) : A
    )
  ), C = W(
    h,
    () => k.input(
      u.type("range"),
      u.class("bc-color-swatch-input__alpha"),
      u.min(0),
      u.max(1),
      u.step(0.01),
      u.value(p.map(m, (g) => String(g ?? 1))),
      u.disabled(t.disabled),
      M.input((g) => {
        const $ = parseFloat(g.target.value);
        m.set($);
        const [F, S, v] = p.get(d), R = O(
          p.get(x),
          p.get(h)
        ), B = P(F, S, v, $, R, p.get(h));
        r?.(B);
      }),
      M.change((g) => {
        const $ = parseFloat(g.target.value);
        m.set($);
        const [F, S, v] = p.get(d), R = O(
          p.get(x),
          p.get(h)
        ), B = P(F, S, v, $, R, p.get(h));
        n?.(B);
      })
    )
  );
  return E({
    baseContainer: !0,
    ...t,
    // ensure our control does not try to grow
    growInput: !1,
    input: T,
    // If caller provided an `after`, append RGB before it
    after: j(
      W(
        a ?? !1,
        () => k.span(u.class("bc-color-swatch-input__rgb"), I)
      ),
      C,
      t.after
    )
  });
}, le = ({
  startEditing: t,
  value: e,
  onChange: s,
  placeholder: n,
  disabled: r
}) => {
  const a = p.deriveProp(t ?? !1), o = rt(!1), i = p.map(r ?? !1, (c) => c);
  return k.div(
    u.class("bc-editable-text"),
    u.class(
      p.map(
        i,
        (c) => c ? "bc-editable-text--disabled" : ""
      )
    ),
    N.disabled(i),
    W(
      a,
      () => k.input(
        u.placeholder(n),
        u.value(e),
        u.class("bc-editable-text__input"),
        Mt(),
        M.keydown((c) => {
          c.key === "Enter" ? a.set(!1) : c.key === "Escape" && (o.set(!0), a.set(!1));
        }),
        M.blur(
          Z((c) => {
            if (a.set(!1), o.value) {
              o.set(!1);
              return;
            }
            s(c);
          })
        )
      ),
      () => k.span(
        M.click(() => {
          p.get(i) || a.set(!0);
        }),
        u.class("bc-editable-text__display"),
        W(
          p.map(e, (c) => c != null && c.trim() !== ""),
          () => k.span(u.class("bc-editable-text__text"), e),
          () => k.span(
            u.class("bc-editable-text__placeholder"),
            n
          )
        ),
        W(
          p.map(i, (c) => !c),
          () => k.button(
            u.type("button"),
            u.class("bc-editable-text__edit-button"),
            lt(yt, (c) => N.label(c.$.editLabel)),
            M.click(() => a.set(!0)),
            q({ icon: "line-md/pencil", color: "neutral" })
          )
        )
      )
    )
  );
}, ue = (t) => {
  const { value: e, onBlur: s, onChange: n, onInput: r, after: a, disabled: o } = t, i = z(e, o, n ?? r);
  return E({
    ...t,
    input: ot.date(
      U(t),
      u.valueAsDate(e),
      u.class("bc-input"),
      s != null ? M.blur(Z(s)) : A,
      n != null ? M.change(et(n)) : A,
      r != null ? M.input(et(r)) : A
    ),
    after: a != null ? j(i, a) : i
  });
}, Ht = (t) => {
  const e = t.getFullYear(), s = t.getMonth() + 1, n = t.getDate(), r = t.getHours(), a = t.getMinutes(), o = t.getSeconds();
  return `${e}-${s.toString().padStart(2, "0")}-${n.toString().padStart(2, "0")}T${r.toString().padStart(2, "0")}:${a.toString().padStart(2, "0")}:${o.toString().padStart(2, "0")}`;
}, pe = (t) => {
  const { value: e, onBlur: s, onChange: n, onInput: r, after: a, disabled: o } = t, i = p.map(e, (l) => l != null ? Ht(l) : null), c = z(e, o, n ?? r);
  return E({
    ...t,
    input: ot["datetime-local"](
      U(t),
      u.value(p.map(i, (l) => l ?? null)),
      u.class("bc-input"),
      s != null ? M.blur(Z(s)) : A,
      n != null ? M.change(nt(n)) : A,
      r != null ? M.input(nt(r)) : A
    ),
    after: a != null ? j(c, a) : c
  });
}, at = (t) => typeof t == "string" && t.trim() === "" ? null : t, jt = (t) => t ?? "";
function L(t) {
  return (e) => {
    const { value: s, onBlur: n, onChange: r, onInput: a, after: o, disabled: i, ...c } = e, l = z(s, i, r ?? a);
    return t({
      ...c,
      disabled: i,
      value: p.map(s, jt),
      onChange: r != null ? (d) => r(at(d)) : void 0,
      onInput: a != null ? (d) => a(at(d)) : void 0,
      onBlur: n,
      after: o != null ? j(l, o) : l
    });
  };
}
const de = L($t), he = L(wt), ge = L(xt), fe = L(
  gt
), me = ({
  value: t,
  onChange: e,
  onInput: s,
  onBlur: n,
  offLabel: r,
  onLabel: a,
  disabled: o = !1,
  size: i = "md",
  id: c,
  color: l = "primary",
  tabIndex: d = 0,
  matchInputHeight: f = !0
}) => {
  const m = c ?? _t("switch");
  function h(y, b) {
    const _ = [
      "bc-switch",
      `bc-switch--size-${b}`,
      `bc-switch--${b}`
    ];
    return f && _.push("bc-switch--match-input"), y && _.push("bc-switch--disabled"), _.join(" ");
  }
  function w(y) {
    const b = y ?? "primary", _ = /* @__PURE__ */ new Map(), T = st(b, "solid", "light"), C = st(b, "solid", "dark");
    return _.set("--switch-track-on-bg", T.backgroundColor), _.set("--switch-track-on-label", T.textColor), _.set("--switch-track-on-bg-dark", C.backgroundColor), _.set("--switch-track-on-label-dark", C.textColor), _.set(
      "--switch-track-on-border-dark",
      It(b, "dark")
    ), Array.from(_.entries()).map(([g, $]) => `${g}: ${$}`).join("; ");
  }
  const I = () => {
    p.get(o) || (e?.(!p.get(t)), s?.(!p.get(t)));
  }, x = (y) => {
    p.get(o) || (y.key === " " || y.key === "Enter") && (y.preventDefault(), I());
  };
  return k.div(
    u.class(
      D(
        o ?? !1,
        i
      )(
        (y, b) => h(y ?? !1, b ?? "md")
      )
    ),
    u.style(
      D(l)(
        (y) => w(y)
      )
    ),
    u.id(m),
    u.role("switch"),
    u.tabindex(
      D(
        o ?? !1,
        d
      )((y, b) => y ? -1 : b ?? 0)
    ),
    N.checked(t),
    N.disabled(o),
    M.click(I),
    M.keydown(x),
    n != null ? M.blur(n) : null,
    k.div(
      u.class("bc-switch__track"),
      u.class(
        p.map(
          t,
          (y) => y ? "bc-switch__track--on" : "bc-switch__track--off"
        )
      ),
      r != null ? k.div(
        N.hidden(!0),
        u.class("bc-switch__track-label bc-switch__track-label--off"),
        u.class(
          p.map(
            t,
            (y) => y ? "bc-switch__track-label--hidden" : "bc-switch__track-label--visible"
          )
        ),
        r
      ) : null,
      a != null ? k.div(
        u.class("bc-switch__track-label bc-switch__track-label--on"),
        u.class(
          p.map(
            t,
            (y) => y ? "bc-switch__track-label--visible" : "bc-switch__track-label--hidden"
          )
        ),
        a
      ) : null,
      k.div(
        u.class("bc-switch__thumb"),
        u.class(
          p.map(
            t,
            (y) => y ? "bc-switch__thumb--on" : "bc-switch__thumb--off"
          )
        )
      )
    )
  );
}, be = L(ft), zt = "line-md:star-alt-filled", Xt = "line-md:star-alt", Yt = (t) => {
  const {
    value: e,
    onChange: s,
    disabled: n,
    max: r = 5,
    fullColor: a = "yellow",
    emptyColor: o = "neutral",
    fullIcon: i = zt,
    emptyIcon: c = Xt,
    size: l = "md",
    onBlur: d,
    rounding: f = 1
  } = t, m = () => {
    const b = p.get(f);
    return b > 0 ? b : 1;
  }, h = (b) => Math.min(Math.max(b, 0), p.get(r)), w = (b, _) => {
    if (p.get(n ?? !1)) return;
    const C = b.currentTarget.getBoundingClientRect(), g = (b.clientX - C.left) / C.width, $ = _ - 1 + g, F = m(), S = Math.ceil($ / F) * F, v = h(S);
    s?.(v);
  }, I = (b) => {
    if (p.get(n ?? !1)) return;
    const _ = m(), T = p.get(e) ?? 0;
    let C;
    switch (b.key) {
      case "ArrowRight":
      case "ArrowUp":
        C = h(T + _);
        break;
      case "ArrowLeft":
      case "ArrowDown":
        C = h(T - _);
        break;
      case "Home":
        C = 0;
        break;
      case "End":
        C = p.get(r);
        break;
      default:
        return;
    }
    b.preventDefault(), s?.(C);
  }, x = p.map(l, (b) => `bc-icon--${b}`), y = ({ index: b, counter: _ }) => k.span(
    u.class("bc-rating-input__icon-container"),
    u.class(x),
    q(
      {
        icon: c,
        size: l,
        color: o,
        tone: "soft"
      },
      u.class("bc-rating-input__icon-empty")
    ),
    k.span(
      u.class("bc-rating-input__icon-clipper"),
      u.class(x),
      pt.width(
        p.map(e, (T) => {
          const C = Math.floor(T);
          return C > b ? "100%" : C < b ? "0%" : `${(T - b) * 100}%`;
        })
      ),
      q(
        {
          icon: i,
          size: l,
          color: a,
          tone: "soft"
        },
        u.class("bc-rating-input__icon-full")
      )
    ),
    M.click(
      dt((T) => w(T, _), {
        preventDefault: !0,
        stopPropagation: !0
      })
    )
  );
  return E({
    baseContainer: !0,
    growInput: !1,
    focusableSelector: '[role="slider"]',
    ...t,
    input: k.div(
      // Common input attributes (id, required, invalid, custom classes, etc.)
      U(t),
      u.class("bc-rating-input"),
      // ARIA slider semantics
      u.role("slider"),
      u.tabindex(p.map(n ?? !1, (b) => b ? -1 : 0)),
      N.disabled(n ?? !1),
      N.valuemin(0),
      N.valuemax(p.map(r, (b) => b ?? 0)),
      N.valuenow(p.map(e, (b) => b ?? 0)),
      N.valuetext(
        D(
          e,
          r
        )((b, _) => {
          const T = b ?? 0, C = _ ?? 0;
          return `${String(T)} / ${String(C)}`;
        })
      ),
      // Keyboard & focus handlers
      M.keydown(I),
      d != null ? M.blur(d) : null,
      ut(r, y)
    )
  });
}, we = (t) => {
  const { value: e, onChange: s, onInput: n, onBlur: r, after: a, disabled: o, ...i } = t, c = z(e, o, s ?? n);
  return Yt({
    ...i,
    // Map null -> 0 for display so the control shows as empty when null
    value: p.map(e, (l) => l ?? 0),
    // Pass through numeric changes; clear button will call onChange(null)
    onChange: s,
    onInput: n,
    onBlur: r,
    after: a != null ? j(c, a) : c
  });
}, ye = (t) => {
  const { value: e, step: s, min: n, max: r, onBlur: a, onChange: o, onInput: i } = t;
  return E({
    ...t,
    // Make sure clicks anywhere focus the range input
    focusableSelector: 'input[type="range"]',
    input: k.input(
      u.type("range"),
      U(t),
      u.min(n),
      u.max(r),
      u.step(s),
      // Using value as number to keep it in sync
      u.valueAsNumber(e),
      u.class("bc-input bc-slider-input"),
      a != null ? M.blur(V(a)) : A,
      o != null ? M.change(V(o)) : A,
      i != null ? M.input(V(i)) : A
    )
  });
}, Ie = (t) => {
  const { value: e, step: s, min: n, max: r, onBlur: a, onChange: o, onInput: i } = t, c = p.map(e, (d) => {
    if (d != null) return d;
    const f = n != null ? p.get(n) : void 0;
    return typeof f == "number" ? f : 0;
  }), l = z(
    e,
    t.disabled,
    o ?? i
  );
  return E({
    ...t,
    focusableSelector: 'input[type="range"]',
    after: l,
    input: k.input(
      u.type("range"),
      U(t),
      u.min(n),
      u.max(r),
      u.step(s),
      u.valueAsNumber(c),
      u.class("bc-input bc-slider-input"),
      a != null ? M.blur(V(a)) : A,
      o != null ? M.change(
        V((d) => {
          o(d);
        })
      ) : A,
      i != null ? M.input(
        V((d) => {
          i(d);
        })
      ) : A
    )
  });
}, Me = L(mt);
class Gt {
  widgets = /* @__PURE__ */ new Map();
  typeMapping = /* @__PURE__ */ new Map();
  // JSON Schema type -> widget names
  /**
   * Register a custom widget
   */
  register(e, s) {
    if (this.widgets.set(e, s), s.supportedTypes)
      for (const n of s.supportedTypes)
        this.typeMapping.has(n) || this.typeMapping.set(n, []), this.typeMapping.get(n).push(e), this.typeMapping.get(n).sort((r, a) => {
          const o = this.widgets.get(r)?.priority || 0;
          return (this.widgets.get(a)?.priority || 0) - o;
        });
  }
  /**
   * Unregister a widget
   */
  unregister(e) {
    const s = this.widgets.get(e);
    if (s && (this.widgets.delete(e), s.supportedTypes))
      for (const n of s.supportedTypes) {
        const r = this.typeMapping.get(n);
        if (r) {
          const a = r.indexOf(e);
          a >= 0 && r.splice(a, 1), r.length === 0 && this.typeMapping.delete(n);
        }
      }
  }
  /**
   * Get a registered widget
   */
  get(e) {
    return this.widgets.get(e);
  }
  /**
   * Get all registered widgets
   */
  getAll() {
    return new Map(this.widgets);
  }
  /**
   * Get widgets that support a specific JSON Schema type
   */
  getForType(e) {
    return (this.typeMapping.get(e) || []).map((n) => this.widgets.get(n)).filter(Boolean);
  }
  /**
   * Find the best widget for a schema context
   */
  findBestWidget(e) {
    const s = kt(e.definition, e.name);
    if (s?.widget) {
      const r = this.widgets.get(s.widget);
      if (r)
        return { name: s.widget, registration: r, resolved: s };
    }
    const n = [];
    for (const [r, a] of this.widgets.entries())
      if (a.matcher)
        try {
          a.matcher(e) && n.push({
            name: r,
            registration: a,
            priority: a.priority ?? 0
          });
        } catch (o) {
          console.warn(`Error in matcher for widget "${r}":`, o);
        }
    if (n.length > 0) {
      n.sort((a, o) => o.priority - a.priority);
      const r = n[0];
      return {
        name: r.name,
        registration: r.registration,
        resolved: { widget: r.name, source: "heuristics" }
      };
    }
    if (typeof e.definition == "object" && e.definition.type) {
      const r = Array.isArray(e.definition.type) ? e.definition.type[0] : e.definition.type, a = this.getForType(r);
      if (a.length > 0) {
        const o = this.typeMapping.get(r)[0];
        return {
          name: o,
          registration: a[0],
          resolved: { widget: o, source: "type-fallback" }
        };
      }
    }
    return null;
  }
}
const $e = new Gt();
function xe(t, e, s) {
  return {
    factory: e,
    matcher: (n) => {
      const a = n.definition["x:ui"];
      return a === t || typeof a == "object" && a !== null && a.widget === t;
    },
    displayName: s?.displayName ?? t,
    priority: s?.priority ?? 100,
    // High priority for explicit matches
    ...s
  };
}
function _e(t, e, s) {
  return {
    factory: e,
    matcher: (n) => n.definition.format === t,
    displayName: s?.displayName ?? `${t} widget`,
    priority: s?.priority ?? 75,
    ...s
  };
}
function ke(t, e, s, n) {
  return {
    factory: s,
    matcher: (r) => {
      const a = r.definition;
      return a.type === t && a.format === e;
    },
    displayName: n?.displayName ?? `${t}:${e} widget`,
    priority: n?.priority ?? 80,
    ...n
  };
}
function Fe(t) {
  const e = t?.logPrefix ?? "WIDGET_DIAG", s = t?.filterFn ?? (() => !0);
  return {
    factory: () => null,
    displayName: "Diagnostic Widget (never matches)",
    priority: -1e3,
    // Very low priority, should never actually match
    matcher: (n) => {
      if (!s(n)) return !1;
      const r = n.definition, a = {
        name: n.name,
        path: n.path.map(String),
        type: r?.type,
        hasRegistry: n.widgetRegistry !== void 0,
        definition: r
      };
      return console.log(
        `[${e}] name="${a.name ?? "ROOT"}" path=[${a.path.join(", ")}] type="${a.type ?? "unknown"}" hasRegistry=${a.hasRegistry}`
      ), t?.onProcess?.(a), !1;
    }
  };
}
export {
  H as A,
  ie as B,
  ce as C,
  oe as D,
  le as E,
  St as F,
  vt as G,
  Rt as H,
  ct as I,
  Nt as J,
  J as K,
  Dt as L,
  G as M,
  he as N,
  K as O,
  Bt as P,
  Vt as Q,
  Yt as R,
  ye as S,
  P as T,
  O as U,
  Gt as W,
  fe as a,
  be as b,
  Me as c,
  ge as d,
  pe as e,
  ue as f,
  $e as g,
  de as h,
  Ie as i,
  we as j,
  me as k,
  xe as l,
  _e as m,
  ke as n,
  Fe as o,
  at as p,
  jt as q,
  re as r,
  it as s,
  At as t,
  Et as u,
  Ot as v,
  Ft as w,
  Ct as x,
  Tt as y,
  Q as z
};
