import { AXM_AUTH_CONFIG_TOKEN } from '@acorex/modules/auth';
import { PkceUtil } from '@acorex/platform/auth';
import * as i0 from '@angular/core';
import { inject, Injectable, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

class AXCExternalAuthorizationService {
    constructor() {
        this.configs = inject(AXM_AUTH_CONFIG_TOKEN);
    }
    async signin(providerName) {
        // Get provider configuration (optional, for validation)
        const providerConfig = this.configs?.externalProviders?.[providerName];
        if (!providerConfig) {
            throw new Error(`Provider '${providerName}' is not configured`);
        }
        // Get application configuration for OIDC
        const clientId = this.configs?.authConfig?.clientId || 'spa-client';
        const redirectUri = encodeURIComponent(`${window.location.origin}/auth/axp-oauth-callback`);
        const authorizeUrl = `${this.configs?.baseUrl}/connect/authorize`;
        // Centralized PKCE code verifier logic
        const codeVerifier = PkceUtil.generateRandomString(128);
        localStorage.setItem('pkce_code_verifier', codeVerifier);
        const codeChallenge = await PkceUtil.generateCodeChallenge(codeVerifier);
        // Build the authorization URL according to the required format
        const authUrl = `${authorizeUrl}` +
            `?response_type=code` +
            `&client_id=${clientId}` +
            `&redirect_uri=${redirectUri}` +
            `&scope=openid profile email` +
            `&code_challenge=${codeChallenge}` +
            `&code_challenge_method=S256` +
            `&prompt=login` +
            `&acr_values=${providerName}`;
        console.log('Redirecting to:', authUrl);
        // Use window.location.assign to redirect
        window.location.assign(authUrl);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCExternalAuthorizationService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCExternalAuthorizationService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCExternalAuthorizationService, decorators: [{
            type: Injectable
        }] });

class AXCUtilsModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCUtilsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.3.19", ngImport: i0, type: AXCUtilsModule, imports: [CommonModule] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCUtilsModule, providers: [AXCExternalAuthorizationService], imports: [CommonModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCUtilsModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule],
                    providers: [AXCExternalAuthorizationService],
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { AXCExternalAuthorizationService, AXCUtilsModule };
//# sourceMappingURL=acorex-connectivity-utils.mjs.map
