import * as i0 from '@angular/core';
import { InjectionToken, Inject, Injectable, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { StsConfigLoader, LogLevel, provideAuth, authInterceptor } from 'angular-auth-oidc-client';
import { from, map } from 'rxjs';
import { provideHttpClient, withInterceptors } from '@angular/common/http';

const SD_OIDC_CONFIGURATION = new InjectionToken('sd-oidc.configuration');

class DynamicStsLoader extends StsConfigLoader {
    oidcConfiguration;
    constructor(oidcConfiguration) {
        super();
        this.oidcConfiguration = oidcConfiguration;
    }
    // SỬA LỖI TYPE: Trả về Observable<OpenIdConfiguration[]>
    loadConfigs() {
        const loaderPromise = this.oidcConfiguration.loadTenantConfig();
        // Dùng 'from' chuyển Promise -> Observable
        return from(loaderPromise).pipe(map(tenantConfig => {
            const authority = `${tenantConfig.url}/realms/${tenantConfig.realm}`;
            const config = {
                authority,
                redirectUrl: window.location.origin,
                postLogoutRedirectUri: window.location.origin,
                clientId: tenantConfig.clientId,
                scope: 'openid profile email offline_access',
                responseType: 'code',
                silentRenew: true, // Tự động Refresh Token
                useRefreshToken: true,
                ignoreNonceAfterRefresh: true,
                renewTimeBeforeTokenExpiresInSeconds: 10,
                logLevel: LogLevel.Warn,
                // Cập nhật domain API của bạn vào đây để Interceptor tự đính Token
                secureRoutes: tenantConfig.secureRoutes?.filter(route => !!route)?.map(route => route.trim()),
            };
            // Trả về mảng (bắt buộc với bản mới)
            return [config];
        }));
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: DynamicStsLoader, deps: [{ token: SD_OIDC_CONFIGURATION }], target: i0.ɵɵFactoryTarget.Injectable });
    static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: DynamicStsLoader });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: DynamicStsLoader, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: undefined, decorators: [{
                    type: Inject,
                    args: [SD_OIDC_CONFIGURATION]
                }] }] });

// 5. Module Wrapper
class SdOidcModule {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    static forRoot(options) {
        if (options.useFactory) {
            return {
                ngModule: SdOidcModule,
                providers: [
                    {
                        provide: SD_OIDC_CONFIGURATION,
                        useFactory: options.useFactory,
                        deps: options.deps,
                    },
                    // B. Provide Auth Logic (Dùng cú pháp provideAuth mới ngay trong NgModule)
                    provideAuth({
                        loader: {
                            provide: StsConfigLoader,
                            useClass: DynamicStsLoader,
                        },
                    }),
                    provideHttpClient(withInterceptors([authInterceptor()])),
                ],
            };
        }
        if (options.useClass) {
            return {
                ngModule: SdOidcModule,
                providers: [
                    {
                        provide: SD_OIDC_CONFIGURATION,
                        useClass: options.useClass,
                    },
                    // B. Provide Auth Logic (Dùng cú pháp provideAuth mới ngay trong NgModule)
                    provideAuth({
                        loader: {
                            provide: StsConfigLoader,
                            useClass: DynamicStsLoader,
                        },
                    }),
                    provideHttpClient(withInterceptors([authInterceptor()])),
                ],
            };
        }
        return {
            ngModule: SdOidcModule,
            providers: [
                // B. Provide Auth Logic (Dùng cú pháp provideAuth mới ngay trong NgModule)
                provideAuth({
                    loader: {
                        provide: StsConfigLoader,
                        useClass: DynamicStsLoader,
                    },
                }),
                provideHttpClient(withInterceptors([authInterceptor()])),
            ],
        };
    }
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdOidcModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.17", ngImport: i0, type: SdOidcModule, imports: [CommonModule] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdOidcModule, imports: [CommonModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdOidcModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [CommonModule],
                    exports: [],
                }]
        }] });

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

export { SD_OIDC_CONFIGURATION, SdOidcModule };
//# sourceMappingURL=sd-angular-core-modules-oidc.mjs.map
