import { n as useTranslation } from "./i18n-DoP1NB7u.js";
import { a as D, p as d, v as u } from "./vendor-preact-DThS2ZQ_.js";
import { n as cn } from "./loadingIcon-B0ysgiNq.js";
import { t as ErrorIcon } from "./errorIcon-BA4T6iy-.js";
import "./icons-CJJbIpBq.js";
import { t as InputComposed } from "./inputComposed-C1R2LAa3.js";
//#region src/shared/errorParams.ts
/**
* Resolves error params by translating the `fieldName` key
* when it holds an i18n key rather than a display string.
* Dot-separated strings are treated as i18n keys; plain strings
* (e.g. "CPF") are used as-is.
*/
function resolveErrorParams(t, params) {
	if (params?.fieldName && typeof params.fieldName === "string") {
		const name = String(params.fieldName);
		const resolved = name.includes(".") ? t(name) : name;
		return {
			...params,
			fieldName: resolved
		};
	}
	return params;
}
/**
* Translates an error key with resolved params.
* Returns `undefined` when there is no error.
*/
function translateError(t, error, params) {
	if (!error) return void 0;
	return t(error, resolveErrorParams(t, params));
}
//#endregion
//#region src/shared/formField/formField.tsx
var FormField = ({ name, label, displayLabel: displayLabelProp, value = "", error, errorParams, type = "text", placeholder, placeholderParams, required, readonly, maxLength, disabled, labelHint, onChange, onBlur }) => {
	const { t } = useTranslation();
	return /* @__PURE__ */ u("div", {
		className: "IncodeFormField",
		children: /* @__PURE__ */ u(InputComposed, {
			id: name,
			label: displayLabelProp ?? t(label),
			labelHint,
			name,
			type,
			value,
			placeholder: placeholder ? t(placeholder, placeholderParams) : void 0,
			readOnly: readonly,
			disabled,
			required,
			maxLength,
			error: translateError(t, error, errorParams),
			onInput: (e) => {
				const target = e.target;
				onChange(name, target.value);
			},
			onBlur: onBlur ? () => onBlur(name) : void 0,
			"data-testid": `form-field-${name}`
		})
	});
};
//#endregion
//#region src/shared/radioButtonComposed/radioButtonComposed.tsx
var RadioButtonComposed = D(({ id, label, labelHint, checked, defaultChecked, disabled = false, required = false, name, value, onChange, error, helper, showErrorIcon = true, class: className, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, "aria-invalid": ariaInvalid, ...restProps }, ref) => {
	const [internalChecked, setInternalChecked] = d(!!defaultChecked);
	const isControlled = checked !== void 0;
	const isChecked = isControlled ? !!checked : internalChecked;
	const isDisabled = !!disabled;
	const isRequired = !!required;
	const classStr = typeof className === "string" ? className : void 0;
	const handleChange = (event) => {
		if (!isControlled) setInternalChecked(event.target.checked);
		onChange?.(event);
	};
	const helperTextId = helper ? `${id}-helper` : void 0;
	const errorTextId = error ? `${id}-error` : void 0;
	const describedBy = [
		ariaDescribedBy,
		errorTextId,
		helperTextId
	].filter(Boolean).join(" ") || void 0;
	return /* @__PURE__ */ u("div", {
		class: cn("IncodeRadioButton", classStr),
		children: [
			/* @__PURE__ */ u("label", {
				class: cn("IncodeRadioButtonLabel", { IncodeRadioButtonLabelDisabled: isDisabled }),
				htmlFor: id,
				children: [/* @__PURE__ */ u("div", {
					class: "relative inline-flex",
					children: [/* @__PURE__ */ u("input", {
						ref,
						type: "radio",
						class: "IncodeRadioButtonInput",
						id,
						name,
						value,
						...isControlled ? { checked } : { defaultChecked },
						disabled,
						required,
						onChange: handleChange,
						"aria-label": ariaLabel,
						"aria-labelledby": ariaLabelledBy,
						"aria-describedby": describedBy,
						"aria-invalid": ariaInvalid || !!error,
						"aria-required": isRequired,
						...restProps
					}), /* @__PURE__ */ u("div", {
						class: cn("IncodeRadioButtonVisual", {
							IncodeRadioButtonVisualChecked: isChecked,
							IncodeRadioButtonVisualDisabled: isDisabled,
							IncodeRadioButtonVisualError: !!error
						}),
						"aria-hidden": "true",
						children: /* @__PURE__ */ u("span", { class: cn("IncodeRadioButtonDot", { IncodeRadioButtonDotVisible: isChecked }) })
					})]
				}), label && /* @__PURE__ */ u("span", {
					class: cn("IncodeRadioButtonText", { IncodeRadioButtonTextDisabled: isDisabled }),
					"aria-hidden": ariaLabelledBy ? "true" : void 0,
					children: [label, labelHint && /* @__PURE__ */ u("span", {
						class: "IncodeRadioButtonLabelHint",
						"data-testid": `${id}-label-hint`,
						children: labelHint
					})]
				})]
			}),
			error && /* @__PURE__ */ u("div", {
				class: "IncodeRadioButtonMessageWrapper",
				"data-testid": `${id}-error-wrapper`,
				children: [showErrorIcon && /* @__PURE__ */ u("div", {
					class: "IncodeRadioButtonIconWrapper",
					children: /* @__PURE__ */ u(ErrorIcon, { size: 17 })
				}), /* @__PURE__ */ u("p", {
					id: errorTextId,
					class: "IncodeRadioButtonErrorText",
					children: error
				})]
			}),
			helper && !error && /* @__PURE__ */ u("div", {
				class: "IncodeRadioButtonMessageWrapper",
				"data-testid": `${id}-helper-wrapper`,
				children: /* @__PURE__ */ u("p", {
					id: helperTextId,
					class: "IncodeRadioButtonHelperText",
					children: helper
				})
			})
		]
	});
});
//#endregion
//#region src/shared/radioGroupComposed/radioGroupComposed.tsx
var RadioGroupComposed = D(({ id, label, labelHint, options, value, defaultValue, error, helper, required = false, disabled = false, name, orientation = "row", onChange, onBlur, class: className, "data-testid": dataTestId }, ref) => {
	const [internalValue, setInternalValue] = d(defaultValue ?? "");
	const isControlled = value !== void 0;
	const currentValue = isControlled ? value : internalValue;
	const labelId = `${id}-label`;
	const errorId = error ? `${id}-error` : void 0;
	const helperId = helper && !error ? `${id}-helper` : void 0;
	const describedBy = errorId ?? helperId;
	const groupName = name ?? id;
	const handleChange = (optionValue, event) => {
		if (!isControlled) setInternalValue(optionValue);
		onChange?.(optionValue, event);
	};
	return /* @__PURE__ */ u("div", {
		ref,
		class: cn("IncodeRadioGroupComposed", className),
		"data-testid": dataTestId,
		children: [
			/* @__PURE__ */ u("span", {
				id: labelId,
				class: "IncodeRadioGroupComposedLabel",
				children: [label, labelHint && /* @__PURE__ */ u("span", {
					class: "IncodeRadioGroupComposedLabelHint",
					"data-testid": `${id}-label-hint`,
					children: labelHint
				})]
			}),
			/* @__PURE__ */ u("div", {
				class: "IncodeRadioGroupComposedOptions",
				role: "radiogroup",
				"aria-labelledby": labelId,
				"aria-required": required || void 0,
				"aria-invalid": error ? true : void 0,
				"aria-describedby": describedBy,
				"data-orientation": orientation,
				children: options.map((option) => /* @__PURE__ */ u(RadioButtonComposed, {
					id: `${id}-${option.value}`,
					name: groupName,
					label: option.label,
					value: option.value,
					checked: currentValue === option.value,
					disabled: disabled || option.disabled,
					showErrorIcon: false,
					"aria-invalid": error ? true : void 0,
					"aria-describedby": describedBy,
					onChange: (event) => handleChange(option.value, event),
					onBlur
				}, option.value))
			}),
			error && /* @__PURE__ */ u("p", {
				id: errorId,
				class: "IncodeRadioGroupComposedError",
				role: "alert",
				children: error
			}),
			helper && !error && /* @__PURE__ */ u("p", {
				id: helperId,
				class: "IncodeRadioGroupComposedHelper",
				children: helper
			})
		]
	});
});
//#endregion
export { translateError as i, RadioButtonComposed as n, FormField as r, RadioGroupComposed as t };
