import { logger } from '../../core/src/utils/logger.mjs';

const BOOTSTRAP_CACHE_KEY = 'bootstrap';
const DEFAULT_BOOTSTRAP = ()=>({
        displayWatermark: false,
        projectName: null,
        cnameDomain: null,
        emailDomains: [
            'stytch.com'
        ],
        captchaSettings: {
            enabled: false
        },
        pkceRequiredForEmailMagicLinks: false,
        pkceRequiredForPasswordResets: false,
        pkceRequiredForOAuth: false,
        pkceRequiredForSso: false,
        slugPattern: null,
        createOrganizationEnabled: false,
        passwordConfig: null,
        runDFPProtectedAuth: false,
        rbacPolicy: null,
        siweRequiredForCryptoWallets: false,
        vertical: null
    });
class BootstrapDataManager {
    _publicToken;
    _networkClient;
    _dataLayer;
    _bootstrapDataPromise;
    constructor(_publicToken, _networkClient, _dataLayer){
        this._publicToken = _publicToken;
        this._networkClient = _networkClient;
        this._dataLayer = _dataLayer;
        // TODO: Push the async version of this into @stytch/core
        // for RN to use, have getSync throw a NotImplementedError
        // and inherit it to create a sync one here?
        this._bootstrapDataPromise = this._networkClient.fetchSDK({
            url: `/projects/bootstrap/${this._publicToken}`,
            method: 'GET'
        }).then(BootstrapDataManager.mapBootstrapData).then((data)=>{
            this._dataLayer.setItem(BOOTSTRAP_CACHE_KEY, JSON.stringify(data));
            return data;
        }).catch((error)=>{
            logger.error(error);
            return DEFAULT_BOOTSTRAP();
        });
    }
    static mapBootstrapData(response) {
        const passwordConfig = response.password_config !== null ? {
            ludsComplexity: response.password_config.luds_complexity,
            ludsMinimumCount: response.password_config.luds_minimum_count
        } : null;
        return {
            projectName: response.project_name,
            displayWatermark: !response.disable_sdk_watermark,
            captchaSettings: response.captcha_settings,
            cnameDomain: response.cname_domain,
            emailDomains: response.email_domains,
            pkceRequiredForEmailMagicLinks: response.pkce_required_for_email_magic_links,
            pkceRequiredForPasswordResets: response.pkce_required_for_password_resets,
            pkceRequiredForOAuth: response.pkce_required_for_oauth,
            pkceRequiredForSso: response.pkce_required_for_sso,
            slugPattern: response.slug_pattern,
            createOrganizationEnabled: response.create_organization_enabled,
            passwordConfig,
            runDFPProtectedAuth: response.dfp_protected_auth_enabled ?? false,
            dfpProtectedAuthMode: response.dfp_protected_auth_mode,
            rbacPolicy: response.rbac_policy ?? null,
            siweRequiredForCryptoWallets: response.siwe_required_for_crypto_wallets,
            vertical: response.vertical
        };
    }
    getSync() {
        const cached = this._dataLayer.getItem(BOOTSTRAP_CACHE_KEY);
        if (cached === null) {
            return DEFAULT_BOOTSTRAP();
        }
        try {
            return JSON.parse(cached);
        } catch  {
            return DEFAULT_BOOTSTRAP();
        }
    }
    getAsync() {
        return this._bootstrapDataPromise;
    }
}

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