import { omitUser } from '../utils/index.mjs';

class HeadlessOTPClient {
    _networkClient;
    _subscriptionService;
    executeRecaptcha;
    dfpProtectedAuth;
    authenticate;
    constructor(_networkClient, _subscriptionService, executeRecaptcha = ()=>Promise.resolve(undefined), dfpProtectedAuth){
        this._networkClient = _networkClient;
        this._subscriptionService = _subscriptionService;
        this.executeRecaptcha = executeRecaptcha;
        this.dfpProtectedAuth = dfpProtectedAuth;
        this.authenticate = this._subscriptionService.withUpdateSession(async (code, method_id, options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const requestBody = {
                token: code,
                method_id,
                dfp_telemetry_id,
                captcha_token,
                ...options
            };
            const resp = await this._networkClient.retriableFetchSDK({
                url: '/otps/authenticate',
                body: requestBody,
                method: 'POST',
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
            return omitUser(resp);
        });
    }
    sms = {
        loginOrCreate: async (phone_number, options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const requestBody = {
                ...options,
                phone_number,
                captcha_token,
                dfp_telemetry_id
            };
            return this._networkClient.retriableFetchSDK({
                url: '/otps/sms/login_or_create',
                body: requestBody,
                method: 'POST',
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
        },
        send: async (phone_number, options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const requestBody = {
                ...options,
                phone_number,
                captcha_token,
                dfp_telemetry_id
            };
            const isLoggedIn = !!this._subscriptionService.getSession();
            const endpoint = isLoggedIn ? '/otps/sms/send/secondary' : '/otps/sms/send/primary';
            return this._networkClient.retriableFetchSDK({
                url: endpoint,
                body: requestBody,
                method: 'POST',
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
        }
    };
    whatsapp = {
        loginOrCreate: async (phone_number, options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const requestBody = {
                ...options,
                phone_number,
                dfp_telemetry_id,
                captcha_token
            };
            return this._networkClient.retriableFetchSDK({
                url: '/otps/whatsapp/login_or_create',
                body: requestBody,
                method: 'POST',
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
        },
        send: async (phone_number, options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const requestBody = {
                ...options,
                phone_number,
                captcha_token,
                dfp_telemetry_id
            };
            const isLoggedIn = !!this._subscriptionService.getSession();
            const endpoint = isLoggedIn ? '/otps/whatsapp/send/secondary' : '/otps/whatsapp/send/primary';
            return this._networkClient.retriableFetchSDK({
                url: endpoint,
                body: requestBody,
                method: 'POST',
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
        }
    };
    email = {
        loginOrCreate: async (email, options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const requestBody = {
                ...options,
                email,
                captcha_token,
                dfp_telemetry_id
            };
            return this._networkClient.retriableFetchSDK({
                url: '/otps/email/login_or_create',
                body: requestBody,
                method: 'POST',
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
        },
        send: async (email, options)=>{
            const captcha_token = await this.executeRecaptcha();
            const requestBody = {
                ...options,
                email,
                captcha_token
            };
            const isLoggedIn = !!this._subscriptionService.getSession();
            const endpoint = isLoggedIn ? '/otps/email/send/secondary' : '/otps/email/send/primary';
            return this._networkClient.fetchSDK({
                url: endpoint,
                body: requestBody,
                method: 'POST'
            });
        }
    };
}

export { HeadlessOTPClient };
//# sourceMappingURL=HeadlessOTPClient.mjs.map
