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 { r as Spacer } from "./loadingIcon-B0ysgiNq.js";
import { t as Page } from "./page-DidH_RkX.js";
import { t as Spinner } from "./spinner-DqB4SGUV.js";
import "./spinner-V_B4DFE1.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 { createCpfOcrManager } from "@incodetech/core/cpf-ocr";
//#region src/modules/cpf-ocr/cpfOcr.tsx
var getInputState = (state) => {
	if (state.status === "inputting") return {
		cpf: state.cpf,
		isValid: state.isValid
	};
	return {
		cpf: "",
		isValid: false
	};
};
var CpfOcr = ({ manager: externalManager, onFinish, onError }) => {
	const [state, manager] = useManager(() => externalManager ?? createCpfOcrManager(), { manageLifecycle: !externalManager });
	const [hasBlurred, setHasBlurred] = d(false);
	const [hasEdited, setHasEdited] = d(false);
	const [isFocused, setIsFocused] = d(false);
	useModuleTerminalCallbacks({
		status: state.status === "finished" ? "finished" : "loading",
		onFinish
	});
	y(() => {
		if (state.status === "error") onError?.(state.error);
	}, [state, onError]);
	if (state.status === "idle" || state.status === "loading" || state.status === "submitting") return /* @__PURE__ */ u(Page, {
		className: "IncodeCpfOcrPage",
		children: /* @__PURE__ */ u("div", {
			class: "IncodeCpfOcrLoading",
			children: /* @__PURE__ */ u(Spinner, { fullScreen: false })
		})
	});
	if (state.status === "error") return /* @__PURE__ */ u(VerificationResult, {
		status: "failure",
		failureTitle: "Não foi possível verificar o CPF",
		onRetry: () => manager.retry()
	});
	if (state.status === "finished") return null;
	const inputState = getInputState(state);
	const isComplete = inputState.cpf.replace(/\D/g, "").length === 11;
	const cpfError = hasEdited && !inputState.isValid && (isComplete || hasBlurred && !isFocused) ? "Insira um CPF válido" : void 0;
	return /* @__PURE__ */ u(Page, {
		className: "IncodeCpfOcrPage",
		title: "Insira e verifique o número do seu CPF",
		children: /* @__PURE__ */ u("div", {
			class: "IncodeCpfOcr",
			children: [
				/* @__PURE__ */ u(Spacer, { size: 32 }),
				/* @__PURE__ */ u(InputComposed, {
					id: "cpf-ocr-input",
					label: "",
					value: inputState.cpf,
					placeholder: "Seu CPF",
					maxLength: 14,
					inputMode: "numeric",
					"aria-label": "CPF",
					onInput: (event) => {
						setHasEdited(true);
						manager.setCpf(event.target.value);
					},
					onFocus: () => {
						setIsFocused(true);
					},
					onBlur: () => {
						setIsFocused(false);
						setHasBlurred(true);
					},
					error: cpfError,
					showErrorIcon: true
				}),
				/* @__PURE__ */ u(Spacer, {}),
				/* @__PURE__ */ u(Button, {
					type: "button",
					onClick: () => manager.submit(),
					disabled: !inputState.isValid,
					"data-testid": "cpf-ocr-continue",
					children: "Continuar"
				})
			]
		})
	});
};
//#endregion
export { CpfOcr as t };
