"use client";
import * as React from "react";
import { useStableCallback } from "../../../utils/esm/useStableCallback.js";
import { useControlled } from "../../../utils/esm/useControlled.js";
import { useRenderElement } from "../utils/useRenderElement.js";
import { CompositeRoot } from "../composite/root/CompositeRoot.js";
import { useToolbarRootContext } from "../toolbar/root/ToolbarRootContext.js";
import { ToggleGroupContext } from "./ToggleGroupContext.js";
import { ToggleGroupDataAttributes } from "./ToggleGroupDataAttributes.js";
import { jsx } from "react/jsx-runtime";
const stateAttributesMapping = {
  multiple(value) {
    if (value) {
      return {
        [ToggleGroupDataAttributes.multiple]: ""
      };
    }
    return null;
  }
};
const ToggleGroup = /* @__PURE__ */ React.forwardRef(function ToggleGroup2(componentProps, forwardedRef) {
  const {
    defaultValue: defaultValueProp,
    disabled: disabledProp = false,
    loopFocus = true,
    onValueChange,
    orientation = "horizontal",
    multiple = false,
    value: valueProp,
    className,
    render,
    ...elementProps
  } = componentProps;
  const toolbarContext = useToolbarRootContext();
  const defaultValue = React.useMemo(() => {
    if (valueProp === void 0) {
      return defaultValueProp ?? [];
    }
    return void 0;
  }, [valueProp, defaultValueProp]);
  const disabled = (toolbarContext?.disabled ?? false) || disabledProp;
  const [groupValue, setValueState] = useControlled({
    controlled: valueProp,
    default: defaultValue,
    name: "ToggleGroup",
    state: "value"
  });
  const setGroupValue = useStableCallback((newValue, nextPressed, eventDetails) => {
    let newGroupValue;
    if (multiple) {
      newGroupValue = groupValue.slice();
      if (nextPressed) {
        newGroupValue.push(newValue);
      } else {
        newGroupValue.splice(groupValue.indexOf(newValue), 1);
      }
    } else {
      newGroupValue = nextPressed ? [newValue] : [];
    }
    if (Array.isArray(newGroupValue)) {
      onValueChange?.(newGroupValue, eventDetails);
      if (eventDetails.isCanceled) {
        return;
      }
      setValueState(newGroupValue);
    }
  });
  const state = React.useMemo(() => ({
    disabled,
    multiple,
    orientation
  }), [disabled, orientation, multiple]);
  const contextValue = React.useMemo(() => ({
    disabled,
    orientation,
    setGroupValue,
    value: groupValue
  }), [disabled, orientation, setGroupValue, groupValue]);
  const defaultProps = {
    role: "group"
  };
  const element = useRenderElement("div", componentProps, {
    enabled: Boolean(toolbarContext),
    state,
    ref: forwardedRef,
    props: [defaultProps, elementProps],
    stateAttributesMapping
  });
  return /* @__PURE__ */ jsx(ToggleGroupContext.Provider, {
    value: contextValue,
    children: toolbarContext ? element : /* @__PURE__ */ jsx(CompositeRoot, {
      render,
      className,
      state,
      refs: [forwardedRef],
      props: [defaultProps, elementProps],
      stateAttributesMapping,
      loopFocus
    })
  });
});
if (process.env.NODE_ENV !== "production") ToggleGroup.displayName = "ToggleGroup";
export {
  ToggleGroup
};
//# sourceMappingURL=ToggleGroup.js.map
