require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_registerFlow = require("../shared/registerFlow.js");
let react = require("react");
//#region src/pro/Login2.0/hooks/useRegisterFlow.ts
function useRegisterFlow(options) {
	const { accountType, emailConfig, phoneConfig, initialAccount = "", onStepChange, onComplete } = options;
	const onStepChangeRef = (0, react.useRef)(onStepChange);
	const onCompleteRef = (0, react.useRef)(onComplete);
	(0, react.useEffect)(() => {
		onStepChangeRef.current = onStepChange;
		onCompleteRef.current = onComplete;
	}, [onStepChange, onComplete]);
	const verificationMethod = (0, react.useMemo)(() => require_registerFlow.determineVerificationMethod(accountType, emailConfig, phoneConfig), [
		accountType,
		emailConfig,
		phoneConfig
	]);
	const flowConfig = (0, react.useMemo)(() => require_registerFlow.getFlowConfig(verificationMethod), [verificationMethod]);
	const autoTransitionTimer = (0, react.useRef)();
	const [flowData, setFlowData] = (0, react.useState)({
		currentStep: require_registerFlow.RegisterStep.INITIAL_FORM,
		account: initialAccount,
		accountType,
		verificationMethod
	});
	const currentStepDef = require_registerFlow.getStepDefinition(flowData.currentStep);
	const clearAutoTransition = (0, react.useCallback)(() => {
		if (autoTransitionTimer.current) {
			clearTimeout(autoTransitionTimer.current);
			autoTransitionTimer.current = void 0;
		}
	}, []);
	const goToStep = (0, react.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 (!require_registerFlow.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 = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prevFlowData), updates), {}, { currentStep: step });
			(_onStepChangeRef$curr = onStepChangeRef.current) === null || _onStepChangeRef$curr === void 0 || _onStepChangeRef$curr.call(onStepChangeRef, step, newFlowData);
			if (step === require_registerFlow.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 = (0, react.useCallback)((updates) => {
		setFlowData((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), updates));
	}, []);
	const resetFlow = (0, react.useCallback)(() => {
		clearAutoTransition();
		setFlowData({
			currentStep: require_registerFlow.RegisterStep.INITIAL_FORM,
			account: initialAccount,
			accountType,
			verificationMethod
		});
	}, [
		clearAutoTransition,
		initialAccount,
		accountType,
		verificationMethod
	]);
	const handleError = (0, react.useCallback)((code, message, targetStep) => {
		const errorData = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, flowData), {}, { error: {
			code,
			message
		} });
		if (targetStep) errorData.currentStep = targetStep;
		setFlowData(errorData);
		onStepChange === null || onStepChange === void 0 || onStepChange(errorData.currentStep, errorData);
	}, [flowData, onStepChange]);
	(0, react.useEffect)(() => {
		return () => {
			clearAutoTransition();
		};
	}, [clearAutoTransition]);
	return {
		flowData,
		currentStepDef,
		flowConfig,
		goToStep,
		updateFlowData,
		resetFlow,
		handleError
	};
}
//#endregion
exports.useRegisterFlow = useRegisterFlow;
