import { n as useTranslation } from "../i18n-DoP1NB7u.js";
import { _ as y, p as d, 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 { r as Spacer } from "../loadingIcon-B0ysgiNq.js";
import { t as Page } from "../page-DidH_RkX.js";
import "../extensibility-DesSYynI.js";
import { t as Title } from "../typography-BZIpjo97.js";
import { t as Button } from "../button-BMYTQ8fc.js";
import "../icons-CJJbIpBq.js";
import { t as SuccessIcon } from "../successIcon-B5mGvdx1.js";
import { t as VerificationResult } from "../verificationResult-Brj8WSyW.js";
import { t as InputComposed } from "../inputComposed-C1R2LAa3.js";
import { t as registerIncodeModuleElement } from "../incodeModule-M8DszCee.js";
import { MIN_PASSWORD_LENGTH, PASSWORD_VALIDATION_KEYS, REQUIREMENT_I18N_KEYS, createCertificateIssuanceManager } from "@incodetech/core/certificate-issuance";
//#region src/modules/certificate-issuance/passwordRequirements.tsx
var PasswordRequirements = ({ validations }) => {
	const { t } = useTranslation();
	return /* @__PURE__ */ u("ul", {
		class: "IncodeCertificateIssuanceRequirements",
		"aria-live": "polite",
		"data-testid": "password-requirements",
		children: PASSWORD_VALIDATION_KEYS.map((key) => {
			const passed = validations[key];
			return /* @__PURE__ */ u("li", {
				class: "IncodeCertificateIssuanceRequirement",
				"data-testid": `password-requirement-${key}`,
				"data-met": passed,
				children: [
					/* @__PURE__ */ u("span", {
						class: "IncodeCertificateIssuanceRequirementIcon",
						"aria-hidden": "true",
						children: /* @__PURE__ */ u(SuccessIcon, { size: 16 })
					}),
					/* @__PURE__ */ u("span", {
						class: "IncodeCertificateIssuanceRequirementLabel",
						children: t(REQUIREMENT_I18N_KEYS[key], { min: MIN_PASSWORD_LENGTH })
					}),
					/* @__PURE__ */ u("span", {
						class: "IncodeSrOnly",
						children: passed ? t("certificateIssuance.password.requirementMet") : t("certificateIssuance.password.requirementNotMet")
					})
				]
			}, key);
		})
	});
};
//#endregion
//#region src/modules/certificate-issuance/certificate-issuance.tsx
var CertificateIssuanceDownloadScreen = ({ blob, onFinish }) => {
	const { t } = useTranslation();
	const [downloadUrl, setDownloadUrl] = d();
	y(() => {
		const url = URL.createObjectURL(blob);
		setDownloadUrl(url);
		return () => {
			URL.revokeObjectURL(url);
		};
	}, [blob]);
	return /* @__PURE__ */ u(Page, {
		className: "IncodeCertificateIssuancePage",
		hideFooterBranding: true,
		children: /* @__PURE__ */ u("div", {
			className: "IncodeCertificateIssuanceDownloadContent",
			children: [
				/* @__PURE__ */ u(SuccessIcon, { size: 64 }),
				/* @__PURE__ */ u(Spacer, { size: 16 }),
				/* @__PURE__ */ u(Title, { children: t("forms.success") }),
				/* @__PURE__ */ u(Spacer, {}),
				/* @__PURE__ */ u("div", {
					className: "IncodeCertificateIssuanceDownloadArea",
					children: [
						/* @__PURE__ */ u(Title, { children: t("certificateIssuance.download.title") }),
						/* @__PURE__ */ u("span", {
							className: "IncodeCertificateIssuanceDownloadText",
							children: t("certificateIssuance.download.text")
						}),
						downloadUrl ? /* @__PURE__ */ u("a", {
							class: "IncodeButton IncodeButtonPrimary IncodeButtonL IncodeCertificateIssuanceDownloadButton",
							href: downloadUrl,
							download: "certificate.cer",
							children: t("certificateIssuance.download.button")
						}) : /* @__PURE__ */ u(Button, {
							disabled: true,
							children: t("certificateIssuance.download.button")
						})
					]
				}),
				/* @__PURE__ */ u(Spacer, {}),
				/* @__PURE__ */ u(Button, {
					variant: "link",
					onClick: onFinish,
					children: t("certificateIssuance.download.finish")
				})
			]
		})
	});
};
var CertificateIssuanceContent = ({ config, manager: externalManager, onFinish }) => {
	const { t } = useTranslation();
	const [state, manager] = useManager(() => {
		if (externalManager) return externalManager;
		return createCertificateIssuanceManager({ config });
	}, { manageLifecycle: !externalManager });
	y(() => {
		if (state.status === "idle") manager.load();
	}, [state.status, manager]);
	useModuleTerminalCallbacks({
		status: state.status,
		onFinish,
		errorStatuses: []
	});
	if (state.status === "finished") return null;
	if (state.status === "idle" || state.status === "processing") return /* @__PURE__ */ u(VerificationResult, {
		submittingTitle: t("common.processing"),
		submittingSubtitle: t("qes.processing.subtitle"),
		status: "submitting",
		hideFooterBranding: false
	});
	if (state.status === "success") return /* @__PURE__ */ u(CertificateIssuanceDownloadScreen, {
		blob: state.blob,
		onFinish: () => manager.finish()
	});
	if (state.status === "error") return /* @__PURE__ */ u(VerificationResult, {
		failureTitle: t("certificateIssuance.errorTitle"),
		status: "failure",
		hideFooterBranding: false
	});
	if (state.status === "password") return /* @__PURE__ */ u(Page, {
		title: t("certificateIssuance.password.title"),
		subtitle: t("certificateIssuance.password.description"),
		className: "IncodeCertificateIssuancePage",
		children: /* @__PURE__ */ u("div", {
			className: "IncodeCertificateIssuanceContent",
			children: [/* @__PURE__ */ u("div", {
				className: "IncodeCertificateIssuancePasswordSection",
				children: /* @__PURE__ */ u("div", {
					className: "IncodeCertificateIssuancePassword",
					children: [/* @__PURE__ */ u(InputComposed, {
						id: "certificate-password",
						"data-testid": "certificate-password",
						type: "password",
						placeholder: t("certificateIssuance.password.placeholder"),
						value: state.password,
						autocomplete: "new-password",
						onInput: (e) => {
							const target = e.target;
							manager.setPassword(target.value);
						},
						closeButton: {
							show: state.password.length > 0,
							onClose: () => manager.setPassword("")
						}
					}), /* @__PURE__ */ u(PasswordRequirements, { validations: state.passwordValidations })]
				})
			}), /* @__PURE__ */ u(Button, {
				disabled: !state.canSubmit,
				onClick: () => manager.submitPassword(),
				children: t("certificateIssuance.continue")
			})]
		})
	});
	return null;
};
var CertificateIssuance = ({ config, manager, onFinish }) => {
	if (!config) return null;
	return /* @__PURE__ */ u(IncodeComponent, { children: /* @__PURE__ */ u(CertificateIssuanceContent, {
		config,
		manager,
		onFinish
	}) });
};
registerIncodeModuleElement(CertificateIssuance, "incode-certificate-issuance");
//#endregion
export { CertificateIssuance };
