import { jsxs, jsx } from 'react/jsx-runtime';
import { createContext, useContext, forwardRef, useId, useMemo, useState } from 'react';
import { c as checkboxStyles } from './Checkbox.module-CURNX27M.js';
import { c as cx } from './index-De1g9FRV.js';
import { F as FieldLabel } from './FieldLabel-Cm5A1Jcx.js';
import { F as FieldMessage } from './FieldMessage-mTGdycSA.js';
import { w as warnDeprecatedErrorUsage } from './utils-CM48ODEJ.js';
import { s as stripInlineMarkdown } from './stripInlineMarkdown-BZzgG-Zm.js';
import { u as useLayoutPropsUtil } from './useLayoutPropsUtil-DjqeAJZ9.js';
import { w as warnOnce } from './warnOnce-Y9PRHcU4.js';
import { I as Icon } from './Icon-B4uCvMU9.js';
import { S as SvgCheckBox, a as SvgCheckBoxOutlineBlank } from './check_box-0U1tbEAd.js';
import { S as SvgIndeterminateCheckBox } from './indeterminate_check_box-jjLG4UY6.js';
import { H as Helper } from './Helper-B_VAFgGd.js';

const CheckboxGroupContext = createContext(void 0);
const useCheckboxGroupContext = () => {
  return useContext(CheckboxGroupContext);
};

const CheckboxGroup = forwardRef((props, ref) => {
  const { layoutStyles, componentProps } = useLayoutPropsUtil(props);
  const {
    className,
    children,
    description,
    error,
    errorAriaLive: _errorAriaLive,
    hint,
    warning,
    hideLabel = false,
    legend,
    moreInfo,
    legendProps,
    onBlur,
    onFocus,
    required,
    style,
    ...rest
  } = componentProps;
  const checkboxGroupClassNames = cx(checkboxStyles["checkbox-group"], className);
  const helperUid = useId();
  const moreInfoUid = useId();
  const styleCombined = {
    ...style,
    ...layoutStyles,
    alignItems: layoutStyles?.alignItems ?? (layoutStyles?.flexDirection === "row" ? "flex-start" : void 0)
  };
  const labelClassNames = cx(checkboxStyles["label"], {
    [checkboxStyles["direction-row"]]: layoutStyles?.flexDirection === "row"
  });
  warnDeprecatedErrorUsage("CheckboxGroup", error);
  if (typeof legend !== "string") {
    warnOnce(
      "CheckboxGroup: Passing ReactNode to legend is deprecated. Use a plain string with inline markdown instead."
    );
  }
  const errorMessages = typeof error === "boolean" || error === void 0 ? void 0 : error;
  const hasHelperText = hint || errorMessages || warning || description;
  const ariaDescribedBy = [hasHelperText && helperUid, moreInfo && moreInfoUid].filter(Boolean).join(" ") || void 0;
  const contextValue = useMemo(
    () => ({
      hasError: !!error
    }),
    [error]
  );
  return /* @__PURE__ */ jsxs(
    "fieldset",
    {
      role: "group",
      "data-anv": "checkbox-group",
      className: checkboxGroupClassNames,
      ref,
      style: styleCombined,
      onFocus,
      onBlur,
      ...rest,
      "aria-describedby": ariaDescribedBy,
      children: [
        /* @__PURE__ */ jsxs("legend", { className: checkboxStyles["legend"], children: [
          typeof legend === "string" ? stripInlineMarkdown(legend) : legend,
          required && ", required"
        ] }),
        !hideLabel && /* @__PURE__ */ jsx("div", { className: checkboxStyles["label-row"], children: /* @__PURE__ */ jsx(
          FieldLabel,
          {
            "aria-hidden": true,
            required,
            moreInfo,
            moreInfoId: moreInfo ? moreInfoUid : void 0,
            ...legendProps,
            className: cx(labelClassNames, legendProps?.className),
            children: legend
          }
        ) }),
        /* @__PURE__ */ jsx(CheckboxGroupContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(
          "div",
          {
            className: checkboxStyles["children"],
            style: {
              flexDirection: layoutStyles?.flexDirection,
              gap: layoutStyles?.gap
            },
            children
          }
        ) }),
        hasHelperText ? /* @__PURE__ */ jsx(
          FieldMessage,
          {
            id: helperUid,
            hint,
            error: errorMessages,
            warning,
            description
          }
        ) : null
      ]
    }
  );
});
CheckboxGroup.displayName = "CheckboxGroup";

