import { RetriableErrorType } from './NetworkClient.mjs';
import { loadESModule } from './utils/loadESModule.mjs';

const loadTelemetryJS = (domain)=>loadESModule(`${domain}/telemetry.js`, ()=>window.GetTelemetryID);
class DFPProtectedAuthProvider {
    bootstrapPromise;
    state;
    constructor(publicToken, dfpBackendURL, dfpCdnDomain, bootstrapPromise, executeRecaptcha = ()=>Promise.resolve(undefined)){
        this.bootstrapPromise = bootstrapPromise;
        this.state = bootstrapPromise.then(async (bootstrapData)=>{
            if (!bootstrapData.runDFPProtectedAuth) {
                return {
                    publicToken,
                    dfpBackendURL,
                    enabled: false,
                    loaded: false,
                    executeRecaptcha
                };
            }
            await loadTelemetryJS(dfpCdnDomain);
            return {
                publicToken,
                dfpBackendURL,
                enabled: true,
                mode: bootstrapData.dfpProtectedAuthMode || 'OBSERVATION',
                loaded: true,
                executeRecaptcha
            };
        });
    }
    isEnabled = async ()=>{
        return this.state.then((state)=>state.enabled);
    };
    getTelemetryID = async ()=>{
        const { publicToken, enabled, dfpBackendURL } = await this.state;
        if (!enabled) {
            return undefined;
        }
        return await window.GetTelemetryID(publicToken, `${dfpBackendURL}/submit`);
    };
    getDFPTelemetryIDAndCaptcha = async ()=>{
        const { enabled, executeRecaptcha, mode } = await this.state;
        let dfp_telemetry_id = undefined;
        let captcha_token = undefined;
        if (!enabled) {
            captcha_token = await executeRecaptcha();
        }
        if (mode === 'DECISIONING') {
            dfp_telemetry_id = await this.getTelemetryID();
        } else if (mode === 'OBSERVATION') {
            dfp_telemetry_id = await this.getTelemetryID();
            captcha_token = await executeRecaptcha();
        }
        return {
            dfp_telemetry_id,
            captcha_token
        };
    };
    retryWithCaptchaAndDFP = async (e, req)=>{
        const { enabled, executeRecaptcha } = await this.state;
        if (e.type === RetriableErrorType.RequiredCaptcha && enabled) {
            if (req.body) {
                req.body.dfp_telemetry_id = await this.getTelemetryID();
                req.body.captcha_token = await executeRecaptcha();
            }
            return req;
        }
        throw new Error('Unable to query captcha and/or dfp telemetry ID');
    };
}
const DisabledDFPProtectedAuthProvider = ()=>({
        isEnabled: async ()=>false,
        getTelemetryID: async ()=>undefined,
        getDFPTelemetryIDAndCaptcha: async ()=>({
                dfp_telemetry_id: undefined,
                captcha_token: undefined
            }),
        retryWithCaptchaAndDFP: async ()=>{
            throw new Error('DFP protected auth is disabled');
        }
    });

export { DFPProtectedAuthProvider, DisabledDFPProtectedAuthProvider };
//# sourceMappingURL=DFPProtectedAuthProvider.mjs.map
