import { n as useTranslation } from "./i18n-DoP1NB7u.js";
import { _ as y, l as A, 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 { createMandatoryConsentManager } from "@incodetech/core/mandatory-consent";
//#region src/modules/mandatory-consent/mandatoryConsent.tsx
var stopLinkPropagation = (event) => {
	if (event.target.closest("a")) event.stopPropagation();
};
var MandatoryConsentContent = ({ config, manager: externalManager, onFinish, onError }) => {
	const { t, i18n } = useTranslation();
	const titleRef = A(null);
	const [state, manager] = useManager(() => {
		if (externalManager) return externalManager;
		if (!config) throw new Error("MandatoryConsent config is required when no manager is provided");
		return createMandatoryConsentManager({ config: {
			...config,
			language: i18n.language
		} });
	}, { manageLifecycle: !externalManager });
	y(() => {
		if (state.status === "display") titleRef.current?.focus();
	}, [state.status]);
	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 });
	const consentBody = state.text.split("\n").filter((line) => line.trim().length > 0).join(" ");
	const isMandatoryConsent = (config?.type ?? "MANDATORY") === "MANDATORY";
	return /* @__PURE__ */ u(Page, {
		className: "IncodeConsentPage",
		title: state.title,
		children: /* @__PURE__ */ u("div", {
			class: "IncodeConsentContent",
			children: [
				/* @__PURE__ */ u("div", {
					class: "IncodeConsentBody",
					children: [/* @__PURE__ */ u("p", {
						class: "IncodeConsentSubtitle",
						children: t("biometricConsent.subtitle")
					}), /* @__PURE__ */ u("div", {
						class: "IncodeConsentCheckboxList",
						children: /* @__PURE__ */ u(Checkbox, {
							id: "mandatory-consent-checkbox",
							class: "IncodeConsentCheckbox",
							checked: state.isSigned,
							required: true,
							onChange: () => manager.toggle(),
							label: /* @__PURE__ */ u("span", {
								class: "IncodeConsentCheckboxLabel",
								role: "presentation",
								onClick: stopLinkPropagation,
								onKeyDown: stopLinkPropagation,
								children: /* @__PURE__ */ u(RichText, {
									content: consentBody,
									class: "IncodeRichText"
								})
							})
						})
					})]
				}),
				state.status === "display" && state.error ? /* @__PURE__ */ u("p", {
					class: "IncodeConsentSubmitError",
					"data-testid": "mandatory-consent-submit-error",
					role: "alert",
					children: state.error
				}) : null,
				/* @__PURE__ */ u("div", {
					class: "IncodeConsentActions",
					children: [/* @__PURE__ */ u(Button, {
						"data-testid": "mandatory-consent-submit",
						class: !isMandatoryConsent ? "IncodeConsentButton" : void 0,
						onClick: () => manager.submit(),
						disabled: !state.canSubmit,
						isLoading: state.status === "submitting",
						children: t("biometricConsent.button.continue")
					}), isMandatoryConsent && /* @__PURE__ */ u(Button, {
						variant: "link",
						onClick: () => manager.cancel(),
						children: t("biometricConsent.button.cancel")
					})]
				})
			]
		})
	}, "ready");
};
var MandatoryConsent = ({ config, manager, onFinish, onError }) => {
	return /* @__PURE__ */ u(IncodeComponent, { children: config || manager ? /* @__PURE__ */ u(MandatoryConsentContent, {
		config,
		manager,
		onFinish,
		onError
	}) : null });
};
registerIncodeModuleElement(MandatoryConsent, "incode-mandatory-consent");
//#endregion
export { MandatoryConsent };
