import { n as useTranslation } from "./i18n-DoP1NB7u.js";
import { d as T, v as u } 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 { t as Page } from "./page-DidH_RkX.js";
import "./extensibility-DesSYynI.js";
import { t as Button } from "./button-BMYTQ8fc.js";
import { t as VerificationResult } from "./verificationResult-Brj8WSyW.js";
import { t as InputComposed } from "./inputComposed-C1R2LAa3.js";
import { t as RadioGroupComposed } from "./radioGroupComposed-TuAWUAFK.js";
import { t as DateInputComposed } from "./dateInputComposed-DuaofcBl.js";
import { t as registerIncodeModuleElement } from "./incodeModule-M8DszCee.js";
import { n as getRequiredHintMode, t as getFieldHint } from "./requiredLabelHint-DX3oVNtu.js";
import { createIdOcrManager, getOcrFields } from "@incodetech/core/id-ocr";
//#region src/modules/id-ocr/idOcrForm.tsx
var TEXT_FIELD_COMPONENTS = {
	text: InputComposed,
	date: DateInputComposed
};
var IdOcrForm = ({ fields, data, validationErrors, isReadonly, onChange, onBlur, onContinue }) => {
	const { t } = useTranslation();
	const hasValidationErrors = validationErrors && Object.keys(validationErrors).length > 0;
	const visibleFields = fields.filter((field) => {
		if (field.key === "issuingCountry") return isReadonly === true;
		if (field.key === "documentNumber" && isReadonly) return !!data.documentNumber;
		return true;
	});
	const mode = T(() => getRequiredHintMode(visibleFields.map((f) => isReadonly ? false : f.required)), [visibleFields, isReadonly]);
	const renderField = (field) => {
		const value = data[field.key] ?? "";
		const fieldErrorKey = validationErrors?.[field.key];
		const fieldError = fieldErrorKey ? t(fieldErrorKey) : void 0;
		const isFieldRequired = isReadonly ? false : field.required;
		const hint = getFieldHint(mode, isFieldRequired);
		const labelHint = hint ? t(`forms.label.${hint}`) : void 0;
		if (field.type === "radio") return /* @__PURE__ */ u(RadioGroupComposed, {
			id: `id-ocr-${field.key}`,
			class: "IncodeIdOcrRadioGroup",
			label: t(field.labelKey),
			labelHint,
			value,
			options: (field.options ?? []).map((option) => ({
				value: option.value,
				label: t(option.label)
			})),
			required: isFieldRequired,
			disabled: isReadonly,
			onChange: (next) => onChange(field.key, next)
		}, field.key);
		const Component = TEXT_FIELD_COMPONENTS[field.type];
		const max = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
		return /* @__PURE__ */ u(Component, {
			id: `id-ocr-${field.key}`,
			label: t(field.labelKey),
			labelHint,
			value,
			disabled: isReadonly,
			required: isFieldRequired,
			error: fieldError,
			showErrorIcon: !!fieldError,
			onInput: (e) => onChange(field.key, e.target.value),
			onBlur: isReadonly || !onBlur ? void 0 : () => onBlur(field.key),
			...field.type === "date" && { max }
		}, field.key);
	};
	return /* @__PURE__ */ u("div", {
		className: "IncodeIdOcrForm",
		children: [visibleFields.map(renderField), /* @__PURE__ */ u("div", {
			className: "IncodeIdOcrButton",
			children: /* @__PURE__ */ u(Button, {
				onClick: onContinue,
				disabled: hasValidationErrors,
				children: t("common.continue")
			})
		})]
	});
};
//#endregion
//#region src/modules/id-ocr/idOcr.tsx
var IdOcrContent = ({ config, onFinish }) => {
	const { t } = useTranslation();
	const [state, manager] = useManager(() => createIdOcrManager({ config }));
	useModuleTerminalCallbacks({
		status: state.status === "finished" ? "finished" : "loading",
		onFinish
	});
	if (state.status === "finished") return null;
	if (state.status === "success") return /* @__PURE__ */ u(VerificationResult, {
		status: "success",
		successTitle: t("idOcr.successTitle")
	});
	if (state.status === "error") return /* @__PURE__ */ u(VerificationResult, {
		status: "failure",
		onRetry: () => manager.retry()
	});
	if (state.status === "loading" || state.status === "submitting") return /* @__PURE__ */ u(VerificationResult, {
		status: "submitting",
		submittingTitle: t("common.holdOnASec"),
		submittingSubtitle: t("idOcr.loadingSubtitle")
	});
	if (state.status === "readonly" || state.status === "inputting") return /* @__PURE__ */ u(Page, {
		title: t("idOcr.title"),
		className: "IncodeIdOcrPage",
		hideFooterBranding: true,
		children: /* @__PURE__ */ u(IdOcrForm, {
			fields: getOcrFields(config),
			data: state.formData,
			validationErrors: state.status === "inputting" ? state.validationErrors : void 0,
			isReadonly: state.status === "readonly",
			onChange: (field, value) => manager.setField(field, value),
			onBlur: state.status === "inputting" ? (field) => manager.validateField(field) : void 0,
			onContinue: () => manager.continue()
		}, state.ocrDocument)
	});
	return null;
};
var IdOcr = ({ config, onFinish }) => /* @__PURE__ */ u(IncodeComponent, { children: config ? /* @__PURE__ */ u(IdOcrContent, {
	config,
	onFinish
}) : null });
registerIncodeModuleElement(IdOcr, "incode-id-ocr");
//#endregion
export { IdOcr };
