import * as i0 from '@angular/core';
import { InjectionToken, signal, Optional, Inject, Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { SdNormalizeAsync, SdResolveMaybeAsync } from '@sd-angular/core/utilities';
import { toSignal } from '@angular/core/rxjs-interop';
import * as i1 from '@angular/router';

const SD_AUTH_CONFIGURATION = new InjectionToken('sd.auth.configuration');

class SdAuthService {
    authConfiguration;
    // Đăng nhập
    // #signin = new Subject<void>();
    // signin$?: Observable<void>;
    getAuthInfo;
    // Đăng xuất
    #signout = new Subject();
    signout$;
    // Thay đổi mật khẩu
    #changePassword = new Subject();
    changePassword$;
    constructor(authConfiguration) {
        this.authConfiguration = authConfiguration;
        // this.#handleSignin();
        this.#handleSignout();
        this.#handleChangePassword();
        const defaultUser = {
            id: 'guest-id',
            username: 'guest',
            firstName: 'Guest',
            email: 'guest@gmail.com',
        };
        if (this.authConfiguration?.guard?.authInfo) {
            this.getAuthInfo = toSignal(SdNormalizeAsync(this.authConfiguration.guard?.authInfo()), {
                initialValue: defaultUser,
            });
        }
        else {
            this.getAuthInfo = signal(defaultUser);
        }
    }
    // #handleSignin = () => {
    //   this.signin$ = this.#signin.asObservable();
    // };
    #handleSignout = () => {
        this.signout$ = this.#signout.asObservable();
    };
    #handleChangePassword = () => {
        this.changePassword$ = this.#changePassword.asObservable();
    };
    signout = () => {
        if (this.authConfiguration?.action?.signout) {
            SdResolveMaybeAsync(this.authConfiguration.action.signout()).then(() => {
                this.#signout.next();
            });
        }
    };
    changePassword = () => {
        if (this.authConfiguration?.action?.changePassword) {
            SdResolveMaybeAsync(this.authConfiguration.action.changePassword()).then(() => {
                this.#changePassword.next();
            });
        }
    };
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdAuthService, deps: [{ token: SD_AUTH_CONFIGURATION, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
    static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdAuthService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdAuthService, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }], ctorParameters: () => [{ type: undefined, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [SD_AUTH_CONFIGURATION]
                }] }] });

class SdAuthGuard {
    authConfiguration;
    router;
    constructor(authConfiguration, router) {
        this.authConfiguration = authConfiguration;
        this.router = router;
    }
    canActivate = (route, state) => {
        if (this.authConfiguration?.guard?.auth) {
            return this.authConfiguration.guard.auth(route, state);
        }
        // Nếu chưa tích hợp Auth Guard thì pass canActive
        return true;
    };
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdAuthGuard, deps: [{ token: SD_AUTH_CONFIGURATION, optional: true }, { token: i1.Router }], target: i0.ɵɵFactoryTarget.Injectable });
    static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdAuthGuard, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdAuthGuard, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }], ctorParameters: () => [{ type: undefined, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [SD_AUTH_CONFIGURATION]
                }] }, { type: i1.Router }] });

class SdPortalGuard {
    authConfiguration;
    router;
    constructor(authConfiguration, router) {
        this.authConfiguration = authConfiguration;
        this.router = router;
    }
    canActivate = (route, state) => {
        if (this.authConfiguration?.guard?.portal) {
            return this.authConfiguration.guard.portal(route, state);
        }
        // Nếu chưa tích hợp Portal Guard thì pass canActive
        return true;
    };
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdPortalGuard, deps: [{ token: SD_AUTH_CONFIGURATION, optional: true }, { token: i1.Router }], target: i0.ɵɵFactoryTarget.Injectable });
    static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdPortalGuard, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.17", ngImport: i0, type: SdPortalGuard, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }], ctorParameters: () => [{ type: undefined, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [SD_AUTH_CONFIGURATION]
                }] }, { type: i1.Router }] });

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

export { SD_AUTH_CONFIGURATION, SdAuthGuard, SdAuthService, SdPortalGuard };
//# sourceMappingURL=sd-angular-core-modules-auth.mjs.map
