/*! @azure/msal-browser v5.6.0 2026-03-18 */
'use strict';
import { Logger, StubPerformanceClient, Constants } from '@azure/msal-common/browser';
import { name, version } from '../../packageMetadata.mjs';
import { DEFAULT_NATIVE_BROKER_HANDSHAKE_TIMEOUT_MS } from '../../config/Configuration.mjs';
import { PlatformAuthExtensionHandler } from './PlatformAuthExtensionHandler.mjs';
import { PlatformAuthDOMHandler } from './PlatformAuthDOMHandler.mjs';
import { BrowserCacheLocation } from '../../utils/BrowserConstants.mjs';
import { PLATFORM_AUTH_DOM_SUPPORT } from '../../cache/CacheKeys.mjs';

/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
/**
 * Checks if the platform broker is available in the current environment.
 * @param loggerOptions
 * @param perfClient
 * @param correlationId
 * @returns
 */
async function isPlatformBrokerAvailable(loggerOptions, perfClient, correlationId) {
    const logger = new Logger(loggerOptions || {}, name, version);
    const cid = correlationId || "";
    logger.trace("07660b", cid);
    const performanceClient = perfClient || new StubPerformanceClient();
    if (typeof window === "undefined") {
        logger.trace("082ed3", cid);
        return false;
    }
    return !!(await getPlatformAuthProvider(logger, performanceClient, cid));
}
async function getPlatformAuthProvider(logger, performanceClient, correlationId, nativeBrokerHandshakeTimeout) {
    logger.trace("134j0v", correlationId);
    const enablePlatformBrokerDOMSupport = isDomEnabledForPlatformAuth();
    logger.trace("04c81g", correlationId);
    let platformAuthProvider;
    try {
        if (enablePlatformBrokerDOMSupport) {
            // Check if DOM platform API is supported first
            platformAuthProvider = await PlatformAuthDOMHandler.createProvider(logger, performanceClient, correlationId);
        }
        if (!platformAuthProvider) {
            logger.trace("0l3na8", correlationId);
            /*
             * If DOM APIs are not available, check if browser extension is available.
             * Platform authentication via DOM APIs is preferred over extension APIs.
             */
            platformAuthProvider =
                await PlatformAuthExtensionHandler.createProvider(logger, nativeBrokerHandshakeTimeout ||
                    DEFAULT_NATIVE_BROKER_HANDSHAKE_TIMEOUT_MS, performanceClient, correlationId);
        }
    }
    catch (e) {
        logger.trace("0icbd7", e);
    }
    return platformAuthProvider;
}
/**
 * Returns true if the DOM API support for platform auth is enabled in session storage
 * @returns boolean
 * @deprecated
 */
function isDomEnabledForPlatformAuth() {
    let sessionStorage;
    try {
        sessionStorage = window[BrowserCacheLocation.SessionStorage];
        // Mute errors if it's a non-browser environment or cookies are blocked.
        return sessionStorage?.getItem(PLATFORM_AUTH_DOM_SUPPORT) === "true";
    }
    catch (e) {
        return false;
    }
}
/**
 * Returns boolean indicating whether or not the request should attempt to use native broker
 * @param logger
 * @param config
 * @param correlationId
 * @param platformAuthProvider
 * @param authenticationScheme
 */
function isPlatformAuthAllowed(config, logger, correlationId, platformAuthProvider, authenticationScheme) {
    logger.trace("0uko3r", correlationId);
    if (!config.system.allowPlatformBroker) {
        logger.trace("04hozs", correlationId);
        // Developer disabled WAM
        return false;
    }
    if (!platformAuthProvider) {
        logger.trace("0kvv1r", correlationId);
        // Platform broker auth providers are not available
        return false;
    }
    if (authenticationScheme) {
        switch (authenticationScheme) {
            case Constants.AuthenticationScheme.BEARER:
            case Constants.AuthenticationScheme.POP:
                logger.trace("18tev1", correlationId);
                return true;
            default:
                logger.trace("1dd2nh", correlationId);
                return false;
        }
    }
    return true;
}

export { getPlatformAuthProvider, isDomEnabledForPlatformAuth, isPlatformAuthAllowed, isPlatformBrokerAvailable };
//# sourceMappingURL=PlatformAuthProvider.mjs.map
