import { Task as xe, Use as F, Value as f, prop as ce, html as h, attr as l, on as C, WithElement as ee, Empty as T, aria as G, bind as re, computedOf as V, style as ye, OnDispose as te, When as U, NotEmpty as Fe, Fragment as E, ForEach as K, coalesce as Re, Ensure as Ne, OneOfType as Pe, Repeat as je, signal as we, input as ne, emitValue as P, emitValueAsNumber as _e } from "@tempots/dom";
import { B as R, b as ke, f as Ce, I as O, d as oe } from "./translations-BGblCUBj.js";
import { l as We } from "./colors-BmLYKmoH.js";
import { g as Ze } from "./timer-Rd2sKnvH.js";
import { ElementRect as He } from "@tempots/ui";
import { I as W, a as qe, g as Ye, B as me } from "./input-container-DhUiLmXE.js";
import { C as ie, m as Ge, c as Je, I as Be, d as ue, O as Q, e as De } from "./use-form-BSvvERhX.js";
import { C as q } from "./text-input-BZmAB3_o.js";
import { S as Ve } from "./stack-BJgsPJ9A.js";
let fe = null;
async function be() {
  const t = globalThis;
  return t.Temporal ? t.Temporal : (fe || (fe = import("./index.esm-ChUrm4xR.js").then((e) => {
    const n = e.Temporal;
    return t.Temporal || (t.Temporal = n), n;
  })), fe);
}
const Xe = (t, e) => e ? xe(be, {
  then: t,
  pending: e.pending,
  error: e.error
}) : xe(be, t);
function pe({
  onChange: t,
  value: e,
  accept: n = "*/*",
  enableClick: s = !0,
  content: r,
  disabled: b = !1,
  allowMultiple: i
}) {
  return F(R, (g) => {
    const p = f.deriveProp(e ?? []), v = ce(!1), d = (u) => {
      u.preventDefault(), u.stopPropagation(), v.value = !0;
    }, m = (u) => {
      u.preventDefault(), u.stopPropagation(), (!u.currentTarget || !u.currentTarget.contains(u.relatedTarget)) && (v.value = !1);
    }, y = (u) => {
      u.preventDefault(), u.stopPropagation(), v.value = !1;
      const x = Array.from(u.dataTransfer?.files || []);
      x.length > 0 && (p.value = x, t(x, "dragdrop"));
    }, o = (u) => {
      f.get(s) && u.currentTarget.querySelector(
        'input[type="file"]'
      )?.click();
    };
    let a = null;
    const D = () => {
      if (a == null) return;
      const u = Array.from(a.files ?? []);
      u.length > 0 && (p.value = u, t(u, "click")), a.value = "";
    }, $ = (u) => {
      f.get(s) && (u.key === "Enter" || u.key === " ") && (u.preventDefault(), o(u));
    };
    return h.div(
      l.role("button"),
      l.tabindex(
        f.map(s, (u) => u ? 0 : -1)
      ),
      l.style("position: relative;"),
      C.dragover(d),
      C.dragleave(m),
      C.drop(y),
      C.click(o),
      C.keydown($),
      // Hidden file input
      h.input(
        l.type("file"),
        l.disabled(b),
        l.accept(n),
        l.multiple(i),
        l.style(
          "position: absolute; left: -9999px; opacity: 0; pointer-events: none;"
        ),
        C.change(D),
        ee((u) => (a = u, p.on((x) => {
          const M = globalThis.DataTransfer;
          if (M != null) {
            const I = new M();
            x.forEach((A) => I.items.add(A)), u.files = I.files;
          }
        }), T))
      ),
      // Screen reader instructions
      G.label(re(g.$.dropZoneInstructions)(s)),
      // Content
      r({ files: p, clear: () => p.value = [], change: p.set })
    );
  });
}
function Qe(t, e) {
  return t.length === e.length && t.every((n, s) => n === e[s]);
}
function Ke(t, e, n) {
  const s = ["bc-segmented-input", `bc-segmented-input--size-${t}`];
  return n === "squared" && s.push("bc-segmented-input--squared"), e && s.push("bc-segmented-input--disabled"), s.join(" ");
}
function et(t) {
  if (t == null) return "";
  const e = /* @__PURE__ */ new Map(), n = ke(t, "solid", "light"), s = ke(t, "solid", "dark");
  return e.set("--si-indicator-bg", n.backgroundColor), e.set("--si-active-text", n.textColor), e.set("--si-indicator-bg-dark", s.backgroundColor), e.set("--si-active-text-dark", s.textColor), Array.from(e.entries()).map(([r, b]) => `${r}: ${b}`).join("; ");
}
function It({
  options: t,
  value: e,
  onChange: n,
  size: s = "md",
  disabled: r = !1,
  variant: b = "pill",
  color: i
}, ...g) {
  const p = We(t).map(([v, d]) => ({
    key: v,
    label: d
  }));
  return ee(() => {
    const v = Object.fromEntries(
      p.map((m, y) => [m.key, y])
    ), d = ce(
      p.map(() => ({ left: 0, width: 0 })),
      Qe
    );
    return h.div(
      l.class(
        V(
          s,
          r,
          b
        )(
          (m, y, o) => Ke(
            m ?? "md",
            y ?? !1,
            o ?? "pill"
          )
        )
      ),
      l.style(
        i != null ? f.map(i, (m) => et(m)) : ""
      ),
      h.div(
        l.class("bc-segmented-input__container"),
        h.div(
          l.class("bc-segmented-input__indicator"),
          ye.width(
            V(
              e,
              d
            )((m, y) => {
              const { width: o } = y[v[m] ?? 0];
              return `${o}px`;
            })
          ),
          ye.left(
            V(
              e,
              d
            )((m, y) => {
              const { left: o } = y[v[m] ?? 0];
              return `${o}px`;
            })
          )
        ),
        // clickable buttons
        p.map(({ label: m, key: y }, o) => h.button(
          l.type("button"),
          C.click((a) => {
            a.preventDefault(), f.get(r) || n?.(y);
          }),
          l.disabled(r),
          l.class("bc-segmented-input__segment"),
          l.class(
            f.map(e, (a) => a === y ? "bc-segmented-input__segment--active" : "bc-segmented-input__segment--inactive")
          ),
          He((a) => {
            function D() {
              d.update((u) => {
                const x = [...u];
                return x[o] = {
                  width: a.value.width,
                  left: a.value.localLeft
                }, x;
              });
            }
            const $ = Ze(D);
            return te($, a.on(D));
          }),
          m
        ))
      ),
      ...g
    );
  });
}
function tt(t) {
  const e = t.type.toLowerCase();
  return e.startsWith("image/") && (e.includes("jpeg") || e.includes("jpg") || e.includes("png") || e.includes("gif") || e.includes("webp") || e.includes("svg"));
}
function nt(t) {
  const e = t.type.toLowerCase();
  return e.startsWith("image/") ? "vscode-icons:file-type-image" : e.startsWith("video/") ? "vscode-icons:file-type-video" : e.startsWith("audio/") ? "vscode-icons:file-type-audio" : e.includes("pdf") ? "vscode-icons:file-type-pdf2" : e.includes("word") || e.includes("document") ? "vscode-icons:file-type-word" : e.includes("excel") || e.includes("spreadsheet") ? "vscode-icons:file-type-excel" : e.includes("powerpoint") || e.includes("presentation") ? "vscode-icons:file-type-powerpoint" : e.includes("zip") || e.includes("archive") ? "vscode-icons:file-type-zip" : e.includes("text") ? "vscode-icons:file-type-text" : e.includes("json") ? "vscode-icons:file-type-json-official" : e.includes("csv") ? "vscode-icons:file-type-csv" : e.includes("xml") ? "vscode-icons:file-type-xml" : e.includes("yaml") ? "vscode-icons:file-type-yaml-official" : "vscode-icons:file-type-binary";
}
function de(t) {
  return U(
    t.map(tt),
    () => {
      const e = ce(null);
      return t.on((n) => {
        e.value && URL.revokeObjectURL(e.value);
        const s = URL.createObjectURL(n);
        e.value = s;
      }), h.div(
        te(() => {
          e.value && URL.revokeObjectURL(e.value);
        }),
        l.class("bc-file-input__thumbnail-container"),
        h.img(
          l.src(e),
          l.alt(t.map((n) => n.name)),
          l.class("bc-file-input__thumbnail")
        )
      );
    },
    () => O({ icon: t.map(nt) })
  );
}
const $t = (t, ...e) => {
  const {
    value: n = ce([]),
    accept: s = "*/*",
    maxFiles: r,
    maxFileSize: b,
    onChange: i,
    onBlur: g,
    disabled: p,
    hasError: v,
    mode: d = "default",
    showFileList: m = !0,
    ...y
  } = t, o = n, a = f.map(d, (k) => k === "input"), D = f.map(d, (k) => k === "compact"), $ = (k) => {
    let c = k;
    if (r != null) {
      const w = f.get(r);
      c = c.slice(0, w);
    }
    if (b) {
      const w = f.get(b);
      c = c.filter((S) => S.size <= w);
    }
    i?.(c);
  }, u = (k) => {
    const w = o.value.filter((S, z) => z !== k);
    i?.(w);
  }, x = () => {
    i?.([]);
  }, M = ({
    files: k,
    clear: c,
    change: w
  }) => F(
    R,
    (S) => h.div(
      l.class("bc-file-input__drop-zone"),
      h.div(
        l.class(
          "bc-file-input__drop-zone-content bc-file-input__drop-zone-content--empty"
        ),
        O({ icon: "mdi:cloud-upload-outline", size: "xl" }),
        h.div(
          l.class("bc-file-input__drop-zone-text"),
          re(S.$.filesInputInstructions)(
            r,
            b,
            S.$.fileSizeUnits.value
          )
        )
      )
    )
  ), I = ({
    files: k
  }) => F(
    R,
    (c) => h.div(
      l.class("bc-file-input__compact-drop-zone"),
      U(
        k.map((w) => w.length > 0),
        () => h.div(
          l.class("bc-file-input__compact-file-list"),
          K(k, (w, S) => {
            const z = S.index;
            return h.div(
              l.class("bc-file-input__compact-file-item"),
              h.div(
                l.class("bc-file-input__compact-file-icon"),
                de(w)
              ),
              h.div(
                l.class("bc-file-input__compact-file-info"),
                h.div(
                  l.class("bc-file-input__compact-file-name"),
                  l.title(w.$.name),
                  w.$.name
                ),
                h.div(
                  l.class("bc-file-input__compact-file-meta"),
                  V(
                    w.$.size,
                    c.$.fileSizeUnits
                  )((_, B) => Ce(_, { units: B })),
                  " • ",
                  V(
                    w.$.type,
                    c.$.unknownType
                  )((_, B) => _ || B)
                )
              ),
              ie(
                {
                  size: "sm",
                  label: c.$.removeFile,
                  disabled: p,
                  onClick: () => u(z)
                },
                l.class("bc-file-input__compact-remove-button")
              )
            );
          })
        ),
        () => h.div(
          l.class("bc-file-input__compact-placeholder"),
          O({ icon: "mdi:cloud-upload-outline", size: "lg" }),
          h.div(
            l.class("bc-file-input__compact-placeholder-text"),
            re(c.$.filesInputInstructions)(
              r,
              b,
              c.$.fileSizeUnits.value
            )
          )
        )
      )
    )
  ), A = ({
    files: k
  }) => F(
    R,
    (c) => h.div(
      l.class("bc-file-input__compact-input"),
      U(
        k.map((w) => w.length > 0),
        () => h.span(
          l.class("bc-file-input__compact-value"),
          K(
            k,
            (w) => h.span(
              l.class("bc-file-input__compact-value-item"),
              de(w),
              h.span(
                l.class("bc-file-input__compact-value-item-name"),
                w.$.name
              )
            )
          )
        ),
        () => h.span(
          l.class("bc-file-input__compact-placeholder"),
          O({ icon: "mdi:cloud-upload-outline", size: "md" }),
          " ",
          re(c.$.filesInputInstructions)(
            r,
            b,
            c.$.fileSizeUnits.value
          )
        )
      )
    )
  );
  return F(
    R,
    (k) => W(
      {
        baseContainer: f.map(a, (c) => !c),
        disabled: p,
        hasError: v,
        after: U(
          a,
          () => U(
            o.map(({ length: c }) => c > 0),
            () => ie(
              {
                size: "sm",
                label: k.$.clearAllFiles,
                disabled: p,
                onClick: x
              },
              l.class("bc-file-input__compact-clear")
            )
          )
        ),
        ...y,
        input: U(
          a,
          () => h.div(
            l.class("bc-file-input bc-file-input--input"),
            pe({
              value: o,
              accept: s,
              enableClick: !0,
              allowMultiple: f.map(r ?? 1 / 0, (c) => c > 1),
              disabled: p,
              onChange: $,
              content: A
            })
          ),
          () => U(
            D,
            () => h.div(
              l.class("bc-file-input bc-file-input--compact"),
              pe({
                value: o,
                accept: s,
                enableClick: !0,
                allowMultiple: f.map(r ?? 1 / 0, (c) => c > 1),
                disabled: p,
                onChange: $,
                content: I
              })
            ),
            () => h.div(
              l.class("bc-file-input"),
              pe({
                value: o,
                accept: s,
                enableClick: !0,
                allowMultiple: f.map(r ?? 1 / 0, (c) => c > 1),
                disabled: p,
                onChange: $,
                content: M
              }),
              U(
                m,
                () => Fe(
                  o,
                  () => E(
                    h.div(
                      l.class("bc-file-input__file-list"),
                      K(o, (c, w) => {
                        const S = w.index;
                        return h.div(
                          l.class("bc-file-input__file-item"),
                          h.div(
                            l.class("bc-file-input__file-icon"),
                            de(c)
                          ),
                          h.div(
                            l.class("bc-file-input__file-info"),
                            h.div(
                              l.class("bc-file-input__file-name"),
                              l.title(c.$.name),
                              c.$.name
                            ),
                            h.div(
                              l.class("bc-file-input__file-meta"),
                              V(
                                c.$.size,
                                k.$.fileSizeUnits
                              )(
                                (z, _) => Ce(z, { units: _ })
                              ),
                              " • ",
                              V(
                                c.$.type,
                                k.$.unknownType
                              )((z, _) => z || _)
                            )
                          ),
                          ie(
                            {
                              size: "sm",
                              label: k.$.removeFile,
                              disabled: p,
                              onClick: () => u(S)
                            },
                            l.class("bc-file-input__remove-button")
                          )
                        );
                      })
                    ),
                    U(
                      o.map(({ length: c }) => c > 1),
                      () => h.div(
                        l.class(
                          "bc-file-input__clear-all-button-container"
                        ),
                        h.button(
                          l.type("button"),
                          l.class("bc-file-input__clear-all-button"),
                          l.disabled(p),
                          k.$.clearAllFiles,
                          C.click((c) => {
                            c.preventDefault(), c.stopPropagation(), x();
                          })
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )
      },
      ...e
    )
  );
}, he = "$$tts-exp-", st = (t, e) => ee((n) => {
  const s = `${he}${t}`;
  return te(f.on(e, (r) => Reflect.set(n, s, r)));
}), Mt = (t, e) => {
  const n = `${he}${t}`;
  return (s) => {
    e(Reflect.get(s.target, n));
  };
}, lt = (t, e) => {
  const n = `${he}${t}`;
  return (s) => {
    const r = s.target, b = r.selectedIndex, i = r.options[b];
    e(Reflect.get(i, n));
  };
};
function Ie(...t) {
  return h.div(l.class("bc-group"), ...t);
}
const Ae = (t, e, n) => Ne(
  t,
  (s) => Pe(s, {
    value: (r) => {
      const b = V(
        r,
        n
      )((i, g) => e(i.value, g));
      return h.option(
        l.selected(b),
        st("value", r.$.value),
        r.$.label
      );
    },
    group: (r) => h.optgroup(
      l.label(r.$.group),
      K(
        r.$.options,
        (b) => Ae(
          b,
          e,
          n
        )
      )
    ),
    break: () => h.hr()
  })
), rt = (t) => {
  const {
    value: e,
    onBlur: n,
    onChange: s,
    options: r,
    unselectedLabel: b,
    equality: i = (v, d) => v === d,
    after: g
  } = t;
  let p;
  return W(
    {
      ...t,
      after: E(
        qe({
          icon: "ph:caret-down-bold",
          color: "neutral",
          size: "sm"
        }),
        g
      ),
      input: h.select(
        ee((v) => {
          p = v;
          const d = new MutationObserver((m) => {
            const { removedNodes: y } = m[0];
            y.length > 0 && (p.selectedIndex = 0);
          });
          return d.observe(v, { childList: !0 }), te(() => d.disconnect());
        }),
        q(t),
        l.class("bc-native-select bc-input"),
        F(
          R,
          (v) => h.option(
            l.hidden("hidden"),
            Re(b, v.$.selectOne)
          )
        ),
        K(r, (v) => Ae(v, i, e)),
        n != null ? C.blur(n) : T,
        s != null ? C.change(lt("value", (v) => s(v))) : T
      )
    },
    C.click(() => {
      if (p?.focus(), typeof p?.showPicker == "function")
        try {
          p.showPicker();
        } catch {
        }
    })
  );
};
function it(t) {
  const { controller: e, onChange: n, onBlur: s, ...r } = t;
  return rt({
    ...r,
    value: e.signal,
    onChange: Je(e, n),
    onBlur: Ge(e, s)
  });
}
function Tt(t) {
  return Be({
    ...t,
    content: it(t)
  });
}
function ot(t, e, n) {
  const s = t.length.map((r) => r);
  return E(
    je(
      s,
      (r) => {
        const b = t.item(r.index), i = [];
        return E(
          te(() => {
            i.forEach((g) => g());
          }),
          e({
            list: t,
            item: b,
            position: r,
            remove: () => t.removeAt(r.index),
            move: (g) => {
              switch (g) {
                case "up":
                  if (r.index === 0) return;
                  t.move(r.index, r.index - 1);
                  break;
                case "down":
                  if (r.index === t.length.value - 1) return;
                  t.move(r.index, r.index + 1);
                  break;
                case "first":
                  t.move(r.index, 0);
                  break;
                case "last":
                  t.move(r.index, t.length.value - 1);
              }
            },
            canMove: (g) => {
              const p = (() => {
                switch (g) {
                  case "up":
                    return we(r.index > 0);
                  case "down":
                    return t.length.map((v) => r.index < v - 1);
                }
              })();
              return i.push(() => p.dispose()), p;
            },
            cannotMove: (g) => {
              const p = (() => {
                switch (g) {
                  case "up":
                    return we(r.index === 0);
                  case "down":
                    return t.length.map((v) => r.index === v - 1);
                }
              })();
              return i.push(() => p.dispose()), p;
            }
          })
        );
      },
      n
    )
  );
}
const $e = {
  9: { pattern: /^[0-9]$/ },
  A: { pattern: /^[A-Za-z]$/, transform: (t) => t.toUpperCase() },
  "*": { pattern: /^.$/ }
}, ct = (t) => t instanceof RegExp;
function Le(t, e, n) {
  if (t == null) return [];
  const s = n ? { ...$e, ...e } : e ?? $e, r = (i, g) => {
    const p = s[g];
    p ? i.push({ type: "pattern", name: g, ...p }) : i.push({ type: "literal", char: g });
  }, b = [];
  if (typeof t == "string") {
    for (const i of t) r(b, i);
    return b;
  }
  for (const i of t)
    if (typeof i == "string")
      if (i.length <= 1) r(b, i);
      else for (const g of i) r(b, g);
    else ct(i) ? b.push({ type: "pattern", pattern: i }) : typeof i == "object" && i && b.push(i);
  return b;
}
function at(t, e) {
  return (n) => {
    switch (t) {
      case "digits":
        return /[0-9]/.test(n);
      case "letters":
        return /[A-Za-z]/.test(n);
      case "alphanumeric":
        return /[A-Za-z0-9]/.test(n);
      case "custom":
        return e?.(n) ?? !0;
      default:
        return !0;
    }
  };
}
function Me(t, e, n, s, r, b) {
  const i = {
    raw: t,
    previousConformed: e,
    cursor: r ?? t.length,
    completed: !1
  }, g = s.definitions ?? {}, p = typeof n == "function" ? n(t, i) : n, v = Le(
    p,
    g,
    s.useDefaultDefinitions ?? !0
  ), d = at(s.allowMode, s.allow), m = /* @__PURE__ */ new Set();
  for (const _ of v) _.type === "literal" && m.add(_.char);
  if (s.prefix) for (const _ of s.prefix) m.add(_);
  if (s.suffix) for (const _ of s.suffix) m.add(_);
  const y = Array.from(t).filter((_) => d(_) && !m.has(_));
  if (y.length === 0) return { value: "", cursor: 0, completed: !1 };
  const o = [], a = [];
  let D = 0, $ = 0, u = 0, x = -1;
  for (const _ of v) {
    if (_.type === "literal") {
      o.push(_.char), a.push({ kind: "literal", filled: !0 });
      continue;
    }
    $ += _.optional ? 0 : 1;
    const B = y[D];
    if (B == null)
      break;
    if (_.type === "any") {
      u++, o.push(_.transform ? _.transform(B) : B), a.push({ kind: "slot", filled: !0 }), x = a.length - 1, D++;
      continue;
    }
    if (_.type === "pattern")
      if (_.pattern.test(B)) {
        u++;
        const N = _.transform ? _.transform(B) : B;
        o.push(N), a.push({ kind: "slot", filled: !0 }), x = a.length - 1, D++;
      } else {
        D++;
        continue;
      }
  }
  const M = o.join(""), I = (s.prefix ?? "") + M + (s.suffix ?? "");
  let k = (s.prefix ?? "").length;
  if (x >= 0) {
    let _ = x + 1;
    if ((b?.policy ?? "smart") !== "sticky")
      for (; _ < a.length && a[_].kind === "literal"; ) _++;
    k += _;
  }
  const c = s.completion?.mode === "min" ? (s.completion.minChars ?? 0) <= u : s.completion?.mode === "custom" ? !!s.completion.isComplete?.(I) : $ > 0 && u >= $, w = s.pipe?.(I, { ...i, completed: c });
  let S, z = k;
  return w === !1 ? (S = e, z = e.length) : typeof w == "string" ? (S = w, z = w.length) : typeof w == "object" && w ? (S = w.value, z = w.cursor ?? w.value.length) : S = I, { value: S, cursor: z, completed: c };
}
function ut(t, e) {
  const n = e?.strategy ?? "none";
  return n === "custom" && e?.unmask ? e.unmask(t) : n === "strip" ? t.replace(/[^A-Za-z0-9]/g, "") : t;
}
const Ue = (t) => {
  const {
    value: e,
    onBlur: n,
    onChange: s,
    onInput: r,
    onAccept: b,
    onComplete: i,
    mask: g,
    definitions: p,
    useDefaultDefinitions: v,
    extraLiterals: d,
    prefix: m,
    suffix: y,
    autofix: o,
    pipe: a,
    completion: D,
    unmask: $,
    allowMode: u,
    allow: x,
    placeholder: M,
    placeholderOptions: I
  } = t, A = V(
    e,
    g,
    p,
    v,
    d,
    m,
    y,
    o,
    D,
    $,
    u,
    M,
    I
  )((c, w, S, z, _, B, N, J, X, Y, Z, se, j) => w ? Me(c ?? "", "", w, {
    definitions: S ?? {},
    useDefaultDefinitions: z ?? !0,
    extraLiterals: _ ?? [],
    autofix: J ?? "none",
    completion: X ?? { mode: "mask" },
    pipe: a ?? ((L) => L),
    unmask: Y ?? { strategy: "none" },
    allowMode: Z ?? "all",
    allow: x,
    prefix: B,
    suffix: N
  }).value : c ?? ""), k = (c, w) => {
    const S = c.value ?? "", z = f.get(e) ?? "", _ = g != null ? f.get(g) : null, B = p != null ? f.get(p) : void 0, N = v != null ? f.get(v) : void 0, J = d != null ? f.get(d) : void 0, X = o != null ? f.get(o) : void 0, Y = D != null ? f.get(D) : void 0, Z = $ != null ? f.get($) : void 0, se = u != null ? f.get(u) : void 0, j = m != null ? f.get(m) : void 0, L = y != null ? f.get(y) : void 0, {
      value: H,
      cursor: ae,
      completed: ge
    } = _ ? Me(
      S,
      z,
      _,
      {
        definitions: B ?? {},
        useDefaultDefinitions: N ?? !0,
        extraLiterals: J ?? [],
        autofix: X ?? "none",
        completion: Y ?? { mode: "mask" },
        pipe: a ?? ((Oe) => Oe),
        unmask: Z ?? { strategy: "none" },
        allowMode: se ?? "all",
        allow: x,
        prefix: j,
        suffix: L
      },
      c.selectionStart ?? S.length,
      t.cursor ? f.get(t.cursor) : void 0
    ) : { value: S, cursor: S.length, completed: !0 };
    if (H !== S) {
      c.value = H;
      try {
        c.setSelectionRange(ae, ae);
      } catch {
      }
    }
    const ve = ut(H, Z);
    b?.({
      raw: ve,
      conformed: H,
      completed: ge,
      cursor: ae
    }), w === "input" ? r?.(H) : (s?.(H), ge && i?.({ raw: ve, conformed: H }));
  };
  return E(
    W({
      ...t,
      input: ne.text(
        q(t),
        l.value(A),
        l.class("bc-input"),
        n != null ? C.blur(n) : T,
        ee(
          (c) => c instanceof HTMLInputElement ? E(
            C.input(() => k(c, "input")),
            C.change(() => k(c, "change")),
            C.keydown((w) => {
              if (w.key !== "Backspace" || !(t.cursor ? f.get(t.cursor)?.backspaceRubberBand ?? !0 : !0)) return;
              const z = c.selectionStart ?? 0, _ = c.selectionEnd ?? z;
              if (z !== _ || z <= 0) return;
              const B = c.value ?? "", N = g != null ? f.get(g) : null;
              if (!N) return;
              const J = p != null ? f.get(p) : void 0, X = v != null ? f.get(v) : !0, Y = m != null ? f.get(m) : void 0, Z = y != null ? f.get(y) : void 0, se = Le(
                typeof N == "function" ? N(B, {
                  raw: B,
                  previousConformed: B,
                  cursor: z,
                  completed: !1
                }) : N,
                J ?? {},
                X ?? !0
              ), j = /* @__PURE__ */ new Set();
              for (const L of se)
                L.type === "literal" && j.add(L.char);
              if (Y) for (const L of Y) j.add(L);
              if (Z) for (const L of Z) j.add(L);
              if (j.has(B[z - 1])) {
                let L = z - 1;
                for (; L >= 0 && j.has(B[L]); ) L--;
                L >= 0 && (w.preventDefault(), w.stopPropagation(), c.value = B.slice(0, L) + B.slice(L + 1), k(c, "input"));
              }
            })
          ) : T
        )
      )
    })
  );
};
function Ee(t, e, n) {
  const s = f.map(t, (i) => i != null), r = V(
    s,
    e ?? !1
  )((i, g) => !i || g), b = oe.clearValue;
  return h.button(
    l.type("button"),
    l.class("bc-input-container__reset"),
    G.label(b),
    l.title(b),
    l.disabled(r),
    O({ icon: "mdi:close", size: "sm" }),
    C.click((i) => {
      i.stopPropagation(), n?.(null);
    })
  );
}
const St = (t) => {
  const { value: e, onBlur: n, onChange: s, onInput: r, rows: b } = t;
  return W({
    baseContainer: !0,
    ...t,
    input: h.textarea(
      l.class(
        V(t.size ?? "md")(
          (i) => Ye(
            !1,
            i ?? "md"
          )
        )
      ),
      q(t),
      l.rows(b ?? 3),
      l.value(e),
      l.class("bc-input"),
      n != null ? C.blur(P(n)) : T,
      s != null ? C.change(P(s)) : T,
      r != null ? C.input(P(r)) : T
    )
  });
}, zt = (t) => {
  const { value: e, step: n, min: s, max: r, onBlur: b, onChange: i, onInput: g, after: p } = t, v = (y) => {
    const o = s != null ? f.get(s) : void 0, a = r != null ? f.get(r) : void 0;
    return o != null && y < o ? o : a != null && y > a ? a : y;
  }, d = n != null ? F(R, (y) => {
    const o = V(
      e,
      s
    )((u, x) => x == null ? !0 : (u ?? 0) > x), a = V(
      e,
      r
    )((u, x) => x == null ? !0 : (u ?? 0) < x), D = (u) => {
      const x = f.get(e) ?? 0, M = f.get(n), I = u?.shiftKey ? 10 : 1, A = x - M * I, k = s != null ? f.get(s) : void 0;
      if (k != null && A < k)
        return;
      const c = v(A);
      c !== x && i && i(c);
    }, $ = (u) => {
      const x = f.get(e) ?? 0, M = f.get(n), I = u?.shiftKey ? 10 : 1, A = x + M * I, k = r != null ? f.get(r) : void 0;
      if (k != null && A > k)
        return;
      const c = v(A);
      c !== x && i && i(c);
    };
    return Ve(
      l.class("bc-number-input-steppers"),
      // Increment second
      h.button(
        l.type("button"),
        l.class(
          "bc-button bc-number-input-steppers-button bc-number-input-steppers-button--increment"
        ),
        l.disabled(
          V(
            a,
            t.disabled ?? !1
          )((u, x) => !u || x)
        ),
        C.click((u) => $(u)),
        G.label(y.$.incrementValue),
        O({ icon: "line-md:plus", size: "xs" })
      ),
      // Decrement first (matches tests expecting first button to be decrement)
      h.button(
        l.type("button"),
        l.class(
          "bc-button bc-number-input-steppers-button bc-number-input-steppers-button--decrement"
        ),
        l.disabled(
          V(
            o,
            t.disabled ?? !1
          )((u, x) => !u || x)
        ),
        C.click((u) => D(u)),
        G.label(y.$.decrementValue),
        O({ icon: "line-md:minus", size: "xs" })
      )
    );
  }) : null, m = p != null && d != null ? E(d, p) : p ?? d;
  return W({
    ...t,
    input: ne.number(
      s != null ? l.min(s) : T,
      r != null ? l.max(r) : T,
      q(t),
      l.valueAsNumber(e),
      l.step(n),
      l.class("bc-input bc-number-input"),
      b != null ? C.blur(P(b)) : T,
      i != null ? C.change(_e(i)) : T,
      g != null ? C.input(_e(g)) : T,
      // Add wheel event support when step is defined
      n != null ? C.wheel((y) => {
        y.preventDefault();
        const o = f.get(e) ?? 0, a = f.get(n), D = y.shiftKey ? 10 : 1, $ = y.deltaY < 0 ? a * D : -a * D, u = v(o + $);
        u !== o && i && i(u);
      }) : T
    ),
    after: m
  });
}, Te = (t) => {
  if (t == null || t === "") return null;
  const e = Number(t);
  return Number.isNaN(e) ? null : e;
}, Bt = (t) => {
  const { value: e, step: n, min: s, max: r, onBlur: b, onChange: i, onInput: g, after: p } = t, v = (o) => {
    const a = s != null ? f.get(s) : void 0, D = r != null ? f.get(r) : void 0;
    return a != null && o < a ? a : D != null && o > D ? D : o;
  }, d = (() => {
    if (n == null) return null;
    const o = V(
      e,
      s
    )((x, M) => {
      const I = x ?? 0;
      return M == null ? !0 : I > M;
    }), a = V(
      e,
      r
    )((x, M) => {
      const I = x ?? 0;
      return M == null ? !0 : I < M;
    }), D = (x) => {
      const M = f.get(e) ?? 0, I = f.get(n), A = x?.shiftKey ? 10 : 1, k = M - I * A, c = s != null ? f.get(s) : void 0;
      if (c != null && k < c) return;
      const w = v(k);
      w !== M && i && i(w);
    }, $ = (x) => {
      const M = f.get(e) ?? 0, I = f.get(n), A = x?.shiftKey ? 10 : 1, k = M + I * A, c = r != null ? f.get(r) : void 0;
      if (c != null && k > c) return;
      const w = v(k);
      w !== M && i && i(w);
    };
    return ((x, M) => Ve(
      l.class("bc-number-input-steppers"),
      h.button(
        l.type("button"),
        l.class(
          "bc-button bc-number-input-steppers-button bc-number-input-steppers-button--increment"
        ),
        l.disabled(
          V(
            a,
            t.disabled ?? !1
          )((I, A) => !I || A)
        ),
        C.click((I) => $(I)),
        G.label(x),
        O({ icon: "line-md:plus", size: "xs" })
      ),
      h.button(
        l.type("button"),
        l.class(
          "bc-button bc-number-input-steppers-button bc-number-input-steppers-button--decrement"
        ),
        l.disabled(
          V(
            o,
            t.disabled ?? !1
          )((I, A) => !I || A)
        ),
        C.click((I) => D(I)),
        G.label(M),
        O({ icon: "line-md:minus", size: "xs" })
      )
    ))(
      oe.incrementValue,
      oe.decrementValue
    );
  })(), m = Ee(
    e,
    t.disabled,
    i ?? g
  ), y = p != null && d != null ? E(d, m, p) : p != null ? E(m, p) : d != null ? E(d, m) : m;
  return W({
    ...t,
    input: ne.number(
      // min/max attributes and reactive watchers (no forced correction)
      s != null ? l.min(s) : T,
      r != null ? l.max(r) : T,
      q(t),
      // Represent null as empty string so the field can be cleared
      l.value(f.map(e, (o) => o == null ? "" : String(o))),
      l.step(n),
      l.class("bc-input bc-number-input"),
      b != null ? C.blur(P(b)) : T,
      i != null ? C.change(
        P((o) => {
          const a = Te(o);
          i(a);
        })
      ) : T,
      g != null ? C.input(
        P((o) => {
          const a = Te(o);
          g(a);
        })
      ) : T,
      // Wheel support when step is defined
      n != null ? C.wheel((o) => {
        o.preventDefault();
        const a = f.get(e) ?? 0, D = f.get(n), $ = o.shiftKey ? 10 : 1, u = o.deltaY < 0 ? D * $ : -D * $, x = v(a + u);
        x !== a && i && i(x);
      }) : T
    ),
    after: y
  });
}, Vt = (t) => {
  const e = {
    type: "pattern",
    pattern: /[0-9A-Fa-f]/,
    transform: (s) => s.toLowerCase()
  };
  return Ue({
    ...t,
    mask: [
      e,
      e,
      e,
      e,
      e,
      e,
      e,
      e,
      "-",
      e,
      e,
      e,
      e,
      "-",
      e,
      e,
      e,
      e,
      "-",
      e,
      e,
      e,
      e,
      "-",
      e,
      e,
      e,
      e,
      e,
      e,
      e,
      e,
      e,
      e,
      e,
      e
    ],
    // Sensible default placeholder; can be overridden via options.placeholder
    placeholder: t.placeholder ?? "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  });
}, ft = /^P(\d+([YMWD]|$)){0,4}(T(\d+([HMS]|$)){0,3})?$/i, pt = /^P(?:(\d+Y)?(\d+M)?(\d+W)?(\d+D)?)(T(\d+H)?(\d+M)?(\d+S)?)?$/i;
function dt(t) {
  return {
    mask: null,
    allowMode: "custom",
    allow: (e) => /[0-9ptwdhms]/i.test(e),
    pipe: (e) => {
      const n = e.toUpperCase();
      return n.length === 0 ? n : !n.startsWith("P") || !ft.test(n) ? !1 : n;
    },
    completion: {
      mode: "custom",
      isComplete: (e) => {
        const n = e.toUpperCase();
        if (!pt.test(n)) return !1;
        try {
          return t(n), !0;
        } catch {
          return !1;
        }
      }
    }
  };
}
const Se = (t, e) => {
  if (t == null) return null;
  try {
    return t.Duration.from(e);
  } catch {
    return null;
  }
}, At = (t) => {
  const { value: e, onChange: n, onInput: s, after: r, disabled: b, onBlur: i } = t, g = () => {
    const d = Ee(e, b, n ?? s);
    return r != null ? E(d, r) : d;
  }, p = t.placeholder != null ? T : l.placeholder("P0DT0H0M0S");
  return Xe(
    (d) => Ue({
      ...t,
      value: f.map(e, (m) => m?.toString() ?? ""),
      onChange: n ? (m) => n(m === "" ? null : d.Duration.from(m)) : void 0,
      onInput: void 0,
      ...dt(d.Duration.from),
      placeholder: "P0DT0H0M0S",
      after: g()
    }),
    { pending: () => {
      const d = (m) => (y) => {
        const a = y.currentTarget?.value ?? "";
        if (a === "") {
          m(null);
          return;
        }
        const D = globalThis.Temporal, $ = Se(D, a);
        if ($ != null) {
          m($);
          return;
        }
        be().then((u) => {
          const x = Se(u, a);
          x != null && m(x);
        }).catch(() => {
        });
      };
      return W({
        ...t,
        input: ne.text(
          q(t),
          l.value(f.map(e, (m) => m?.toString() ?? "")),
          l.class("bc-input"),
          p,
          i != null ? C.blur(() => i()) : T,
          n != null ? C.change(d(n)) : T,
          s != null ? C.input(d(s)) : T
        ),
        after: g()
      });
    } }
  );
}, Lt = (t) => {
  const { value: e, onBlur: n, onChange: s, onInput: r } = t;
  return W({
    ...t,
    input: ne.url(
      q(t),
      l.value(e),
      l.class("bc-input"),
      n != null ? C.blur(P(n)) : T,
      s != null ? C.change(P(s)) : T,
      r != null ? C.input(P(r)) : T
    )
  });
};
function mt(t, e) {
  const {
    onMove: n,
    cannotMoveUp: s,
    cannotMoveDown: r,
    onRemove: b,
    showMove: i = !0,
    showRemove: g = !0,
    removeDisabled: p,
    showMoveButtons: v = !0,
    layout: d = "aside"
  } = t, m = f.toSignal(d).map((a) => a === "aside"), y = U(
    i,
    () => h.div(
      l.class("bc-list-item-controls__move"),
      me(
        {
          size: "xs",
          roundedness: "full",
          variant: "text",
          onClick: () => n("up"),
          disabled: s
        },
        F(
          R,
          (a) => O({
            size: "xs",
            icon: "line-md:arrow-up",
            title: a.$.incrementValue
          })
        )
      ),
      me(
        {
          size: "xs",
          roundedness: "full",
          variant: "text",
          onClick: () => n("down"),
          disabled: r
        },
        F(
          R,
          (a) => O({
            size: "xs",
            icon: "line-md:arrow-down",
            title: a.$.decrementValue
          })
        )
      )
    )
  ), o = U(
    g,
    () => F(
      R,
      (a) => ie({
        size: "xs",
        label: f.map(a.$.removeItem, (D) => D.toLowerCase()),
        color: "danger",
        disabled: p,
        onClick: b
      })
    )
  );
  return U(
    m,
    () => h.div(
      l.class("bc-list-item-controls bc-list-item-controls--aside"),
      h.div(l.class("bc-list-item-controls__content"), e),
      h.div(
        l.class("bc-list-item-controls__actions"),
        U(v, () => y),
        o
      )
    ),
    () => h.div(
      l.class("bc-list-item-controls bc-list-item-controls--below"),
      h.div(l.class("bc-list-item-controls__content"), e),
      h.div(
        l.class("bc-list-item-controls__actions"),
        U(
          v,
          () => y,
          () => h.div()
        ),
        o
      )
    )
  );
}
const bt = (t) => {
  const {
    controller: e,
    element: n,
    separator: s,
    showMove: r = !0,
    showRemove: b = !0,
    showAdd: i = !0,
    createItem: g,
    addLabel: p,
    controlsLayout: v = "aside",
    removeDisabled: d,
    addDisabled: m
  } = t, y = U(
    V(i, g)((o, a) => o && a != null),
    () => Ie(
      l.class(
        "bc-group--gap-2 bc-group--align-center bc-group--justify-center"
      ),
      me(
        {
          size: "sm",
          variant: "filled",
          onClick: () => e.push(g()),
          disabled: V(
            e.disabled,
            m ?? !1
          )(
            (o, a) => o || a
          )
        },
        F(
          R,
          (o) => Ie(
            l.class("bc-group--gap-2"),
            O({ icon: "line-md:plus" }),
            p ?? o.$.addLabel
          )
        )
      )
    )
  );
  return E(
    ot(
      e,
      (o) => mt(
        {
          onMove: o.move,
          cannotMoveUp: o.cannotMove("up"),
          cannotMoveDown: o.cannotMove("down"),
          onRemove: o.remove,
          showMove: r,
          showRemove: b,
          removeDisabled: d,
          showMoveButtons: e.signal.map((a) => a.length > 1),
          layout: v
        },
        n(o)
      ),
      s
    ),
    y
  );
}, Ut = (t, ...e) => {
  const {
    controller: n,
    element: s,
    separator: r,
    showMove: b,
    showRemove: i,
    showAdd: g,
    createItem: p,
    addLabel: v,
    controlsLayout: d,
    removeDisabled: m,
    addDisabled: y,
    ...o
  } = t;
  return Be(
    {
      ...o,
      content: bt({
        controller: n,
        element: s,
        separator: r,
        showMove: b,
        showRemove: i,
        showAdd: g,
        createItem: p,
        addLabel: v,
        controlsLayout: d,
        removeDisabled: m,
        addDisabled: y
      })
    },
    ...e
  );
};
class Et extends ue {
  /** The immutable list of branch definitions for this union */
  branches;
  /** Reactive signal containing the key of the currently active branch */
  activeBranch;
  /** @internal Reactive signal containing the controller for the active branch */
  #t;
  /** @internal Cache of lazily-created branch controllers */
  #e = /* @__PURE__ */ new Map();
  /**
   * Creates a new UnionController.
   *
   * @param path - The path segments identifying this controller in the form tree
   * @param change - Callback to propagate value changes to the parent
   * @param signal - Reactive signal holding the current union value
   * @param status - Reactive signal holding the current validation status
   * @param parent - Parent context providing disabled state and optional validation mode
   * @param branches - Array of branch definitions describing the possible value types
   * @param equals - Optional equality function for comparing values. Defaults to `strictEqual`.
   */
  constructor(e, n, s, r, b, i, g = Q) {
    super(e, n, s, r, b, g), this.branches = i;
    const p = (d) => {
      for (const m of i)
        if (m.detect(d))
          return m.key;
      return i[0]?.key ?? "unknown";
    };
    this.activeBranch = s.map(p, Q);
    const v = (d) => {
      if (this.#e.has(d))
        return this.#e.get(d);
      const m = i.find((o) => o.key === d);
      if (!m)
        throw new Error(`Unknown branch: ${d}`);
      const y = new ue(
        [...e, d],
        (o) => {
          this.change(o);
        },
        this.signal.map(
          (o) => m.detect(o) ? o : m.defaultValue(),
          g
        ),
        r.map(De([d])),
        {
          disabled: this.disabled,
          validationMode: this.parent.validationMode
        },
        g
      );
      return this.#e.set(d, y), y;
    };
    this.#t = this.activeBranch.map(
      (d) => v(d),
      Q
    ), this.onDispose(() => {
      for (const d of this.#e.values())
        d.dispose();
      this.#e.clear(), this.activeBranch.dispose(), this.#t.dispose();
    });
  }
  /**
   * Gets the controller for the currently active branch.
   *
   * The returned controller's value and type correspond to the branch that
   * matches the current union value.
   *
   * @returns The `Controller<unknown>` for the active branch
   */
  get activeController() {
    return f.get(this.#t);
  }
  /**
   * Gets or creates a controller for a specific branch by key.
   *
   * Controllers are created lazily and cached for subsequent calls.
   *
   * @param branchKey - The unique key of the branch to get a controller for
   * @returns The `Controller<unknown>` for the specified branch
   * @throws Error if the branch key is not found in the branches array
   */
  getBranchController(e) {
    const n = this.branches.find((r) => r.key === e);
    if (!n)
      throw new Error(`Unknown branch: ${e}`);
    if (this.#e.has(e))
      return this.#e.get(e);
    const s = new ue(
      [...this.path, e],
      (r) => {
        this.change(r);
      },
      this.signal.map(
        (r) => n.detect(r) ? r : n.defaultValue(),
        Q
      ),
      this.status.map(De([e])),
      {
        disabled: this.disabled,
        validationMode: this.parent.validationMode
      },
      Q
    );
    return this.#e.set(e, s), s;
  }
  /**
   * Switches the union value to a different branch.
   *
   * Attempts the following in order:
   * 1. If already on the target branch, returns `true` immediately
   * 2. If the branch has a `convert` function, tries to convert the current value
   * 3. If conversion fails and `confirmChange` is true, prompts the user with `window.confirm`
   * 4. Sets the value to the branch's default value
   *
   * @param branchKey - The key of the branch to switch to
   * @param confirmChange - If true, prompts the user before losing the current value when conversion fails
   * @returns `true` if the switch was successful, `false` if the user cancelled
   * @throws Error if the branch key is not found in the branches array
   */
  switchToBranch(e, n = !1) {
    const s = this.branches.find((i) => i.key === e);
    if (!s)
      throw new Error(`Unknown branch: ${e}`);
    const r = f.get(this.signal);
    if (s.detect(r))
      return !0;
    if (s.convert) {
      const i = s.convert(r);
      if (i.ok)
        return this.change(i.value), !0;
    }
    if (n && typeof window == "object" && typeof window.confirm == "function" && !window.confirm(oe.changeTypeConfirmation))
      return !1;
    const b = s.defaultValue();
    return this.change(b), !0;
  }
  /**
   * Gets the {@link UnionBranch} definition for the currently active branch.
   *
   * @returns The active branch definition, or `undefined` if no branch matches
   */
  get activeBranchDefinition() {
    const e = f.get(this.activeBranch);
    return this.branches.find((n) => n.key === e);
  }
}
function Ot(t) {
  return t.transform(
    (e) => e ?? null,
    (e) => e ?? void 0
  );
}
function Ft(t) {
  return t.transform(
    (e) => e ?? "",
    (e) => e === "" ? void 0 : e
  );
}
function Rt(t) {
  return t.transform(
    (e) => e ?? void 0,
    (e) => e ?? null
  );
}
function le(t) {
  return t !== null && typeof t == "object" && !Array.isArray(t) && Object.prototype.toString.call(t) === "[object Object]";
}
function ht(t, e) {
  if (e == null)
    return ze(t);
  if (t == null)
    return e;
  if (le(t) && le(e)) {
    const n = { ...e };
    for (const s of Object.keys(t))
      n[s] === void 0 ? n[s] = ze(t[s]) : le(t[s]) && le(n[s]) && (n[s] = ht(t[s], n[s]));
    return n;
  }
  return e;
}
function ze(t) {
  return typeof structuredClone == "function" ? structuredClone(t) : JSON.parse(JSON.stringify(t));
}
export {
  bt as B,
  st as E,
  $t as F,
  Ie as G,
  Ut as L,
  Ue as M,
  Ee as N,
  It as S,
  St as T,
  Vt as U,
  Xe as W,
  Lt as a,
  rt as b,
  At as c,
  dt as d,
  Ft as e,
  Bt as f,
  zt as g,
  Tt as h,
  Et as i,
  ht as j,
  pe as k,
  mt as l,
  Rt as m,
  ot as n,
  it as o,
  Mt as p,
  lt as q,
  be as r,
  Ot as t
};
