import type {
	AuthorizeOptions,
	AuthOptions as Auth0Options,
	Auth0Result,
} from 'auth0-js';
import { Auth0WebAuth } from '../utils/Auth0Auth';
import { LoginWithPasswordProps } from 'lib/providers/WidgetProvider';

declare global {
	namespace WidgetProvider {
		interface Action extends Partial<WidgetProvider.Context> {
			type: ActionType;
			error?: any;
		}

		type ActionType =
			| 'CAPTCHA_ERROR'
			| 'CAPTCHA_RENDERED'
			| 'CLIENT_INITIALIZED'
			| 'INITIATED_CHECK_SESSION'
			| 'INITIATED_LOGIN_WITH_PASSWORD'
			| 'INITIATED_OTP'
			| 'INITIATED_OTP_VERIFY'
			| 'INITIATED_AUTH0_MANAGE'
			| 'INITIATED_SOCIAL_AUTH'
			| 'OTP_SENT'
			| 'OTP_VERIFIED'
			| 'MANAGEMENT_SESSION_CHECKED'
			| 'SWITCH_FLOW'
			| 'UPDATE_OPTIONS'
			| 'USER_FETCHED'
			| 'USER_UPDATED'
			| 'ERROR';

		type AuthFlow =
			| 'email'
			| 'email-link'
			| 'email-verify'
			| 'mfa'
			| 'password'
			| 'signup'
			| 'sms'
			| 'sms-verify'
			| 'progressive';

		type Connections =
			| 'password'
			| 'email'
			| 'sms'
			| 'apple'
			| 'facebook'
			| 'google'
			| 'google-oauth2'
			| string;

		interface AuthOptions extends Auth0Options {
			apiDomain: string;
			connections: WidgetProvider.AuthOptionsConnections;
		}

		interface AuthOptionsConnections {
			[key: WidgetProvider.Connections]: boolean | string;
		}

		interface Error {
			error_description?: string;
			name?: string;
		}

		interface Props extends Omit<WidgetProvider.Context, 'authOptions'> {
			authOptions: WidgetProvider.AuthOptions;
			children?: React.ReactNode;
		}

		type ContextInitializer = (
			context: WidgetProvider.InitialContext
		) => WidgetProvider.Context;

		interface Context {
			auth0?: Auth0WebAuth;
			auth0Manage?: Management;
			authOptions: WidgetProvider.AuthOptions;
			captcha?: Captcha;
			flow: WidgetProvider.AuthFlow;
			initialized: boolean;
			isAuthenticated: boolean;
			isInvalidCaptcha: boolean;
			isLoading: boolean;
			isLoadingAuth: boolean;
			loginHint?: string;
			magicRes?: Auth0Result;
			managementToken?: string;
			themeContent: Theme.Contents;
			user?: User.Profile;
			goTo: (flow: WidgetProvider.AuthFlow) => void;
			loginWithPassword: (
				options: WidgetProvider.LoginWithPasswordOptions
			) => Promise<void>;
			loginWithRedirect: (options?: AuthorizeOptions) => void;
			renderCaptcha: (
				container: HTMLDivElement,
				isReRender?: boolean
			) => Captcha;
			sendMagicLink: (
				options?: WidgetProvider.SendMagicLinkOptions
			) => Promise<void>;
			socialLogin: (connection: Connections) => void;
			updateUser: (options: User.Profile) => void;
			verifyMagicLink: (options: VerifyMagicLinkProps) => void;
		}

		interface InitialContext
			extends Omit<WidgetProvider.Context, 'auth0' | 'authOptions'> {
			authOptions: Partial<WidgetProvider.AuthOptions>;
		}

		interface LoginWithPasswordOptions {
			username?: string;
			email?: string;
			password: string;
			connection?: string;
		}

		interface SendMagicLinkOptions {
			email?: string;
			resend?: boolean;
			phoneNumber?: string;
		}

		interface VerifyMagicLinkProps {
			code: string;
			email?: string;
			phone?: string;
			redirectUri?: string;
		}
	}
}

export {};
