import { n as useTranslation } from "../i18n-DoP1NB7u.js";
import { d as T, v as u, x as k } from "../vendor-preact-DThS2ZQ_.js";
import { c as useManager, o as useModuleTerminalCallbacks } from "../hooks-Da_yP_Dr.js";
import { t as IncodeComponent } from "../incodeComponent-CCP7GTMj.js";
import { r as Spacer } from "../loadingIcon-B0ysgiNq.js";
import { t as Page } from "../page-DidH_RkX.js";
import { t as TransitionSpinner } from "../transitionSpinner-dYLkc7PO.js";
import { t as Button } from "../button-BMYTQ8fc.js";
import { t as ErrorIcon } from "../errorIcon-BA4T6iy-.js";
import "../icons-CJJbIpBq.js";
import { t as SuccessIcon } from "../successIcon-B5mGvdx1.js";
import { t as AutoFocusTitle } from "../autoFocusTitle-CuRu2lPg.js";
import { t as InputComposed } from "../inputComposed-C1R2LAa3.js";
import { t as DropdownComposed } from "../dropdownComposed-ZdNkbyDs.js";
import "../dropdownComposed-Db7dKNuM.js";
import { t as DateInputComposed } from "../dateInputComposed-DuaofcBl.js";
import "../dateInputComposed-DRzZi6uy.js";
import { t as registerIncodeModuleElement } from "../incodeModule-M8DszCee.js";
import { n as getRequiredHintMode, t as getFieldHint } from "../requiredLabelHint-DX3oVNtu.js";
import { GENERATE_CURP_FIELDS, createCurpValidationManager, mexicanStates } from "@incodetech/core/curp-validation";
//#region src/modules/curp-validation/curpInput.tsx
var CurpInput = ({ curp, errorKey, onCurpChange, onCurpBlur, onVerify, onSwitchToGenerate, disabled = false, placeholder, continueLabel, dontHaveLabel }) => {
	const { t } = useTranslation();
	const displayPlaceholder = placeholder ?? t("curp.placeholder.curp") ?? "CURP";
	const displayContinueLabel = continueLabel ?? t("curp.continue") ?? "Continue";
	const displayDontHaveLabel = dontHaveLabel ?? t("curp.dontHave") ?? "I don't have my CURP";
	const errorMessage = errorKey ? t(errorKey) : void 0;
	const isContinueDisabled = curp.length === 0 || !!errorMessage || disabled;
	const handleInput = (e) => {
		const target = e.target;
		onCurpChange(target.value.toUpperCase());
	};
	return /* @__PURE__ */ u("div", {
		class: "IncodeCurpValidation IncodeCurpInput",
		children: [
			/* @__PURE__ */ u(InputComposed, {
				id: "curp-input",
				value: curp,
				onInput: handleInput,
				onBlur: onCurpBlur,
				placeholder: displayPlaceholder,
				disabled,
				required: true,
				maxLength: 18,
				"aria-label": t("curp.placeholder.curp") ?? "CURP",
				error: errorMessage,
				showErrorIcon: !!errorMessage
			}),
			/* @__PURE__ */ u(Spacer, {}),
			/* @__PURE__ */ u(Button, {
				type: "button",
				onClick: onVerify,
				disabled: isContinueDisabled,
				"data-testid": "curp-verify",
				children: displayContinueLabel
			}),
			/* @__PURE__ */ u(Button, {
				type: "button",
				variant: "secondary",
				onClick: onSwitchToGenerate,
				disabled,
				"data-testid": "curp-switch-to-generate",
				children: displayDontHaveLabel
			})
		]
	});
};
//#endregion
//#region src/modules/curp-validation/generateCurpForm.tsx
var GenerateCurpFormComponent = ({ form, validationErrors, onFormChange, onValidateField, onGenerate, disabled = false, labels: customLabels }) => {
	const { t } = useTranslation();
	const labels = {
		firstName: t("curp.labels.firstName") ?? "Your First name",
		firstLast: t("curp.labels.firstLast") ?? "Your First last name",
		secondLast: t("curp.labels.secondLast") ?? "Your Second last name",
		gender: t("curp.labels.genderV2") ?? "Gender",
		genderPlaceholder: t("curp.placeholder.gender") ?? "Choose gender",
		male: t("curp.labels.maleV2") ?? "Male",
		female: t("curp.labels.femaleV2") ?? "Female",
		other: t("curp.labels.other") ?? "Other",
		dob: t("curp.labels.dob") ?? "Date of birth",
		birthState: t("curp.labels.birthState") ?? "State of birth",
		statePlaceholder: t("curp.placeholder.state") ?? "Select state",
		generate: t("curp.generateCta") ?? "Generate CURP",
		...customLabels
	};
	const mode = T(() => getRequiredHintMode(GENERATE_CURP_FIELDS.map((f) => f.required)), []);
	const hasValidationErrors = !!validationErrors && Object.keys(validationErrors).length > 0;
	const handleTextChange = (field) => (e) => {
		const target = e.target;
		onFormChange({ [field]: target.value });
	};
	const handleBlur = (field) => () => {
		onValidateField(field);
	};
	const genderOptions = [
		{
			value: "H",
			label: labels.male
		},
		{
			value: "M",
			label: labels.female
		},
		{
			value: "X",
			label: labels.other
		}
	];
	const stateOptions = mexicanStates.map((s) => ({
		value: s.value,
		label: s.label
	}));
	const labelForField = (key) => {
		switch (key) {
			case "name": return labels.firstName;
			case "firstLastName": return labels.firstLast;
			case "secondLastName": return labels.secondLast;
			case "gender": return labels.gender;
			case "birthDate": return labels.dob;
			case "birthState": return labels.birthState;
		}
	};
	const renderField = (field) => {
		const hint = getFieldHint(mode, field.required);
		const labelHint = hint ? t(`forms.label.${hint}`) ?? void 0 : void 0;
		const errorKey = validationErrors?.[field.key];
		const errorMessage = errorKey ? t(errorKey) ?? void 0 : void 0;
		const label = labelForField(field.key);
		if (field.type === "dropdown") {
			const options = field.key === "gender" ? genderOptions : stateOptions;
			const placeholder = field.key === "gender" ? labels.genderPlaceholder : labels.statePlaceholder;
			return /* @__PURE__ */ u(DropdownComposed, {
				id: `curp-${field.key === "birthState" ? "birth-state" : "gender"}`,
				label,
				labelHint,
				placeholder,
				value: form[field.key],
				options,
				disabled,
				required: field.required,
				onChange: (value) => {
					if (field.key === "gender") onFormChange({ gender: value });
					else onFormChange({ [field.key]: value });
				},
				onBlur: handleBlur(field.key),
				error: errorMessage,
				showErrorIcon: !!errorMessage,
				"data-testid": field.key === "gender" ? "curp-gender" : "curp-birth-state",
				...field.key === "birthState" ? { searchable: true } : {}
			}, field.key);
		}
		if (field.type === "date") return /* @__PURE__ */ u(DateInputComposed, {
			id: "curp-birth-date",
			label,
			labelHint,
			value: form.birthDate,
			onInput: handleTextChange("birthDate"),
			onBlur: handleBlur("birthDate"),
			max: (/* @__PURE__ */ new Date()).toISOString().split("T")[0],
			disabled,
			required: field.required,
			error: errorMessage,
			showErrorIcon: !!errorMessage,
			"data-testid": "curp-birth-date",
			...field.autoComplete ? { autoComplete: field.autoComplete } : {}
		}, field.key);
		return /* @__PURE__ */ u(InputComposed, {
			id: field.key === "name" ? "curp-first-name" : field.key === "firstLastName" ? "curp-first-last" : "curp-second-last",
			label,
			labelHint,
			value: form[field.key],
			onInput: handleTextChange(field.key),
			onBlur: handleBlur(field.key),
			placeholder: label,
			disabled,
			required: field.required,
			error: errorMessage,
			showErrorIcon: !!errorMessage,
			...field.autoComplete ? { autoComplete: field.autoComplete } : {}
		}, field.key);
	};
	return /* @__PURE__ */ u("div", {
		class: "IncodeCurpValidation IncodeGenerateCurpForm",
		children: [
			GENERATE_CURP_FIELDS.map(renderField),
			/* @__PURE__ */ u(Spacer, { size: 16 }),
			/* @__PURE__ */ u(Button, {
				type: "button",
				onClick: onGenerate,
				disabled: hasValidationErrors || disabled,
				"data-testid": "curp-generate",
				children: labels.generate
			})
		]
	});
};
//#endregion
//#region src/modules/curp-validation/curp-validation.tsx
var CurpValidationContent = ({ config, manager: externalManager, onFinish, onError }) => {
	const { t } = useTranslation();
	const [state, manager] = useManager(() => {
		if (externalManager) return externalManager;
		if (!config) throw new Error("CurpValidation config is required when no manager is provided");
		return createCurpValidationManager({ config });
	}, { manageLifecycle: !externalManager });
	useModuleTerminalCallbacks({
		status: state.status,
		onFinish,
		onError
	});
	if (state.status === "idle" || state.status === "loading") return /* @__PURE__ */ u(TransitionSpinner, { hideText: true });
	if (state.status === "verifying" || state.status === "generating") return /* @__PURE__ */ u(TransitionSpinner, { spinnerConfig: { title: state.status === "generating" ? t("curp.status.generating") ?? "Generating..." : t("curp.status.verifying") ?? "Verifying..." } });
	if (state.status === "enterCurp") return /* @__PURE__ */ u(Page, {
		className: "IncodeCurpValidationPage",
		title: t("curp.enterCurp") ?? "Enter your CURP",
		children: /* @__PURE__ */ u(CurpInput, {
			curp: state.curp,
			errorKey: state.validationErrors?.curp,
			onCurpChange: (curp) => manager.setCurp(curp),
			onCurpBlur: () => manager.validateField("curp"),
			onVerify: () => manager.verify(),
			onSwitchToGenerate: () => manager.switchToGenerate(),
			placeholder: t("curp.placeholder.curp") ?? "CURP",
			continueLabel: t("curp.continue") ?? "Continue",
			dontHaveLabel: t("curp.dontHave") ?? "I don't have my CURP"
		})
	});
	if (state.status === "generateCurp") return /* @__PURE__ */ u(Page, {
		className: "IncodeCurpValidationPage",
		title: t("curp.generate") ?? "Generate your CURP",
		hideFooterBranding: true,
		children: /* @__PURE__ */ u(GenerateCurpFormComponent, {
			form: state.form,
			validationErrors: state.validationErrors,
			onFormChange: (form) => manager.setGenerateForm(form),
			onValidateField: (field) => manager.validateField(field),
			onGenerate: () => manager.generate()
		})
	});
	if (state.status === "confirmCurp") return /* @__PURE__ */ u(Page, {
		className: "IncodeCurpValidationPage",
		title: t("curp.status.confirm") ?? "Confirm your CURP",
		children: /* @__PURE__ */ u("div", {
			class: "IncodeCurpValidation IncodeCurpConfirm",
			children: [
				/* @__PURE__ */ u(InputComposed, {
					id: "curp-confirm-input",
					value: state.curp,
					readOnly: true,
					"aria-label": t("curp.status.generatedCurp") ?? "Generated CURP",
					"data-testid": "curp-confirm-input"
				}),
				/* @__PURE__ */ u(Spacer, {}),
				/* @__PURE__ */ u(Button, {
					type: "button",
					onClick: () => manager.confirmGenerated(),
					"data-testid": "curp-confirm",
					children: t("curp.continue") ?? "Continue"
				})
			]
		})
	});
	if (state.status === "success") return /* @__PURE__ */ u(Page, {
		className: "IncodeCurpValidationPage",
		children: /* @__PURE__ */ u("div", {
			class: "IncodeCurpValidation IncodeCurpSuccess",
			children: [
				/* @__PURE__ */ u(SuccessIcon, { size: 64 }),
				/* @__PURE__ */ u(Spacer, { size: 16 }),
				/* @__PURE__ */ u(AutoFocusTitle, {
					className: "IncodeCurpSuccessTitle",
					children: t("curp.status.verified") ?? "Verified"
				})
			]
		})
	});
	if (state.status === "failure" || state.status === "generateError") {
		const canRetry = state.retriesLeft > 0;
		const title = state.status === "failure" ? t("curp.status.notVerified") ?? "Not verified" : t("curp.status.couldntGenerate") ?? "Couldn't generate CURP";
		const failureRetryLabel = state.status === "failure" && state.fromAutoVerify ? t("curp.verifyManually") : t("curp.status.tryAgain");
		return /* @__PURE__ */ u(Page, {
			className: "IncodeCurpValidationPage",
			children: /* @__PURE__ */ u("div", {
				class: "IncodeCurpValidation IncodeCurpFailure",
				children: [
					/* @__PURE__ */ u(Spacer, {}),
					/* @__PURE__ */ u(ErrorIcon, { size: 64 }),
					/* @__PURE__ */ u(Spacer, { size: 16 }),
					/* @__PURE__ */ u(AutoFocusTitle, {
						className: "IncodeCurpFailureTitle",
						children: title
					}),
					state.status === "generateError" && canRetry && /* @__PURE__ */ u(k, { children: [/* @__PURE__ */ u(Spacer, { size: 12 }), /* @__PURE__ */ u("p", {
						class: "IncodeCurpFailureHint",
						role: "alert",
						children: t("curp.status.checkInfo") ?? "Please check your information"
					})] }),
					/* @__PURE__ */ u(Spacer, {}),
					canRetry && /* @__PURE__ */ u(k, { children: /* @__PURE__ */ u(Button, {
						type: "button",
						onClick: () => manager.retry(),
						"data-testid": "curp-retry",
						children: state.status === "failure" ? failureRetryLabel : t("curp.status.edit") ?? "Edit information"
					}) })
				]
			})
		});
	}
	if (state.status === "finished" || state.status === "closed") return null;
	return null;
};
var CurpValidation = ({ config, manager, onFinish, onError }) => {
	return /* @__PURE__ */ u(IncodeComponent, { children: config || manager ? /* @__PURE__ */ u(CurpValidationContent, {
		config,
		manager,
		onFinish,
		onError
	}) : null });
};
registerIncodeModuleElement(CurpValidation, "incode-curp-validation");
//#endregion
export { CurpValidation };
