/*! @azure/msal-browser v5.15.0 2026-06-23 */
'use strict';
import { UrlString, invokeAsync, AuthorizationCodeClient, ProtocolUtils } from '@azure/msal-common/browser';
import { BaseInteractionClient, getDiscoveredAuthority, getRedirectUri } from './BaseInteractionClient.mjs';
import { StandardInteractionClientGetClientConfiguration, StandardInteractionClientGetDiscoveredAuthority, InitializeBaseRequest } from '../telemetry/BrowserPerformanceEvents.mjs';
import { BrowserConstants } from '../utils/BrowserConstants.mjs';
import { version } from '../packageMetadata.mjs';
import { getCurrentUri } from '../utils/BrowserUtils.mjs';
import { createNewGuid } from '../crypto/BrowserCrypto.mjs';
import { initializeBaseRequest, validateRequestMethod } from '../request/RequestHelpers.mjs';

/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
/**
 * Defines the class structure and helper functions used by the "standard", non-brokered auth flows (popup, redirect, silent (RT), silent (iframe))
 */
class StandardInteractionClient extends BaseInteractionClient {
    /**
     * Initializer for the logout request.
     * @param logoutRequest
     */
    initializeLogoutRequest(logoutRequest) {
        this.logger.verbose("0546u4", this.correlationId);
        const validLogoutRequest = {
            correlationId: this.correlationId,
            ...logoutRequest,
        };
        /**
         * Set logout_hint to be login_hint from ID Token Claims if present
         * and logoutHint attribute wasn't manually set in logout request
         */
        if (logoutRequest) {
            // If logoutHint isn't set and an account was passed in, try to extract logoutHint from account loginHint or ID Token Claims
            if (!logoutRequest.logoutHint) {
                if (logoutRequest.account) {
                    const logoutHint = logoutRequest.account.loginHint ||
                        this.getLogoutHintFromIdTokenClaims(logoutRequest.account);
                    if (logoutHint) {
                        this.logger.verbose("0d7s8p", this.correlationId);
                        validLogoutRequest.logoutHint = logoutHint;
                    }
                }
                else {
                    this.logger.verbose("0pdtc3", this.correlationId);
                }
            }
            else {
                this.logger.verbose("12k4l4", this.correlationId);
            }
        }
        else {
            this.logger.verbose("07ndze", this.correlationId);
        }
        /*
         * Only set redirect uri if logout request isn't provided or the set uri isn't null.
         * Otherwise, use passed uri, config, or current page.
         */
        if (!logoutRequest || logoutRequest.postLogoutRedirectUri !== null) {
            if (logoutRequest && logoutRequest.postLogoutRedirectUri) {
                this.logger.verbose("1vamm6", validLogoutRequest.correlationId);
                validLogoutRequest.postLogoutRedirectUri =
                    UrlString.getAbsoluteUrl(logoutRequest.postLogoutRedirectUri, getCurrentUri(), validLogoutRequest.correlationId);
            }
            else if (this.config.auth.postLogoutRedirectUri === null) {
                this.logger.verbose("15m5g7", validLogoutRequest.correlationId);
            }
            else if (this.config.auth.postLogoutRedirectUri) {
                this.logger.verbose("1f4xlz", validLogoutRequest.correlationId);
                validLogoutRequest.postLogoutRedirectUri =
                    UrlString.getAbsoluteUrl(this.config.auth.postLogoutRedirectUri, getCurrentUri(), validLogoutRequest.correlationId);
            }
            else {
                this.logger.verbose("17s5rf", validLogoutRequest.correlationId);
                validLogoutRequest.postLogoutRedirectUri =
                    UrlString.getAbsoluteUrl(getCurrentUri(), getCurrentUri(), validLogoutRequest.correlationId);
            }
        }
        else {
            this.logger.verbose("0ljv63", validLogoutRequest.correlationId);
        }
        return validLogoutRequest;
    }
    /**
     * Parses login_hint ID Token Claim out of AccountInfo object to be used as
     * logout_hint in end session request.
     * @param account
     */
    getLogoutHintFromIdTokenClaims(account) {
        const idTokenClaims = account.idTokenClaims;
        if (idTokenClaims) {
            if (idTokenClaims.login_hint) {
                this.logger.verbose("0u5bmc", this.correlationId);
                return idTokenClaims.login_hint;
            }
            else {
                this.logger.verbose("0mvp54", this.correlationId);
            }
        }
        else {
            this.logger.verbose("1e7bdp", this.correlationId);
        }
        return null;
    }
    /**
     * Creates an Authorization Code Client with the given authority, or the default authority.
     * @param params {
     *         serverTelemetryManager: ServerTelemetryManager;
     *         authorityUrl?: string;
     *         requestAzureCloudOptions?: AzureCloudOptions;
     *         requestExtraQueryParameters?: StringDict;
     *         account?: AccountInfo;
     *        }
     */
    async createAuthCodeClient(params) {
        // Create auth module.
        const clientConfig = await invokeAsync(this.getClientConfiguration.bind(this), StandardInteractionClientGetClientConfiguration, this.logger, this.performanceClient, this.correlationId)(params);
        return new AuthorizationCodeClient(clientConfig, this.performanceClient);
    }
    /**
     * Creates a Client Configuration object with the given request authority, or the default authority.
     * @param params {
     *         serverTelemetryManager: ServerTelemetryManager;
     *         requestAuthority?: string;
     *         requestAzureCloudOptions?: AzureCloudOptions;
     *         requestExtraQueryParameters?: boolean;
     *         account?: AccountInfo;
     *        }
     */
    async getClientConfiguration(params) {
        const { serverTelemetryManager, requestAuthority, requestAzureCloudOptions, requestExtraQueryParameters, account, } = params;
        const discoveredAuthority = params.authority ||
            (await invokeAsync(getDiscoveredAuthority, StandardInteractionClientGetDiscoveredAuthority, this.logger, this.performanceClient, this.correlationId)(this.config, this.correlationId, this.performanceClient, this.browserStorage, this.logger, requestAuthority, requestAzureCloudOptions, requestExtraQueryParameters, account));
        const logger = this.config.system.loggerOptions;
        return {
            authOptions: {
                clientId: this.config.auth.clientId,
                authority: discoveredAuthority,
                clientCapabilities: this.config.auth.clientCapabilities,
                redirectUri: this.config.auth.redirectUri,
                isMcp: this.config.auth.isMcp,
            },
            systemOptions: {
                tokenRenewalOffsetSeconds: this.config.system.tokenRenewalOffsetSeconds,
                preventCorsPreflight: true,
            },
            loggerOptions: {
                loggerCallback: logger.loggerCallback,
                piiLoggingEnabled: logger.piiLoggingEnabled,
                logLevel: logger.logLevel,
                correlationId: this.correlationId,
            },
            cryptoInterface: this.browserCrypto,
            networkInterface: this.networkClient,
            storageInterface: this.browserStorage,
            serverTelemetryManager: serverTelemetryManager,
            libraryInfo: {
                sku: BrowserConstants.MSAL_SKU,
                version: version,
                cpu: "",
                os: "",
            },
            telemetry: this.config.telemetry,
        };
    }
}
/**
 * Helper to initialize required request parameters for interactive APIs and ssoSilent().
 *
 * @param request - The authentication request object (RedirectRequest, PopupRequest, or SsoSilentRequest).
 * @param interactionType - The type of interaction (e.g., redirect, popup, silent).
 * @param config - The browser configuration object.
 * @param browserCrypto - The cryptographic interface for browser operations.
 * @param browserStorage - The browser storage manager instance.
 * @param logger - The logger instance for logging messages.
 * @param performanceClient - The performance client for telemetry.
 * @param correlationId - The correlation ID for the request.
 * @returns A promise that resolves to a CommonAuthorizationUrlRequest object with initialized parameters.
 */