const CheckboxElement = forwardRef(
  (props, ref) => {
    const { layoutStyles, componentProps } = useLayoutPropsUtil(props);
    const {
      indeterminate,
      onChange,
      onClick,
      value,
      className,
      disabled,
      error,
      icons,
      checked: checkedProp,
      defaultChecked,
      label,
      style,
      required,
      description,
      hideCheckbox = false,
      labelWrapperClassName,
      ...rest
    } = componentProps;
    const [checked, setChecked] = useState(checkedProp ?? defaultChecked);
    const groupContext = useCheckboxGroupContext();
    const {
      checked: checkedIcon = SvgCheckBox,
      indeterminate: indeterminateIcon = SvgIndeterminateCheckBox,
      unchecked: uncheckedIcon = SvgCheckBoxOutlineBlank
    } = { ...icons };
    const isControlled = typeof checkedProp !== "undefined";
    const onChangeHandler = (e) => {
      if (!isControlled) {
        setChecked(e.target.checked);
      }
      onChange?.(e, { value, checked: checkedProp ?? e.target.checked });
    };
    const onClickHandler = (e) => {
      onClick?.(e, {
        value,
        checked: checkedProp ?? e.target.checked
      });
    };
    const helperUid = useId();
    const hasError = error || groupContext?.hasError;
    const wrapperClassNames = cx(
      checkboxStyles["wrapper"],
      { [checkboxStyles["focus"]]: hideCheckbox },
      className
    );
    const checkboxClassNames = cx(checkboxStyles["checkbox"], {
      [checkboxStyles["checkbox-error"]]: hasError,
      [checkboxStyles["checkbox-checked"]]: isControlled ? checkedProp : checked,
      [checkboxStyles["checkbox-indeterminate"]]: indeterminate
    });
    const styleCombined = {
      ...style,
      ...layoutStyles
    };
    return /* @__PURE__ */ jsxs(
      "div",
      {
        className: wrapperClassNames,
        style: styleCombined,
        "data-anv": "checkbox",
        children: [
          /* @__PURE__ */ jsxs("label", { className: checkboxClassNames, children: [
            /* @__PURE__ */ jsx(
              "input",
              {
                type: "checkbox",
                disabled,
                value,
                onChange: onChangeHandler,
                onClick: onClickHandler,
                "aria-checked": indeterminate ? "mixed" : isControlled ? checkedProp : checked,
                checked: isControlled ? checkedProp : void 0,
                defaultChecked,
                "aria-invalid": hasError,
                "aria-describedby": description ? helperUid : void 0,
                ref,
                ...rest
              }
            ),
            /* @__PURE__ */ jsx(
              "span",
              {
                className: cx(checkboxStyles["icon-wrapper"], {
                  [checkboxStyles["sr-only"]]: hideCheckbox
                }),
                "aria-hidden": true,
                children: /* @__PURE__ */ jsx(
                  Icon,
                  {
                    size: "large",
                    svg: indeterminate ? indeterminateIcon : (isControlled ? checkedProp : checked) ? checkedIcon : uncheckedIcon,
                    className: checkboxStyles["icon"]
                  }
                )
              }
            ),
            /* @__PURE__ */ jsxs(
              "span",
              {
                className: cx(labelWrapperClassName, {
                  [checkboxStyles["focus-label"]]: hideCheckbox
                }),
                children: [
                  label,
                  required && /* @__PURE__ */ jsx("span", { "aria-hidden": true, className: checkboxStyles["required-asterisk"], children: "*" })
                ]
              }
            )
          ] }),
          description ? /* @__PURE__ */ jsx("div", { className: checkboxStyles["helper-wrapper"], children: /* @__PURE__ */ jsx(Helper, { id: helperUid, description }) }) : null
        ]
      }
    );
  }
);
CheckboxElement.displayName = "Checkbox";
const Checkbox = CheckboxElement;

export { Checkbox as C, CheckboxGroup as a };
//# sourceMappingURL=Checkbox-C3rtd-Xe.js.map
