import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { RegisterStep, determineVerificationMethod, getFlowConfig, getStepDefinition, isValidTransition } from "../shared/registerFlow.js";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
//#region src/pro/Login2.0/hooks/useRegisterFlow.ts
function useRegisterFlow(options) {
	const { accountType, emailConfig, phoneConfig, initialAccount = "", onStepChange, onComplete } = options;
	const onStepChangeRef = useRef(onStepChange);
	const onCompleteRef = useRef(onComplete);
	useEffect(() => {
		onStepChangeRef.current = onStepChange;
		onCompleteRef.current = onComplete;
	}, [onStepChange, onComplete]);
	const verificationMethod = useMemo(() => determineVerificationMethod(accountType, emailConfig, phoneConfig), [
		accountType,
		emailConfig,
		phoneConfig
	]);
	const flowConfig = useMemo(() => getFlowConfig(verificationMethod), [verificationMethod]);
	const autoTransitionTimer = useRef();
	const [flowData, setFlowData] = useState({
		currentStep: RegisterStep.INITIAL_FORM,
		account: initialAccount,
		accountType,
		verificationMethod
	});
	const currentStepDef = getStepDefinition(flowData.currentStep);
	const clearAutoTransition = useCallback(() => {
		if (autoTransitionTimer.current) {
			clearTimeout(autoTransitionTimer.current);
			autoTransitionTimer.current = void 0;
		}
	}, []);
	const goToStep = useCallback((step, updates) => {
		setFlowData((prevFlowData) => {
			var _updates$verification, _onStepChangeRef$curr, _flowConfig$autoTrans;
			const targetVerificationMethod = (_updates$verification = updates === null || updates === void 0 ? void 0 : updates.verificationMethod) !== null && _updates$verification !== void 0 ? _updates$verification : prevFlowData.verificationMethod;
			if (!isValidTransition(targetVerificationMethod, prevFlowData.currentStep, step)) {
				console.warn(`Invalid transition from ${prevFlowData.currentStep} to ${step} for ${targetVerificationMethod}`, "(current verificationMethod:", prevFlowData.verificationMethod, ", target:", targetVerificationMethod, ")");
				return prevFlowData;
			}
			clearAutoTransition();
			const newFlowData = _objectSpread2(_objectSpread2(_objectSpread2({}, prevFlowData), updates), {}, { currentStep: step });
			(_onStepChangeRef$curr = onStepChangeRef.current) === null || _onStepChangeRef$curr === void 0 || _onStepChangeRef$curr.call(onStepChangeRef, step, newFlowData);
			if (step === RegisterStep.COMPLETE) {
				var _onCompleteRef$curren;
				(_onCompleteRef$curren = onCompleteRef.current) === null || _onCompleteRef$curren === void 0 || _onCompleteRef$curren.call(onCompleteRef, newFlowData);
			}
			const autoTransition = (_flowConfig$autoTrans = flowConfig.autoTransition) === null || _flowConfig$autoTrans === void 0 ? void 0 : _flowConfig$autoTrans[step];
			if (autoTransition) autoTransitionTimer.current = setTimeout(() => {
				goToStep(autoTransition.nextStep);
			}, autoTransition.delay);
			return newFlowData;
		});
	}, [
		verificationMethod,
		flowConfig,
		clearAutoTransition
	]);
	const updateFlowData = useCallback((updates) => {
		setFlowData((prev) => _objectSpread2(_objectSpread2({}, prev), updates));
	}, []);
	const resetFlow = useCallback(() => {
		clearAutoTransition();
		setFlowData({
			currentStep: RegisterStep.INITIAL_FORM,
			account: initialAccount,
			accountType,
			verificationMethod
		});
	}, [
		clearAutoTransition,
		initialAccount,
		accountType,
		verificationMethod
	]);
	const handleError = useCallback((code, message, targetStep) => {
		const errorData = _objectSpread2(_objectSpread2({}, flowData), {}, { error: {
			code,
			message
		} });
		if (targetStep) errorData.currentStep = targetStep;
		setFlowData(errorData);
		onStepChange === null || onStepChange === void 0 || onStepChange(errorData.currentStep, errorData);
	}, [flowData, onStepChange]);
	useEffect(() => {
		return () => {
			clearAutoTransition();
		};
	}, [clearAutoTransition]);
	return {
		flowData,
		currentStepDef,
		flowConfig,
		goToStep,
		updateFlowData,
		resetFlow,
		handleError
	};
}
//#endregion
export { useRegisterFlow };
