import { n as useTranslation } from "../i18n-DoP1NB7u.js";
import { d as T, v as u } from "../vendor-preact-DThS2ZQ_.js";
import { a as isTerminalModuleState, 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 { t as TransitionSpinner } from "../transitionSpinner-dYLkc7PO.js";
import { t as Button } from "../button-BMYTQ8fc.js";
import { t as Checkbox } from "../checkbox-1wUHSoaK.js";
import { t as registerIncodeModuleElement } from "../incodeModule-M8DszCee.js";
import { t as RichText } from "../richText-DsTP4H3j.js";
import { n as getRequiredHintMode, t as getFieldHint } from "../requiredLabelHint-DX3oVNtu.js";
import { createConsentManager } from "@incodetech/core/consent";
//#region src/modules/consent/consent.tsx
var stopLinkPropagation = (event) => {
	if (event.target.closest("a")) event.stopPropagation();
};
var ConsentContent = ({ config, manager: externalManager, onFinish, onError }) => {
	const { t, i18n } = useTranslation();
	const [state, manager] = useManager(() => {
		if (externalManager) return externalManager;
		if (!config) throw new Error("Consent config is required");
		return createConsentManager({ config: {
			...config,
			language: i18n.language
		} });
	}, { manageLifecycle: !externalManager });
	const checkboxes = "checkboxes" in state ? state.checkboxes : void 0;
	const mode = T(() => getRequiredHintMode(checkboxes?.map((c) => c.required) ?? []), [checkboxes]);
	useModuleTerminalCallbacks({
		status: state.status,
		onFinish,
		onError,
		error: state.status === "error" ? state.error : void 0
	});
	if (isTerminalModuleState(state)) return null;
	if (state.status === "idle" || state.status === "loading") return /* @__PURE__ */ u(TransitionSpinner, { hideText: true });
	return /* @__PURE__ */ u(Page, {
		className: "IncodeConsentPage",
		title: state.title,
		children: /* @__PURE__ */ u("div", {
			class: "IncodeConsentContent",
			children: [
				/* @__PURE__ */ u("div", {
					class: "IncodeConsentBody",
					children: [/* @__PURE__ */ u(RichText, {
						content: state.richText,
						class: "IncodeRichText"
					}), /* @__PURE__ */ u("div", {
						class: "IncodeConsentCheckboxList",
						role: "group",
						"aria-label": t("biometricConsent.consentOptions"),
						children: state.checkboxes.map((checkbox) => {
							const hint = getFieldHint(mode, checkbox.required);
							const labelHint = hint ? t(`forms.label.${hint}`) : void 0;
							return /* @__PURE__ */ u(Checkbox, {
								id: `consent-checkbox-${checkbox.id}`,
								class: "IncodeConsentCheckbox",
								size: "s",
								checked: checkbox.checked,
								required: checkbox.required,
								labelHint,
								onChange: () => manager.toggleCheckbox(checkbox.id),
								label: /* @__PURE__ */ u("span", {
									class: "IncodeConsentCheckboxLabel",
									role: "presentation",
									onClick: stopLinkPropagation,
									onKeyDown: stopLinkPropagation,
									children: /* @__PURE__ */ u(RichText, {
										content: checkbox.label,
										class: "IncodeRichText",
										disallowedTags: ["img"]
									})
								})
							}, checkbox.id);
						})
					})]
				}),
				state.status === "display" && state.error ? /* @__PURE__ */ u("p", {
					class: "IncodeConsentSubmitError",
					"data-testid": "consent-submit-error",
					role: "alert",
					children: state.error
				}) : null,
				/* @__PURE__ */ u(Button, {
					class: "IncodeConsentButton",
					"data-testid": "consent-submit",
					onClick: () => manager.submit(),
					disabled: !state.canSubmit,
					isLoading: state.status === "submitting",
					children: t("biometricConsent.button.continue")
				})
			]
		})
	}, "ready");
};
var Consent = ({ config, manager, onFinish, onError }) => {
	return /* @__PURE__ */ u(IncodeComponent, { children: config || manager ? /* @__PURE__ */ u(ConsentContent, {
		config,
		manager,
		onFinish,
		onError
	}) : null });
};
registerIncodeModuleElement(Consent, "incode-consent");
//#endregion
export { Consent };
