const DefaultDynamicConfig$3 = Promise.resolve({
    pkceRequiredForPasswordResets: false
});
class HeadlessB2BPasswordsClient {
    _networkClient;
    _subscriptionService;
    _pkceManager;
    _config;
    dfpProtectedAuth;
    authenticate;
    discovery;
    resetByEmail;
    resetByExistingPassword;
    resetBySession;
    constructor(_networkClient, _subscriptionService, _pkceManager, _config = DefaultDynamicConfig$3, dfpProtectedAuth){
        this._networkClient = _networkClient;
        this._subscriptionService = _subscriptionService;
        this._pkceManager = _pkceManager;
        this._config = _config;
        this.dfpProtectedAuth = dfpProtectedAuth;
        this.authenticate = this._subscriptionService.withUpdateSession(async (options)=>{
            const pkPair = await this._pkceManager.getPKPair();
            const code_verifier = pkPair?.code_verifier;
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            return this._networkClient.retriableFetchSDK({
                url: '/b2b/passwords/authenticate',
                method: 'POST',
                body: {
                    organization_id: options.organization_id,
                    email_address: options.email_address,
                    password: options.password,
                    session_duration_minutes: options.session_duration_minutes,
                    locale: options.locale,
                    captcha_token,
                    dfp_telemetry_id,
                    code_verifier,
                    intermediate_session_token: this._subscriptionService.getIntermediateSessionToken() || undefined
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
        });
        this.discovery = {
            resetByEmailStart: async (options)=>{
                const pkce_code_challenge = await this.getCodeChallenge();
                const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
                return this._networkClient.retriableFetchSDK({
                    url: '/b2b/passwords/discovery/reset/start',
                    method: 'POST',
                    body: {
                        email_address: options.email_address,
                        discovery_redirect_url: options.discovery_redirect_url,
                        reset_password_redirect_url: options.reset_password_redirect_url,
                        reset_password_expiration_minutes: options.reset_password_expiration_minutes,
                        reset_password_template_id: options.reset_password_template_id,
                        verify_email_template_id: options.verify_email_template_id,
                        locale: options.locale,
                        pkce_code_challenge,
                        captcha_token,
                        dfp_telemetry_id
                    },
                    retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
                });
            },
            resetByEmail: this._subscriptionService.withUpdateSession(async (options)=>{
                const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
                const pkPair = await this._pkceManager.getPKPair();
                const pkce_code_verifier = pkPair?.code_verifier;
                const resp = await this._networkClient.retriableFetchSDK({
                    url: '/b2b/passwords/discovery/reset',
                    method: 'POST',
                    body: {
                        password_reset_token: options.password_reset_token,
                        password: options.password,
                        captcha_token,
                        dfp_telemetry_id,
                        pkce_code_verifier,
                        intermediate_session_token: await this._subscriptionService.getIntermediateSessionToken() || undefined
                    },
                    retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
                });
                this._pkceManager.clearPKPair();
                return resp;
            }),
            authenticate: this._subscriptionService.withUpdateSession(async (options)=>{
                const pkPair = await this._pkceManager.getPKPair();
                const code_verifier = pkPair?.code_verifier;
                const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
                return this._networkClient.retriableFetchSDK({
                    url: '/b2b/passwords/discovery/authenticate',
                    method: 'POST',
                    body: {
                        email_address: options.email_address,
                        password: options.password,
                        captcha_token,
                        dfp_telemetry_id,
                        code_verifier
                    },
                    retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
                });
            })
        };
        this.resetByEmail = this._subscriptionService.withUpdateSession(async (options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const pkPair = await this._pkceManager.getPKPair();
            const code_verifier = pkPair?.code_verifier;
            const resp = await this._networkClient.retriableFetchSDK({
                url: '/b2b/passwords/email/reset',
                method: 'POST',
                body: {
                    password_reset_token: options.password_reset_token,
                    password: options.password,
                    session_duration_minutes: options.session_duration_minutes,
                    locale: options.locale,
                    captcha_token,
                    dfp_telemetry_id,
                    code_verifier: code_verifier,
                    intermediate_session_token: this._subscriptionService.getIntermediateSessionToken() || undefined
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
            this._pkceManager.clearPKPair();
            return resp;
        });
        this.resetByExistingPassword = this._subscriptionService.withUpdateSession(async (options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            return this._networkClient.retriableFetchSDK({
                url: '/b2b/passwords/existing_password/reset',
                method: 'POST',
                body: {
                    organization_id: options.organization_id,
                    email_address: options.email_address,
                    existing_password: options.existing_password,
                    new_password: options.new_password,
                    locale: options.locale,
                    session_duration_minutes: options.session_duration_minutes,
                    captcha_token,
                    dfp_telemetry_id
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
        });
        this.resetBySession = this._subscriptionService.withUpdateSession(async (options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            return this._networkClient.retriableFetchSDK({
                url: '/b2b/passwords/session/reset',
                method: 'POST',
                body: {
                    password: options.password,
                    captcha_token,
                    dfp_telemetry_id
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
        });
    }
    async getCodeChallenge() {
        const { pkceRequiredForPasswordResets } = await this._config;
        if (!pkceRequiredForPasswordResets) {
            return undefined;
        }
        let keyPair = await this._pkceManager.getPKPair();
        if (keyPair) {
            return keyPair.code_challenge;
        }
        keyPair = await this._pkceManager.startPKCETransaction();
        return keyPair.code_challenge;
    }
    async resetByEmailStart(options) {
        const code_challenge = await this.getCodeChallenge();
        const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
        return this._networkClient.retriableFetchSDK({
            url: '/b2b/passwords/email/reset/start',
            method: 'POST',
            body: {
                organization_id: options.organization_id,
                email_address: options.email_address,
                login_redirect_url: options.login_redirect_url,
                reset_password_redirect_url: options.reset_password_redirect_url,
                reset_password_expiration_minutes: options.reset_password_expiration_minutes,
                reset_password_template_id: options.reset_password_template_id,
                verify_email_template_id: options.verify_email_template_id,
                locale: options.locale,
                code_challenge,
                captcha_token,
                dfp_telemetry_id
            },
            retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
        });
    }
    async strengthCheck(options) {
        return this._networkClient.fetchSDK({
            url: '/b2b/passwords/strength_check',
            method: 'POST',
            body: {
                email_address: options.email_address,
                password: options.password
            }
        });
    }
}

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