"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/Radio/index.tsx
var Radio_exports = {};
__export(Radio_exports, {
  Radio: () => Radio,
  RadioGroup: () => RadioGroup
});
module.exports = __toCommonJS(Radio_exports);

// src/components/Radio/Radio.tsx
var import_dedupe6 = __toESM(require("classnames/dedupe"));
var import_react2 = require("react");

// src/components/ErrorMessage/ErrorMessage.tsx
var import_icons = require("@simplybusiness/icons");
var import_dedupe4 = __toESM(require("classnames/dedupe"));

// src/components/Icon/Icon.tsx
var import_dedupe = __toESM(require("classnames/dedupe"));
var import_Icon = require("@simplybusiness/mobius/src/components/Icon/Icon.css");
var import_jsx_runtime = require("react/jsx-runtime");
var ICON_PREFIX = "mobius-icon";
var capitaliseFirstLetter = (str) => str.charAt(0).toUpperCase() + str.slice(1);
function Icon({
  ref,
  icon,
  className,
  size = "xs",
  color,
  fixedWidth,
  spin,
  spinReverse,
  title,
  ...otherProps
}) {
  if (!icon) {
    throw new Error("Must specify icon object");
  }
  const classes = (0, import_dedupe.default)(
    "mobius",
    "mobius-icon",
    `svg-inline--${ICON_PREFIX}`,
    `--size-${size}`,
    className,
    {
      [`${ICON_PREFIX}-fw`]: fixedWidth,
      [`${ICON_PREFIX}-spin`]: spin || spinReverse,
      [`${ICON_PREFIX}-spin-reverse`]: spinReverse
    }
  );
  const { iconName, width, height, svgPathData } = icon;
  const formattedIconName = iconName.split("-").join(" ");
  const defaultTitle = `${capitaliseFirstLetter(formattedIconName)} icon`;
  const titleText = title || defaultTitle;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    "svg",
    {
      ref,
      focusable: "false",
      "data-icon": iconName,
      className: classes,
      role: "img",
      xmlns: "http://www.w3.org/2000/svg",
      viewBox: `0 0 ${width} ${height}`,
      style: { color },
      ...otherProps,
      children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)("title", { children: titleText }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { fill: "currentColor", d: svgPathData })
      ]
    }
  );
}

// src/components/TextOrHTML/TextOrHTML.tsx
var import_dedupe3 = __toESM(require("classnames/dedupe"));
var import_react = require("react");

// src/components/Text/Text.tsx
var import_dedupe2 = __toESM(require("classnames/dedupe"));
var import_Text = require("@simplybusiness/mobius/src/components/Text/Text.css");
var import_jsx_runtime2 = require("react/jsx-runtime");
var getElementType = (variant, elementType) => {
  if (elementType) {
    return elementType;
  }
  if (variant && ["h1", "h2", "h3", "h4"].includes(variant)) {
    return variant;
  }
  return "p";
};
var Text = ({ ref, elementType, ...props }) => {
  const { variant, className, spacing, ...otherProps } = props;
  const Element = getElementType(variant, elementType);
  const variantType = variant || Element;
  const classes = (0, import_dedupe2.default)(
    "mobius",
    "mobius-text",
    { [`--is-${variantType}`]: variantType },
    { [`--has-line-height-${spacing}`]: spacing },
    className
  );
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Element, { ref, ...otherProps, className: classes });
};
Text.displayName = "Text";

