import { jsx as o, jsxs as N } from "react/jsx-runtime";
import { forwardRef as S, useRef as E, useState as O, useLayoutEffect as R } from "react";
import { noop as P } from "es-toolkit/function";
import m from "../../utils/classNames.js";
import { getCurrentBackgroundColor as D } from "../../utils/currentColors.js";
const f = 40, H = 28, U = ({ enabledText: d, disabledText: h, minWidth: n, autoMinWidth: x }) => {
  const t = E(null), [p, u] = O(f), c = n !== void 0, e = [d, h].filter((s) => !!s).reduce(
    (s, i) => i.length > s.length ? i : s,
    ""
  ), l = !!(d || h), a = !c && x, r = c ? n : a ? p : l ? void 0 : f;
  return R(() => {
    if (!a)
      return;
    if (!e) {
      u(f);
      return;
    }
    const s = t.current?.offsetWidth ?? 0, i = Math.ceil(s) + H;
    u(Math.max(f, i));
  }, [e, a]), { computedMinWidth: r, longestText: e, shouldUseAutoTextWidth: a, textMeasureRef: t };
}, M = S((d, h) => {
  const {
    keyName: n,
    checked: x = !1,
    enabledText: t,
    minWidth: p,
    autoMinWidth: u = !1,
    disabled: c = !1,
    color: T = "primary",
    disabledText: e,
    children: l,
    className: a = "",
    labelPosition: r = l ? "right" : void 0,
    onChange: s = P,
    customTextStyle: i = !1,
    ...W
  } = d, { computedMinWidth: C, longestText: g, shouldUseAutoTextWidth: v, textMeasureRef: A } = U({
    enabledText: t,
    disabledText: e,
    minWidth: p,
    autoMinWidth: u
  }), I = (k) => {
    s(k.target.checked);
  }, L = !!(t && e), y = !!(t && !e), w = m("uikit-switch", a), B = m(
    "switch-label",
    w,
    c && "cursor-not-allowed",
    r && `label-position-${r}`,
    r === "left" && "display-inline-flex height-20"
  ), _ = m("switch-content"), b = D(T);
  return /* @__PURE__ */ o("div", { ref: h, ...W, className: w, children: /* @__PURE__ */ N("label", { className: B, children: [
    v && g && /* @__PURE__ */ o("span", { ref: A, className: "switch-text-measure", "aria-hidden": "true", children: g }),
    /* @__PURE__ */ o(
      "input",
      {
        onChange: I,
        checked: x,
        type: "checkbox",
        id: n,
        className: "switch-input",
        disabled: c
      },
      n
    ),
    /* @__PURE__ */ N(
      "div",
      {
        className: _,
        style: { color: b, minWidth: C },
        children: [
          L ? /* @__PURE__ */ o("div", { className: "switch-text", "data-on": t, "data-off": e }) : y && /* @__PURE__ */ o("div", { className: "switch-text", "data-on": t, "data-off": t }),
          /* @__PURE__ */ o("div", { className: "switch-handle" })
        ]
      }
    ),
    l && /* @__PURE__ */ o(
      "div",
      {
        className: m(
          "switch-text-wrapper",
          "user-select-none",
          i ? "" : "padding-top-1"
        ),
        children: l
      }
    )
  ] }) });
});
M.LABEL_POSITION_LEFT = "left";
M.LABEL_POSITION_RIGHT = "right";
export {
  M as default
};
//# sourceMappingURL=Switch.js.map
