import * as i2 from '@acorex/components/button';
import { AXButtonModule } from '@acorex/components/button';
import { AXLoadingModule } from '@acorex/components/loading';
import * as i5 from '@acorex/core/translation';
import { AXTranslationModule } from '@acorex/core/translation';
import { AXPSessionService, AXPAuthStrategyRegistryService } from '@acorex/platform/auth';
import { AXPHomePageService } from '@acorex/platform/common';
import { AXPStateMessageComponent } from '@acorex/platform/layout/components';
import * as i1 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { inject, Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { A as AXM_AUTH_CONFIG_TOKEN } from './acorex-modules-auth-acorex-modules-auth-D1GLmcE-.mjs';

class AXPOAuthCallbackComponent {
    constructor() {
        //#region ----   Services & Dependencies   ----
        this.sessionService = inject(AXPSessionService);
        this.router = inject(Router);
        this.route = inject(ActivatedRoute);
        this.homePageService = inject(AXPHomePageService);
        this.configs = inject(AXM_AUTH_CONFIG_TOKEN);
        this.authStrategyRegistry = inject(AXPAuthStrategyRegistryService);
        this.strategyName = this.sessionService.getSessionData()?.strategy;
        this.strategy = this.authStrategyRegistry.get(this.strategyName);
        //#endregion
        //#region ----   Properties   ----
        this.isLoading$ = new BehaviorSubject(true);
        this.error$ = new BehaviorSubject(null);
        this.status$ = new BehaviorSubject('Processing authorization...');
    }
    // Removed: @Input() flow
    //#endregion
    //#region ----   Lifecycle Methods   ----
    async ngOnInit() {
        try {
            const queryParams = this.route.snapshot.queryParams;
            const code = queryParams['code'];
            const error = queryParams['error'];
            // Removed: flowParam and its assignment
            if (error) {
                throw new Error(`OAuth error: ${error} - ${queryParams['error_description'] || 'Unknown error'}`);
            }
            if (!code) {
                throw new Error('No authorization code received from OAuth provider');
            }
            this.status$.next('Exchanging authorization code for tokens...');
            // Use redirect URI from injected auth configs
            const redirectUri = this.configs.authConfig?.redirectUri || '/auth/axp-oauth-callback';
            await this.sessionService.updateToken({ code, redirectUri: redirectUri });
            this.status$.next('Authentication successful! Redirecting...');
            await this.navigateAfterSuccess();
        }
        catch (error) {
            console.error('OAuth callback error:', error);
            this.error$.next('Authentication failed. Please try again.');
            this.isLoading$.next(false);
        }
        finally {
            this.isLoading$.next(false);
        }
    }
    //#endregion
    //#region ----   Navigation   ----
    async navigateAfterSuccess() {
        // Use await with router.navigate to ensure navigation completes
        if (this.sessionService.tenant?.id == null) {
            await this.router.navigate(['/auth/account/tenant-chooser']);
            return;
        }
        if (this.sessionService.application?.id == null) {
            await this.router.navigate(['/auth/account/app-chooser']);
            return;
        }
        // Navigate to home page
        await this.homePageService.navigateTo();
    }
    //#endregion
    //#region ----   UI Handlers   ----
    handleRetry() {
        this.router.navigate(['/auth/login']);
    }
    handleGoHome() {
        this.router.navigate(['/']);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPOAuthCallbackComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.4", type: AXPOAuthCallbackComponent, isStandalone: true, selector: "axp-oauth-callback", ngImport: i0, template: "<ng-container *translate=\"let t\">\n  <div class=\"ax-h-full ax-flex ax-flex-col ax-items-center ax-justify-center ax-p-8\">\n    <div class=\"ax-w-96 ax-text-center\">\n      <!-- Loading State -->\n      <axp-state-message\n        *ngIf=\"isLoading$ | async\"\n        mode=\"loading\"\n        icon=\"fa-solid fa-spinner fa-spin\"\n        [title]=\"(status$ | async) || '@auth:state-message.loading.title'\"\n        [description]=\"'@auth:state-message.loading.description'\"\n      >\n      </axp-state-message>\n\n      <!-- Error State -->\n      <axp-state-message\n        *ngIf=\"(error$ | async) && !(isLoading$ | async)\"\n        mode=\"error\"\n        icon=\"fa-solid fa-exclamation-triangle\"\n        [title]=\"'@auth:state-message.error.title'\"\n        [description]=\"(error$ | async) || '@auth:state-message.error.description'\"\n      >\n        <div slot=\"actions\" class=\"ax-flex ax-flex-col ax-gap-2 sm:ax-flex-row sm:ax-justify-center\">\n          <ax-button color=\"primary\" text=\"Try Again\" (onClick)=\"handleRetry()\" class=\"ax-w-full sm:ax-w-auto\">\n          </ax-button>\n          <ax-button color=\"default\" text=\"Go Home\" (onClick)=\"handleGoHome()\" class=\"ax-w-full sm:ax-w-auto\">\n          </ax-button>\n        </div>\n      </axp-state-message>\n\n      <!-- Success State (briefly shown before redirect) -->\n      <axp-state-message\n        *ngIf=\"!(isLoading$ | async) && !(error$ | async)\"\n        mode=\"success\"\n        icon=\"fa-solid fa-check\"\n        [title]=\"'@auth:state-message.success.title'\"\n        [description]=\"'@auth:state-message.success.description'\"\n      >\n      </axp-state-message>\n    </div>\n  </div>\n</ng-container>\n", dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "ngmodule", type: AXButtonModule }, { kind: "component", type: i2.AXButtonComponent, selector: "ax-button", inputs: ["disabled", "size", "tabIndex", "color", "look", "text", "toggleable", "selected", "iconOnly", "type", "loadingText"], outputs: ["onBlur", "onFocus", "onClick", "selectedChange", "toggleableChange", "lookChange", "colorChange", "disabledChange", "loadingTextChange"] }, { kind: "ngmodule", type: AXLoadingModule }, { kind: "ngmodule", type: AXTranslationModule }, { kind: "directive", type: i5.AXTranslatorDirective, selector: "[translate]" }, { kind: "component", type: AXPStateMessageComponent, selector: "axp-state-message", inputs: ["mode", "icon", "title", "description", "variant"] }, { kind: "pipe", type: i1.AsyncPipe, name: "async" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPOAuthCallbackComponent, decorators: [{
            type: Component,
            args: [{ selector: 'axp-oauth-callback', imports: [CommonModule, AXButtonModule, AXLoadingModule, AXTranslationModule, AXPStateMessageComponent], standalone: true, template: "<ng-container *translate=\"let t\">\n  <div class=\"ax-h-full ax-flex ax-flex-col ax-items-center ax-justify-center ax-p-8\">\n    <div class=\"ax-w-96 ax-text-center\">\n      <!-- Loading State -->\n      <axp-state-message\n        *ngIf=\"isLoading$ | async\"\n        mode=\"loading\"\n        icon=\"fa-solid fa-spinner fa-spin\"\n        [title]=\"(status$ | async) || '@auth:state-message.loading.title'\"\n        [description]=\"'@auth:state-message.loading.description'\"\n      >\n      </axp-state-message>\n\n      <!-- Error State -->\n      <axp-state-message\n        *ngIf=\"(error$ | async) && !(isLoading$ | async)\"\n        mode=\"error\"\n        icon=\"fa-solid fa-exclamation-triangle\"\n        [title]=\"'@auth:state-message.error.title'\"\n        [description]=\"(error$ | async) || '@auth:state-message.error.description'\"\n      >\n        <div slot=\"actions\" class=\"ax-flex ax-flex-col ax-gap-2 sm:ax-flex-row sm:ax-justify-center\">\n          <ax-button color=\"primary\" text=\"Try Again\" (onClick)=\"handleRetry()\" class=\"ax-w-full sm:ax-w-auto\">\n          </ax-button>\n          <ax-button color=\"default\" text=\"Go Home\" (onClick)=\"handleGoHome()\" class=\"ax-w-full sm:ax-w-auto\">\n          </ax-button>\n        </div>\n      </axp-state-message>\n\n      <!-- Success State (briefly shown before redirect) -->\n      <axp-state-message\n        *ngIf=\"!(isLoading$ | async) && !(error$ | async)\"\n        mode=\"success\"\n        icon=\"fa-solid fa-check\"\n        [title]=\"'@auth:state-message.success.title'\"\n        [description]=\"'@auth:state-message.success.description'\"\n      >\n      </axp-state-message>\n    </div>\n  </div>\n</ng-container>\n" }]
        }] });

export { AXPOAuthCallbackComponent };
//# sourceMappingURL=acorex-modules-auth-oauth-callback.component-CfTRvv9t.mjs.map
