"use strict";
var import_RadioGroup = require("../RadioGroup.css");
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const jsxRuntime = require("react/jsx-runtime");
const antd = require("antd");
const React = require("react");
const kit_Button = require("./Button.cjs");
const kit_Icon = require("./Icon.cjs");
const kit_internal_useResize = require("./internal/useResize.cjs");
const kit_Theme_UI = require("./Theme/UI.cjs");
require("./Theme/themeUtils.cjs");
require("./Theme/themes.cjs");
const kit_Tooltip = require("./Tooltip.cjs");
const kit_internal_ConditionalWrapper = require("./internal/ConditionalWrapper.cjs");
const base = "RadioGroup_base_c5402";
const option = "RadioGroup_option_c5402";
const label = "RadioGroup_label_c5402";
const iconOnly = "RadioGroup_iconOnly_c5402";
const row = "RadioGroup_row_c5402";
const css = {
  base,
  option,
  label,
  iconOnly,
  row
};
const DEFAULT_RESIZE_THROTTLE_TIME = 500;
const PARENT_WIDTH_BUFFER = 16;
const HEIGHT_LIMIT = 50;
function RadioGroup({
  defaultValue,
  iconOnly: iconOnly2 = false,
  onChange,
  options,
  value,
  radioType = "button"
}) {
  const { refCallback, size, refObject: baseRef } = kit_internal_useResize();
  const originalWidth = React.useRef();
  const [sizes, setSizes] = React.useState({ baseHeight: 0, baseWidth: 0, parentWidth: 0 });
  const {
    themeSettings: { className: themeClass }
  } = kit_Theme_UI.useTheme();
  const classes = [css.base, themeClass];
  if (radioType === "row")
    classes.push(css.row);
  const hasIconsAndLabels = React.useMemo(() => {
    if (options.length === 0)
      return false;
    return options.reduce((acc, option2) => acc || !!option2.icon && !!option2.label, false);
  }, [options]);
  const showLabels = React.useMemo(() => {
    if (radioType === "row")
      return true;
    if (!hasIconsAndLabels || !baseRef.current)
      return true;
    if (sizes.baseWidth === 0 || sizes.parentWidth === 0)
      return true;
    if (originalWidth.current && originalWidth.current < sizes.parentWidth)
      return true;
    if (sizes.baseHeight > HEIGHT_LIMIT)
      return false;
    return true;
  }, [hasIconsAndLabels, sizes]);
  if (iconOnly2)
    classes.push(css.iconOnly);
  const handleChange = React.useCallback(
    (e) => {
      onChange == null ? void 0 : onChange(e.target.value);
    },
    [onChange]
  );
  React.useEffect(() => {
    if (!hasIconsAndLabels || !baseRef.current)
      return;
    const parent = baseRef.current.parentElement;
    if (!parent)
      return;
    const parentRect = parent.getBoundingClientRect();
    if (!parentRect)
      return;
    const baseRect = size;
    if (!originalWidth.current)
      originalWidth.current = baseRect.width;
    setSizes({
      baseHeight: baseRect.height,
      baseWidth: parentRect.width,
      parentWidth: parentRect.width - PARENT_WIDTH_BUFFER
    });
  }, [hasIconsAndLabels, size]);
  return /* @__PURE__ */ jsxRuntime.jsx(
    antd.Radio.Group,
    {
      className: classes.join(" "),
      defaultValue,
      ref: refCallback,
      size: radioType === "row" ? "large" : void 0,
      value,
      onChange: handleChange,
      children: options.map((option2) => /* @__PURE__ */ jsxRuntime.jsx(
        kit_internal_ConditionalWrapper.ConditionalWrapper,
        {
          condition: !showLabels || iconOnly2,
          wrapper: (children) => /* @__PURE__ */ jsxRuntime.jsx(kit_Tooltip, { content: option2.label, placement: "top", children }),
          children: radioType === "radio" ? /* @__PURE__ */ jsxRuntime.jsxs(antd.Radio, { className: css.option, value: option2.id, children: [
            option2.icon && /* @__PURE__ */ jsxRuntime.jsx(kit_Icon.default, { decorative: true, name: option2.icon, size: option2.iconSize }),
            option2.label && showLabels && !iconOnly2 && /* @__PURE__ */ jsxRuntime.jsx("span", { className: css.label, children: option2.label })
          ] }) : radioType === "row" ? /* @__PURE__ */ jsxRuntime.jsx(
            kit_Button,
            {
              column: true,
              icon: /* @__PURE__ */ jsxRuntime.jsx(kit_Icon.default, { decorative: true, name: option2.icon, size: option2.iconSize }),
              selected: value ? option2.id === value : option2.id === defaultValue,
              size: "large",
              onClick: () => onChange == null ? void 0 : onChange(option2.id),
              children: option2.label && showLabels && !iconOnly2 && /* @__PURE__ */ jsxRuntime.jsx("span", { className: css.label, children: option2.label })
            }
          ) : /* @__PURE__ */ jsxRuntime.jsxs(antd.Radio.Button, { className: css.option, value: option2.id, children: [
            option2.icon && /* @__PURE__ */ jsxRuntime.jsx(kit_Icon.default, { decorative: true, name: option2.icon, size: option2.iconSize }),
            option2.label && showLabels && !iconOnly2 && /* @__PURE__ */ jsxRuntime.jsx("span", { className: css.label, children: option2.label })
          ] })
        },
        option2.id
      ))
    }
  );
}
exports.DEFAULT_RESIZE_THROTTLE_TIME = DEFAULT_RESIZE_THROTTLE_TIME;
exports.default = RadioGroup;
