"use strict";
"use client";
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/Checkbox/Checkbox.tsx
var Checkbox_exports = {};
__export(Checkbox_exports, {
  Checkbox: () => Checkbox
});
module.exports = __toCommonJS(Checkbox_exports);
var import_react2 = require("react");
var import_dedupe6 = __toESM(require("classnames/dedupe"));
var import_icons2 = require("@simplybusiness/icons");

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

// src/components/Checkbox/Checkbox.tsx
var import_Checkbox = require("@simplybusiness/mobius/src/components/Checkbox/Checkbox.css");
var import_jsx_runtime6 = require("react/jsx-runtime");
var Checkbox = ({ ref, ...props }) => {
  const {
    id,
    label,
    isDisabled,
    isRequired,
    isInvalid,
    onChange,
    className,
    errorMessage,
    selected,
    defaultSelected = false,
    isReadOnly,
    isLastItem,
    name,
    value,
    ["aria-describedby"]: ariaDescribedBy,
    ...rest
  } = props;
  const [checked, setChecked] = (0, import_react2.useState)(defaultSelected);
  const fallbackRef = (0, import_react2.useRef)(null);
  const refObj = ref || fallbackRef;
  const inputId = (0, import_react2.useId)();
  const isControlled = typeof selected === "boolean";
  (0, import_react2.useEffect)(() => {
    if (isControlled) {
      setChecked(selected);
    }
  }, [selected, isControlled]);
  const validationClasses = useValidationClasses({ isInvalid });
  const sharedClasses = (0, import_dedupe6.default)(
    {
      "--is-disabled": isDisabled,
      "--is-selected": checked,
      "--is-required": typeof isRequired === "boolean" && isRequired,
      "--is-optional": typeof isRequired === "boolean" && !isRequired
    },
    validationClasses
  );
  const wrapperClasses = (0, import_dedupe6.default)(
    "mobius",
    "mobius-checkbox",
    sharedClasses,
    className
  );
  const labelClasses = (0, import_dedupe6.default)("mobius-checkbox__label", sharedClasses);
  const inputClasses = (0, import_dedupe6.default)("mobius-checkbox__input", sharedClasses);
  const iconClasses = (0, import_dedupe6.default)("mobius-checkbox__icon", sharedClasses);
  const errorMessageId = (0, import_react2.useId)();
  const shouldErrorMessageShow = errorMessage ? errorMessageId : void 0;
  const describedBy = spaceDelimitedList([
    shouldErrorMessageShow,
    ariaDescribedBy
  ]);
  const labelId = (0, import_react2.useId)();
  const handleChange = (event) => {
    setChecked(!checked);
    if (onChange) {
      onChange(event, isLastItem);
    }
  };
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(Stack, { gap: "xs", className: wrapperClasses, children: [
    /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("label", { className: labelClasses, children: [
      /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
        "input",
        {
          "aria-describedby": describedBy,
          "aria-errormessage": shouldErrorMessageShow,
          "aria-invalid": isInvalid,
          "aria-labelledby": labelId,
          readOnly: isReadOnly,
          disabled: isDisabled,
          ref: refObj,
          className: inputClasses,
          onChange: handleChange,
          type: "checkbox",
          checked,
          required: isRequired,
          id: id || inputId,
          name,
          value,
          ...rest
        }
      ),
      /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
        Icon,
        {
          icon: checked ? import_icons2.squareTick : import_icons2.square,
          size: "md",
          className: iconClasses
        }
      ),
      /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { id: labelId, className: "mobius-checkbox__visible-label", children: label })
    ] }),
    /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ErrorMessage, { id: errorMessageId, errorMessage })
  ] });
};
Checkbox.displayName = "Checkbox";
//# sourceMappingURL=Checkbox.js.map