// src/components/TextOrHTML/TextOrHTML.tsx
var import_jsx_runtime3 = require("react/jsx-runtime");
var BLOCK_TAG = /<(div|p|ul|ol|li|h[1-6]|table|tr|td|th|blockquote|pre|hr|dl|dt|dd)[\s>/]/i;
var containsBlockHTML = (text) => BLOCK_TAG.test(text);
var containsHTML = (text) => /<[a-z/]/i.test(text) || /&(?:#\d+|#x[\da-f]+|[a-z]\w*);/i.test(text);
var buildTextClasses = (textProps, htmlClassName) => {
  const { variant, elementType, spacing, className } = textProps;
  const variantType = variant || getElementType(variant, elementType);
  return (0, import_dedupe3.default)(
    "mobius",
    "mobius-text",
    { [`--is-${variantType}`]: variantType },
    { [`--has-line-height-${spacing}`]: spacing },
    className,
    htmlClassName
  );
};
var TextOrHTML = ({
  ref,
  text,
  htmlClassName,
  htmlElementType,
  textWrapper = false,
  autoDetect = false,
  ...textProps
}) => {
  const hasBlockContent = autoDetect && containsBlockHTML(text);
  const dangerousHTML = (0, import_react.useMemo)(() => ({ __html: text }), [text]);
  if (autoDetect && !hasBlockContent) {
    const { variant, spacing, elementType, className, ...domProps } = textProps;
    const Element = getElementType(variant, elementType);
    const classes = buildTextClasses(textProps, htmlClassName);
    return containsHTML(text) ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
      Element,
      {
        ref,
        ...domProps,
        className: classes,
        dangerouslySetInnerHTML: dangerousHTML
      }
    ) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Element, { ref, ...domProps, className: classes, children: text });
  }
  const DangerousComponent = htmlElementType ?? (hasBlockContent ? "div" : "span");
  const dangerousElement = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
    DangerousComponent,
    {
      className: htmlClassName,
      dangerouslySetInnerHTML: dangerousHTML
    }
  );
  if (textWrapper) {
    return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Text, { ref, ...textProps, children: dangerousElement });
  }
  return dangerousElement;
};
TextOrHTML.displayName = "TextOrHTML";

// src/components/ErrorMessage/ErrorMessage.tsx
var import_ErrorMessage = require("@simplybusiness/mobius/src/components/ErrorMessage/ErrorMessage.css");
var import_jsx_runtime4 = require("react/jsx-runtime");
var ErrorMessage = ({
  id,
  errorMessage,
  className
}) => {
  const classes = (0, import_dedupe4.default)("mobius", "mobius-error-message", className);
  if (!errorMessage) return null;
  return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { id, className: classes, "data-testid": "ErrorMessage", role: "alert", children: [
    /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
      Icon,
      {
        icon: import_icons.error,
        className: "mobius-error-message__icon",
        "aria-hidden": "true"
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
      TextOrHTML,
      {
        elementType: "span",
        className: "mobius-error-message__text",
        text: errorMessage
      }
    )
  ] });
};
ErrorMessage.displayName = "ErrorMessage";

// src/components/Label/Label.tsx
var import_dedupe5 = __toESM(require("classnames/dedupe"));
var import_Label = require("@simplybusiness/mobius/src/components/Label/Label.css");
var import_jsx_runtime5 = require("react/jsx-runtime");
var Label = ({ ref, ...props }) => {
  const { elementType: Element = "label", children, ...otherProps } = props;
  const classes = (0, import_dedupe5.default)("mobius", "mobius-label", props.className);
  otherProps.className = classes;
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Element, { ref, ...otherProps, className: classes, children });
};
Label.displayName = "Label";

