import { WebAuth } from 'auth0-js';
import { WidgetError } from './widgetError';

import type {
	Auth0Result,
	CheckSessionOptions,
	PasswordlessStartOptions,
	PasswordlessVerifyOptions,
} from 'auth0-js';

export class Auth0WebAuth extends WebAuth {
	constructor(authOptions: WidgetProvider.AuthOptions) {
		super(authOptions);
	}

	getTokens: (options: CheckSessionOptions) => Promise<Auth0Result> = (
		options
	) =>
		new Promise((resolve, reject) =>
			this.checkSession(options, (err, result) =>
				err ? reject(new WidgetError(err)) : resolve(result)
			)
		);
	sendMagicLink = (options: PasswordlessStartOptions) =>
		new Promise<void>((resolve, reject) =>
			this.passwordlessStart(options, (err, result) => {
				console.log({ err, result });
				if (err) {
					return reject(new WidgetError(err));
				}

				return resolve(result);
			})
		);
	verifyMagicLink: (
		options: PasswordlessVerifyOptions
	) => Promise<Auth0Result> = (options) =>
		new Promise((resolve, reject) =>
			this.passwordlessVerify(options, (err, result) =>
				err ? reject(new WidgetError(err)) : resolve(result)
			)
		);
}
