import { n as AnalyticsId } from "../../constants2.js";
import { t as AnchorLink_default } from "../../AnchorLink.js";
import { r as isComponentWithDisplayName, t as isComponent } from "../../component.js";
import { t as Checkbox_default } from "../../Checkbox2.js";
import { t as Title_default } from "../../Title2.js";
import { t as Select_default } from "../../Select.js";
import { t as Slider_default } from "../../Slider.js";
import { t as Textarea_default } from "../../Textarea.js";
import { n as FormGroup_default } from "../../FormGroup.js";
import { t as Input_default } from "../../Input2.js";
import { t as List_default } from "../../List.js";
import { t as RadioButton_default } from "../../RadioButton.js";
import classNames from "classnames";
import React, { useId } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import styles from "./styles.module.scss";
var ErrorListItem = (props) => {
	const handleClick = (event, element) => {
		event?.preventDefault();
		if (element?.focus) element?.focus();
	};
	if (props.error.ref) return /* @__PURE__ */ jsx(AnchorLink_default, {
		href: `#${props.name}`,
		onClick: (e) => handleClick(e, props.error.ref),
		children: props.error.message
	});
	return /* @__PURE__ */ jsx(Fragment, { children: props.error.message });
};
var ErrorListItem_default = ErrorListItem;
var ErrorList = (props) => /* @__PURE__ */ jsx(List_default, { children: Object.entries(props.errors).map(([name, error]) => /* @__PURE__ */ jsx(List_default.Item, { children: /* @__PURE__ */ jsx(ErrorListItem_default, {
	name,
	error
}) }, name)) });
var ErrorList_default = ErrorList;
var ValidationSummary = (props) => {
	const { errorTitleHtmlMarkup = "h2" } = props;
	const titleId = useId();
	const hasErrors = !!props.errors && Object.entries(props.errors).length > 0;
	const visuallyHidden = props.visuallyHidden;
	const summaryClasses = classNames(styles["validation__summary"], {
		[styles["validation__summary--visible"]]: hasErrors && !visuallyHidden,
		[styles["validation__summary--sr-only"]]: visuallyHidden
	});
	return /* @__PURE__ */ jsxs("div", {
		role: "status",
		"aria-atomic": "true",
		"aria-live": "polite",
		"aria-relevant": "all",
		"aria-labelledby": hasErrors && props.errorTitle ? titleId : void 0,
		className: summaryClasses,
		children: [hasErrors && /* @__PURE__ */ jsxs(Fragment, { children: [props.errorTitle && /* @__PURE__ */ jsx(Title_default, {
			appearance: "title4",
			id: titleId,
			htmlMarkup: errorTitleHtmlMarkup,
			margin: {
				marginTop: 0,
				marginBottom: 1
			},
			children: props.errorTitle
		}), /* @__PURE__ */ jsx(ErrorList_default, { errors: props.errors })] }), props.children]
	});
};
var ValidationSummary_default = ValidationSummary;
const Validation = (props) => {
	const validationErrorClass = styles["validation__error-wrapper"];
	const cloneFormElement = (child) => {
		return React.cloneElement(child, {
			...child.props,
			errorWrapperClassName: validationErrorClass
		});
	};
	const renderChild = (child) => {
		if (isComponent(child, FormGroup_default) || isComponent(child, Checkbox_default) || isComponent(child, RadioButton_default) || isComponent(child, Textarea_default) || isComponent(child, Input_default) || isComponent(child, Select_default) || isComponent(child, Slider_default) || isComponentWithDisplayName(child, "DateTimePickerWrapper") || isComponentWithDisplayName(child, "DatePicker") || isComponentWithDisplayName(child, "DateTime")) return cloneFormElement(child);
		if (isComponent(child, React.Fragment)) return React.Children.map(child.props.children, (child$1) => {
			return renderChild(child$1);
		});
		return child;
	};
	return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("div", {
		"data-testid": props.testId,
		"data-analyticsid": AnalyticsId.Validation,
		className: props.className,
		ref: props.ref,
		children: React.Children.map(props.children, (child) => renderChild(child))
	}), /* @__PURE__ */ jsx(ValidationSummary_default, {
		errorTitle: props.errorTitle,
		errors: props.errors,
		visuallyHidden: props.visuallyHiddenSummary
	})] });
};
Validation.displayName = "Validation";
var Validation_default = Validation;
export { Validation, Validation_default as default };

//# sourceMappingURL=index.js.map