// src/components/Radio/Radio.tsx
var import_Radio = require("@simplybusiness/mobius/src/components/Radio/Radio.css");
var import_jsx_runtime6 = require("react/jsx-runtime");
var Radio = ({ ref, ...props }) => {
  const {
    children,
    value,
    label,
    className,
    isDisabled,
    errorMessage,
    onChange,
    defaultChecked,
    groupDisabled,
    name,
    selected,
    setSelected,
    isRequired,
    onOverflow,
    orientation,
    ...otherProps
  } = props;
  const realDisabled = groupDisabled || isDisabled;
  const isMultiline = label && children;
  const isControlled = selected !== void 0;
  const isChecked = isControlled ? selected === value : defaultChecked;
  const contentRef = (0, import_react2.useRef)(null);
  const prevOverflowRef = (0, import_react2.useRef)({
    vertical: false,
    horizontal: false
  });
  const hasIconFirst = (0, import_react2.useMemo)(() => {
    if (!children || import_react2.Children.count(children) === 0) return false;
    const firstChild = import_react2.Children.toArray(children)[0];
    if (!(0, import_react2.isValidElement)(firstChild)) return false;
    const props2 = firstChild.props;
    return "icon" in props2 && props2.icon !== void 0;
  }, [children]);
  (0, import_react2.useLayoutEffect)(() => {
    if (!contentRef.current || !onOverflow) return;
    if (orientation === "vertical") {
      return;
    }
    const element = contentRef.current;
    const scrollOverflowVertical = element.scrollHeight > element.clientHeight;
    const scrollOverflowHorizontal = element.scrollWidth > element.clientWidth;
    const styles = window.getComputedStyle(element);
    const lineHeight = parseFloat(styles.lineHeight);
    const fontSize = parseFloat(styles.fontSize);
    const singleLineHeight = isNaN(lineHeight) ? fontSize * 1.2 : lineHeight;
    const WRAP_DETECTION_TOLERANCE = 1.1;
    const hasWrapped = element.clientHeight > singleLineHeight * WRAP_DETECTION_TOLERANCE;
    const vertical = scrollOverflowVertical || hasWrapped;
    const horizontal = scrollOverflowHorizontal;
    const newOverflowState = { vertical, horizontal };
    const prevOverflow = prevOverflowRef.current;
    if (newOverflowState.vertical !== prevOverflow.vertical || newOverflowState.horizontal !== prevOverflow.horizontal) {
      prevOverflowRef.current = newOverflowState;
      onOverflow(newOverflowState);
    }
  }, [label, children, onOverflow, orientation]);
  const radioClasses = {
    "--is-disabled": realDisabled,
    "--is-selected": selected === value,
    "--is-multiline": !!isMultiline,
    "--is-required": isRequired
  };
  const containerClasses = (0, import_dedupe6.default)(
    "mobius-radio__label",
    radioClasses,
    className,
    { "--has-icon-first": hasIconFirst }
  );
  const inputClasses = (0, import_dedupe6.default)("mobius-radio__input", radioClasses);
  const { "aria-describedby": _ariaDescribedBy, ...rest } = otherProps;
  const handleChange = (event) => {
    if (setSelected) {
      setSelected(event.target.value);
    }
    if (onChange) {
      const adaptedEvent = {
        ...event.nativeEvent,
        target: event.target
      };
      onChange(adaptedEvent);
    }
  };
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_jsx_runtime6.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(Label, { className: containerClasses, children: [
      /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
        "input",
        {
          "aria-describedby": otherProps["aria-describedby"],
          disabled: realDisabled,
          ref,
          className: inputClasses,
          value,
          tabIndex: 0,
          type: "radio",
          onChange: handleChange,
          ...isControlled ? { checked: isChecked } : { defaultChecked: isChecked },
          name,
          required: isRequired,
          ...rest
        }
      ),
      isMultiline ? /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { ref: contentRef, className: "mobius-radio__content--multiline", children: [
        /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "mobius-radio__content-first-line", children: label }),
        /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "mobius-radio__extra-content", children })
      ] }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { ref: contentRef, className: "mobius-radio__content", children: label || children })
    ] }),
    errorMessage && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ErrorMessage, { errorMessage })
  ] });
};
Radio.displayName = "Radio";

// src/components/Radio/RadioGroup.tsx
var import_dedupe8 = __toESM(require("classnames/dedupe"));
var import_react3 = require("react");

// src/utils/spaceDelimitedList.ts
function spaceDelimitedList(list) {
  return list.filter(Boolean).join(" ") || void 0;
}

// src/hooks/useValidationClasses/useValidationClasses.ts
var useValidationClasses = (props) => {
  const { isInvalid } = props;
  if (isInvalid) {
    return "--is-invalid";
  }
  if (isInvalid === false) {
    return "--is-valid";
  }
  return "";
};

// src/components/Stack/Stack.tsx
var import_dedupe7 = __toESM(require("classnames/dedupe"));
var import_Stack = require("@simplybusiness/mobius/src/components/Stack/Stack.css");
var import_jsx_runtime7 = require("react/jsx-runtime");
var Stack = ({ ref, ...props }) => {
  const { elementType: Element = "div", gap, ...otherProps } = props;
  const classes = (0, import_dedupe7.default)(
    "mobius",
    "mobius-stack",
    {
      [`--gap-${gap}`]: gap
    },
    otherProps.className
  );
  return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Element, { ref, ...otherProps, className: classes });
};
Stack.displayName = "Stack";

