import { jsxs, jsx } from "react/jsx-runtime";
import * as React from "react";
import { forwardRef, useId } from "react";
import { c as composeEventHandlers } from "../../index-B-mNdDrH.js";
import { u as useComposedRefs } from "../../index-DcJYRxi-.js";
import { c as createContextScope } from "../../index-voAj_M6Q.js";
import { u as useControllableState } from "../../index-vuqbEv5O.js";
import { u as usePrevious } from "../../index-CshadhlS.js";
import { u as useSize } from "../../index-DzFPFHc9.js";
import { P as Primitive } from "../../index-BEpjRf6p.js";
import { c as cn } from "../../cn-FAaWYZ6x.js";
var SWITCH_NAME = "Switch";
var [createSwitchContext, createSwitchScope] = createContextScope(SWITCH_NAME);
var [SwitchProvider, useSwitchContext] = createSwitchContext(SWITCH_NAME);
var Switch$1 = React.forwardRef(
  (props, forwardedRef) => {
    const {
      __scopeSwitch,
      name,
      checked: checkedProp,
      defaultChecked,
      required,
      disabled,
      value = "on",
      onCheckedChange,
      form,
      ...switchProps
    } = props;
    const [button, setButton] = React.useState(null);
    const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
    const hasConsumerStoppedPropagationRef = React.useRef(false);
    const isFormControl = button ? form || !!button.closest("form") : true;
    const [checked, setChecked] = useControllableState({
      prop: checkedProp,
      defaultProp: defaultChecked ?? false,
      onChange: onCheckedChange,
      caller: SWITCH_NAME
    });
    return /* @__PURE__ */ jsxs(SwitchProvider, { scope: __scopeSwitch, checked, disabled, children: [
      /* @__PURE__ */ jsx(
        Primitive.button,
        {
          type: "button",
          role: "switch",
          "aria-checked": checked,
          "aria-required": required,
          "data-state": getState(checked),
          "data-disabled": disabled ? "" : void 0,
          disabled,
          value,
          ...switchProps,
          ref: composedRefs,
          onClick: composeEventHandlers(props.onClick, (event) => {
            setChecked((prevChecked) => !prevChecked);
            if (isFormControl) {
              hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
              if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
            }
          })
        }
      ),
      isFormControl && /* @__PURE__ */ jsx(
        SwitchBubbleInput,
        {
          control: button,
          bubbles: !hasConsumerStoppedPropagationRef.current,
          name,
          value,
          checked,
          required,
          disabled,
          form,
          style: { transform: "translateX(-100%)" }
        }
      )
    ] });
  }
);
Switch$1.displayName = SWITCH_NAME;
var THUMB_NAME = "SwitchThumb";
var SwitchThumb = React.forwardRef(
  (props, forwardedRef) => {
    const { __scopeSwitch, ...thumbProps } = props;
    const context = useSwitchContext(THUMB_NAME, __scopeSwitch);
    return /* @__PURE__ */ jsx(
      Primitive.span,
      {
        "data-state": getState(context.checked),
        "data-disabled": context.disabled ? "" : void 0,
        ...thumbProps,
        ref: forwardedRef
      }
    );
  }
);
SwitchThumb.displayName = THUMB_NAME;
var BUBBLE_INPUT_NAME = "SwitchBubbleInput";
var SwitchBubbleInput = React.forwardRef(
  ({
    __scopeSwitch,
    control,
    checked,
    bubbles = true,
    ...props
  }, forwardedRef) => {
    const ref = React.useRef(null);
    const composedRefs = useComposedRefs(ref, forwardedRef);
    const prevChecked = usePrevious(checked);
    const controlSize = useSize(control);
    React.useEffect(() => {
      const input = ref.current;
      if (!input) return;
      const inputProto = window.HTMLInputElement.prototype;
      const descriptor = Object.getOwnPropertyDescriptor(
        inputProto,
        "checked"
      );
      const setChecked = descriptor.set;
      if (prevChecked !== checked && setChecked) {
        const event = new Event("click", { bubbles });
        setChecked.call(input, checked);
        input.dispatchEvent(event);
      }
    }, [prevChecked, checked, bubbles]);
    return /* @__PURE__ */ jsx(
      "input",
      {
        type: "checkbox",
        "aria-hidden": true,
        defaultChecked: checked,
        ...props,
        tabIndex: -1,
        ref: composedRefs,
        style: {
          ...props.style,
          ...controlSize,
          position: "absolute",
          pointerEvents: "none",
          opacity: 0,
          margin: 0
        }
      }
    );
  }
);
SwitchBubbleInput.displayName = BUBBLE_INPUT_NAME;
function getState(checked) {
  return checked ? "checked" : "unchecked";
}
var Root = Switch$1;
var Thumb = SwitchThumb;
const Switch = forwardRef(({ className, label, description, size = "default", ...props }, ref) => {
  const id = useId();
  const descriptionId = `${id}-description`;
  const sizes = {
    default: {
      root: "h-5 w-[34px] p-0.5 md:h-6 md:w-[40px]",
      thumb: "size-4 md:size-5",
      translate: "data-[state=checked]:translate-x-[14px] md:data-[state=checked]:translate-x-4"
    },
    small: {
      root: "h-4 w-[26px] p-0.5",
      thumb: "size-3",
      translate: "data-[state=checked]:translate-x-[10px]"
    },
    xs: {
      root: "h-3 w-[21px] p-px",
      thumb: "size-2.5",
      translate: "data-[state=checked]:translate-x-[9px]"
    }
  };
  const switchElement = /* @__PURE__ */ jsx(
    Root,
    {
      ref,
      id,
      "data-slot": "switch",
      "aria-describedby": description ? descriptionId : void 0,
      className: cn(
        "inline-flex shrink-0 items-center rounded-full peer cursor-pointer",
        "data-[state=checked]:bg-accent data-[state=unchecked]:bg-gray-300 dark:data-[state=unchecked]:bg-gray-700",
        "transition-all outline-none focus-visible:ring-[3px] focus-visible:ring-accent/50",
        "disabled:cursor-not-allowed disabled:opacity-50",
        sizes[size].root,
        className
      ),
      ...props,
      children: /* @__PURE__ */ jsx(
        Thumb,
        {
          "data-slot": "switch-thumb",
          className: cn(
            "pointer-events-none block rounded-full ring-0",
            "bg-white",
            "transition-all duration-200 data-[state=unchecked]:translate-x-0",
            sizes[size].thumb,
            sizes[size].translate
          )
        }
      )
    }
  );
  if (!label) {
    return switchElement;
  }
  return /* @__PURE__ */ jsx("div", { className: "py-2 space-y-1", children: /* @__PURE__ */ jsxs(
    "label",
    {
      htmlFor: id,
      className: "flex items-center space-x-2 cursor-pointer",
      children: [
        switchElement,
        /* @__PURE__ */ jsxs("div", { className: "flex-1", children: [
          /* @__PURE__ */ jsx("span", { className: "block font-medium text-strong leading-5", children: label }),
          description && /* @__PURE__ */ jsx("span", { id: descriptionId, className: "block text-sm text-subtle", children: description })
        ] })
      ]
    }
  ) });
});
Switch.displayName = "Switch";
export {
  Switch
};
