var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/pro/Login2.0/hooks/useRegisterFlow.ts
var useRegisterFlow_exports = {};
__export(useRegisterFlow_exports, {
  useRegisterFlow: () => useRegisterFlow
});
module.exports = __toCommonJS(useRegisterFlow_exports);
var import_react = require("react");
var import_registerFlow = require("../shared/registerFlow");
function useRegisterFlow(options) {
  const {
    accountType,
    emailConfig,
    phoneConfig,
    initialAccount = "",
    onStepChange,
    onComplete
  } = options;
  const onStepChangeRef = (0, import_react.useRef)(onStepChange);
  const onCompleteRef = (0, import_react.useRef)(onComplete);
  (0, import_react.useEffect)(() => {
    onStepChangeRef.current = onStepChange;
    onCompleteRef.current = onComplete;
  }, [onStepChange, onComplete]);
  const verificationMethod = (0, import_react.useMemo)(
    () => (0, import_registerFlow.determineVerificationMethod)(accountType, emailConfig, phoneConfig),
    [accountType, emailConfig, phoneConfig]
  );
  const flowConfig = (0, import_react.useMemo)(
    () => (0, import_registerFlow.getFlowConfig)(verificationMethod),
    [verificationMethod]
  );
  const autoTransitionTimer = (0, import_react.useRef)();
  const [flowData, setFlowData] = (0, import_react.useState)({
    currentStep: import_registerFlow.RegisterStep.INITIAL_FORM,
    account: initialAccount,
    accountType,
    verificationMethod
  });
  const currentStepDef = (0, import_registerFlow.getStepDefinition)(flowData.currentStep);
  const clearAutoTransition = (0, import_react.useCallback)(() => {
    if (autoTransitionTimer.current) {
      clearTimeout(autoTransitionTimer.current);
      autoTransitionTimer.current = void 0;
    }
  }, []);
  const goToStep = (0, import_react.useCallback)(
    (step, updates) => {
      setFlowData((prevFlowData) => {
        var _a, _b, _c;
        const targetVerificationMethod = (updates == null ? void 0 : updates.verificationMethod) ?? prevFlowData.verificationMethod;
        if (!(0, import_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 = {
          ...prevFlowData,
          ...updates,
          currentStep: step
        };
        (_a = onStepChangeRef.current) == null ? void 0 : _a.call(onStepChangeRef, step, newFlowData);
        if (step === import_registerFlow.RegisterStep.COMPLETE) {
          (_b = onCompleteRef.current) == null ? void 0 : _b.call(onCompleteRef, newFlowData);
        }
        const autoTransition = (_c = flowConfig.autoTransition) == null ? void 0 : _c[step];
        if (autoTransition) {
          autoTransitionTimer.current = setTimeout(() => {
            goToStep(autoTransition.nextStep);
          }, autoTransition.delay);
        }
        return newFlowData;
      });
    },
    [
      verificationMethod,
      flowConfig,
      clearAutoTransition
    ]
  );
  const updateFlowData = (0, import_react.useCallback)((updates) => {
    setFlowData((prev) => ({ ...prev, ...updates }));
  }, []);
  const resetFlow = (0, import_react.useCallback)(() => {
    clearAutoTransition();
    setFlowData({
      currentStep: import_registerFlow.RegisterStep.INITIAL_FORM,
      account: initialAccount,
      accountType,
      verificationMethod
    });
  }, [clearAutoTransition, initialAccount, accountType, verificationMethod]);
  const handleError = (0, import_react.useCallback)(
    (code, message, targetStep) => {
      const errorData = {
        ...flowData,
        error: { code, message }
      };
      if (targetStep) {
        errorData.currentStep = targetStep;
      }
      setFlowData(errorData);
      onStepChange == null ? void 0 : onStepChange(errorData.currentStep, errorData);
    },
    [flowData, onStepChange]
  );
  (0, import_react.useEffect)(() => {
    return () => {
      clearAutoTransition();
    };
  }, [clearAutoTransition]);
  return {
    flowData,
    currentStepDef,
    flowConfig,
    goToStep,
    updateFlowData,
    resetFlow,
    handleError
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useRegisterFlow
});