// src/components/Radio/RadioGroup.tsx
var import_jsx_runtime8 = require("react/jsx-runtime");
var getDefaultVal = (children, defaultValue) => {
  if (Array.isArray(children) && defaultValue) {
    const option = children?.find((item) => item.props.value === defaultValue);
    if (!option) return "";
    return option.props.value;
  }
  return "";
};
var RadioGroup = ({ ref, ...props }) => {
  const {
    label,
    isDisabled = false,
    isRequired,
    isInvalid,
    orientation = "vertical",
    className,
    errorMessage,
    children,
    defaultValue,
    value,
    isReadOnly,
    name,
    onChange,
    autoStack = false,
    ...rest
  } = props;
  const defaultSelected = getDefaultVal(children, value || defaultValue);
  const [selected, setSelected] = (0, import_react3.useState)(defaultSelected);
  const overflowsRef = (0, import_react3.useRef)({});
  const [hasOverflow, setHasOverflow] = (0, import_react3.useState)(false);
  (0, import_react3.useEffect)(() => {
    if (value !== void 0) {
      setSelected(value);
    }
  }, [value]);
  const handleOverflow = (0, import_react3.useCallback)(
    (radioValue, overflow) => {
      overflowsRef.current = {
        ...overflowsRef.current,
        [radioValue]: overflow
      };
      const anyOverflow = Object.values(overflowsRef.current).some(
        (o) => o.vertical || o.horizontal
      );
      if (anyOverflow !== hasOverflow) {
        setHasOverflow(anyOverflow);
      }
    },
    [hasOverflow]
  );
  const effectiveOrientation = autoStack && orientation === "horizontal" && hasOverflow ? "vertical" : orientation;
  const validationClasses = useValidationClasses({ isInvalid });
  const radioClasses = {
    "--is-disabled": isDisabled,
    "--is-required": typeof isRequired === "boolean" && isRequired,
    "--is-optional": typeof isRequired === "boolean" && !isRequired,
    [`--is-${effectiveOrientation}`]: true,
    [className || ""]: true
  };
  const radioGroupClasses = (0, import_dedupe8.default)(
    "mobius",
    "mobius-radio-group",
    radioClasses,
    validationClasses
  );
  const radioWrapperClasses = (0, import_dedupe8.default)("mobius-radio__wrapper", {
    [`--is-${effectiveOrientation}`]: true
  });
  const labelClasses = (0, import_dedupe8.default)(radioClasses, validationClasses);
  const errorMessageId = (0, import_react3.useId)();
  const defaultNameAttrId = (0, import_react3.useId)();
  const nameAttribute = name || defaultNameAttrId;
  const shouldErrorMessageShow = errorMessage ? errorMessageId : void 0;
  const describedBy = spaceDelimitedList([
    shouldErrorMessageShow,
    props["aria-describedby"]
  ]);
  const labelId = (0, import_react3.useId)();
  return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
    "div",
    {
      ...rest,
      "aria-describedby": describedBy,
      "aria-disabled": isDisabled,
      "aria-errormessage": shouldErrorMessageShow,
      "aria-invalid": isInvalid,
      "aria-label": props["aria-label"],
      "aria-labelledby": props["aria-labelledby"] || labelId,
      "aria-orientation": effectiveOrientation,
      "aria-readonly": isReadOnly,
      "aria-required": isRequired,
      ref,
      className: radioGroupClasses,
      role: "radiogroup",
      children: /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(Stack, { gap: "xs", children: [
        label && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(Label, { htmlFor: name, id: labelId, className: labelClasses, children: label }),
        /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("div", { className: radioWrapperClasses, children: import_react3.Children.map(children, (child) => {
          if ((0, import_react3.isValidElement)(child)) {
            const childValue = child.props.value;
            return (0, import_react3.cloneElement)(
              child,
              {
                orientation: effectiveOrientation,
                groupDisabled: isDisabled,
                name: nameAttribute,
                selected: selected || defaultSelected,
                // in case state does not update
                setSelected,
                isRequired,
                "aria-describedby": describedBy,
                onChange,
                onOverflow: childValue ? (overflow) => handleOverflow(childValue, overflow) : void 0
              }
            );
          }
          return child;
        }) }),
        /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ErrorMessage, { id: errorMessageId, errorMessage })
      ] })
    }
  );
};
RadioGroup.displayName = "RadioGroup";
//# sourceMappingURL=index.js.map
