/**
 * Auth flow state machine types: state, actions, and context.
 */
import type { AuthStep, SSOSession, UserType } from './auth-types';
/** Identifier type for OTP / profile steps. */
export type IdentifierType = 'phone' | 'email';
export type LinkOrMergeConflictField = IdentifierType;
export type LinkOrMergeStage = 'summary' | 'otp';
export interface LinkOrMergeOtpTarget {
    type: IdentifierType;
    value: string;
    countryCode?: string;
    phone?: string;
    maskedValue?: string;
}
export type LinkOrMergeState = {
    stage: LinkOrMergeStage;
    conflictField: 'email';
    name: string;
    existingEmail?: string;
    existingCountryCode?: string;
    existingPhone?: string;
    oldEmail?: string;
    newEmail: string;
    otpTarget?: LinkOrMergeOtpTarget;
} | {
    stage: LinkOrMergeStage;
    conflictField: 'phone';
    name: string;
    existingEmail?: string;
    existingCountryCode?: string;
    existingPhone?: string;
    oldCountryCode?: string;
    oldPhone?: string;
    newCountryCode: string;
    newPhone: string;
    otpTarget?: LinkOrMergeOtpTarget;
};
export type LinkOrMergeSetupPayload = (Omit<Extract<LinkOrMergeState, {
    conflictField: 'email';
}>, 'stage'> & {
    stage?: LinkOrMergeStage;
}) | (Omit<Extract<LinkOrMergeState, {
    conflictField: 'phone';
}>, 'stage'> & {
    stage?: LinkOrMergeStage;
});
/** Full state for the auth flow. */
export interface AuthFlowState {
    step: AuthStep;
    /** Previous step for goBack. */
    lastStep: AuthStep;
    /** Indian (+91) vs Foreign (non +91); drives layout and OTP routing. */
    userType: UserType;
    /** Current identifier value (phone or email) when in OTP or signup. */
    identifierValue: string;
    /** Type of identifier (phone vs email). */
    identifierType: IdentifierType;
    /** Masked value for display (e.g. "+91 9825910X0X"). */
    maskedRecipient: string;
    /** For SIGNUP_DETAILS (Indian): which field to collect (phone or email). */
    signupCollectField: IdentifierType;
    /** For REPEAT_LOGIN: last used method label. */
    lastMethod: string;
    /** Set when step is AUTHENTICATED. */
    session: SSOSession | null;
    /** Foreign flow: phone value when user entered phone before signup details. */
    pendingPhone: string;
    /** Foreign flow: signup form data for completeProfile after OTP verify. */
    pendingFullName: string;
    pendingEmail: string;
    /** Link/merge details collected after duplicate email/mobile profile errors. */
    linkOrMerge: LinkOrMergeState | null;
}
/** Pure reducer actions. */
export type AuthFlowAction = {
    type: 'SET_SESSION';
    session: SSOSession;
} | {
    type: 'SET_STEP_REPEAT_LOGIN';
    lastMethod: string;
    maskedIdentifier: string;
} | {
    type: 'SET_STEP_METHOD_SELECT';
} | {
    type: 'SET_STEP_PHONE_ENTRY';
} | {
    type: 'SET_STEP_EMAIL_ENTRY';
} | {
    type: 'SET_STEP_OTP_VERIFICATION';
    userType: UserType;
    identifierType: IdentifierType;
    value: string;
    maskedValue: string;
    /** Foreign flow: for completeProfile after OTP. */
    pendingFullName?: string;
    pendingEmail?: string;
    pendingPhone?: string;
} | {
    type: 'SET_STEP_SIGNUP_DETAILS';
    userType: UserType;
    /** Indian: which field to collect (phone or email). Foreign: ignored. */
    signupCollectField: IdentifierType;
    /** Foreign only: phone value when user entered phone before signup. */
    pendingPhone?: string;
    /** When coming from identifier entry (foreign): preserve for later OTP. */
    identifierType?: IdentifierType;
    identifierValue?: string;
    maskedRecipient?: string;
} | ({
    type: 'SET_STEP_LINK_OR_MERGE';
} & LinkOrMergeSetupPayload) | {
    type: 'SET_LINK_OR_MERGE_OTP_STAGE';
    otpTarget: LinkOrMergeOtpTarget;
} | {
    type: 'SET_STEP_PROVIDER_PENDING';
} | {
    type: 'SET_STEP_AUTHENTICATED';
    session: SSOSession;
} | {
    type: 'GO_BACK';
} | {
    type: 'RESET';
} | {
    type: 'SIGN_OUT';
}
/**
 * Replace the entire state with a previously-persisted snapshot. Used
 * by the provider to rehydrate the flow on page refresh so the user
 * doesn't lose their step. See `flow-state-storage.ts` and the
 * CHECKING_SESSION fallback in `auth-provider.tsx`.
 */
 | {
    type: 'RESTORE_STATE';
    state: AuthFlowState;
};
/** Provider config slice exposed to flow for Google/Apple wiring. */
export interface AuthFlowProviderConfig {
    google?: {
        clientId: string;
        enableOneTap?: boolean;
    };
    apple?: {
        clientId: string;
        redirectUri: string;
    };
}
/** Async actions that perform I/O and dispatch to the flow reducer. */
export interface AuthFlowActions {
    selectMethod: (method: 'phone' | 'email' | 'google' | 'apple') => void;
    /** From REPEAT_LOGIN: continue with last used method. */
    continueWithLastMethod: () => void;
    submitIdentifier: (data: {
        phone?: string;
        email?: string;
        countryCode?: string;
    }) => void;
    /** SIGNUP_DETAILS step: submit name + phone/email, then OTP (foreign) or complete (Indian). */
    submitSignupDetails: (data: {
        fullName: string;
        phone?: string;
        email?: string;
        countryCode?: string;
    }) => void;
    /**
     * Verifies the OTP against the auth API. Resolves on success (after the
     * reducer has been advanced) and rejects with an `AuthError` whose
     * `message` carries the API's failure reason — callers can surface it
     * as a field-level error.
     */
    verifyOtp: (otp: string) => Promise<void>;
    resendOtp: () => void;
    providerCallback: (params: {
        provider: 'google' | 'apple';
        credential: string;
    }) => void;
    continueLinkOrMerge: () => Promise<void>;
    verifyLinkOrMerge: (otp: string) => Promise<void>;
    goBack: () => void;
    reset: () => void;
    signOut: () => void;
}
/** Auth flow context value provided by SSOAuthProvider. */
export interface AuthFlowContextValue {
    state: AuthFlowState;
    actions: AuthFlowActions;
    /** Session when step is AUTHENTICATED. */
    session: SSOSession | null;
    /** True while step is CHECKING_SESSION. */
    isLoading: boolean;
    /** Provider config for Google/Apple (One Tap, Sign-In button). */
    providerConfig: AuthFlowProviderConfig | undefined;
}
