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

const DefaultDynamicConfig = Promise.resolve({
    pkceRequiredForPasswordResets: false
});
class HeadlessPasswordClient {
    _networkClient;
    _subscriptionService;
    _pkceManager;
    _config;
    dfpProtectedAuth;
    create;
    authenticate;
    resetByEmail;
    resetByExistingPassword;
    resetBySession;
    constructor(_networkClient, _subscriptionService, _pkceManager, _config = DefaultDynamicConfig, dfpProtectedAuth){
        this._networkClient = _networkClient;
        this._subscriptionService = _subscriptionService;
        this._pkceManager = _pkceManager;
        this._config = _config;
        this.dfpProtectedAuth = dfpProtectedAuth;
        this.create = this._subscriptionService.withUpdateSession(async (options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const resp = await this._networkClient.retriableFetchSDK({
                url: '/passwords',
                method: 'POST',
                body: {
                    email: options.email,
                    password: options.password,
                    session_duration_minutes: options.session_duration_minutes,
                    captcha_token,
                    dfp_telemetry_id
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
            return omitUser(resp);
        });
        this.authenticate = this._subscriptionService.withUpdateSession(async (options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const resp = await this._networkClient.retriableFetchSDK({
                url: '/passwords/authenticate',
                method: 'POST',
                body: {
                    email: options.email,
                    password: options.password,
                    session_duration_minutes: options.session_duration_minutes,
                    captcha_token,
                    dfp_telemetry_id
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
            return omitUser(resp);
        });
        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: '/passwords/email/reset',
                method: 'POST',
                body: {
                    token: options.token,
                    password: options.password,
                    session_duration_minutes: options.session_duration_minutes,
                    captcha_token,
                    code_verifier,
                    dfp_telemetry_id
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
            this._pkceManager.clearPKPair();
            return omitUser(resp);
        });
        this.resetByExistingPassword = this._subscriptionService.withUpdateSession(async (options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            const resp = await this._networkClient.retriableFetchSDK({
                url: '/passwords/existing_password/reset',
                method: 'POST',
                body: {
                    email: options.email,
                    existing_password: options.existing_password,
                    new_password: options.new_password,
                    session_duration_minutes: options.session_duration_minutes,
                    dfp_telemetry_id,
                    captcha_token
                },
                retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
            });
            return omitUser(resp);
        });
        this.resetBySession = this._subscriptionService.withUpdateSession(async (options)=>{
            const { dfp_telemetry_id, captcha_token } = await this.dfpProtectedAuth.getDFPTelemetryIDAndCaptcha();
            return this._networkClient.retriableFetchSDK({
                url: '/passwords/session/reset',
                method: 'POST',
                body: {
                    password: options.password,
                    session_duration_minutes: options.session_duration_minutes,
                    dfp_telemetry_id,
                    captcha_token
                },
                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: '/passwords/email/reset/start',
            method: 'POST',
            body: {
                email: options.email,
                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,
                locale: options.locale,
                code_challenge,
                captcha_token,
                dfp_telemetry_id
            },
            retryCallback: this.dfpProtectedAuth.retryWithCaptchaAndDFP
        });
    }
    async strengthCheck(options) {
        return this._networkClient.fetchSDK({
            url: '/passwords/strength_check',
            method: 'POST',
            body: {
                email: options.email,
                password: options.password
            }
        });
    }
}

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