async function initializeAuthorizationRequest(request, interactionType, config, browserCrypto, browserStorage, logger, performanceClient, correlationId) {
    const redirectUri = getRedirectUri(request.redirectUri, config.auth.redirectUri, logger, correlationId);
    if (new URL(redirectUri).origin !== new URL(window.location.href).origin) {
        logger.warning("08qbvw", correlationId);
        performanceClient.addFields({ isRedirectUriCrossOrigin: true }, correlationId);
    }
    const browserState = {
        interactionType: interactionType,
    };
    const state = ProtocolUtils.setRequestState(browserCrypto, (request && request.state) || "", browserState, correlationId);
    const baseRequest = await invokeAsync(initializeBaseRequest, InitializeBaseRequest, logger, performanceClient, correlationId)({ ...request, correlationId: correlationId }, config, performanceClient, logger, correlationId);
    const interactionRequest = {
        ...baseRequest,
        redirectUri: redirectUri,
        state: state,
        nonce: request.nonce || createNewGuid(),
        responseMode: config.auth.OIDCOptions.responseMode,
    };
    const validatedRequest = {
        ...interactionRequest,
        httpMethod: validateRequestMethod(interactionRequest, config.system.protocolMode),
    };
    // Skip active account lookup if either login hint or session id is set
    if (request.loginHint || request.sid) {
        return validatedRequest;
    }
    const account = request.account || browserStorage.getActiveAccount(correlationId);
    if (account) {
        logger.verbose("1eqlb3", correlationId);
        logger.verbosePii(`0tf99t ${account.homeAccountId}`, correlationId);
        validatedRequest.account = account;
    }
    return validatedRequest;
}

export { StandardInteractionClient, initializeAuthorizationRequest };
//# sourceMappingURL=StandardInteractionClient.mjs.map
