import { Component, EventEmitter } from '@angular/core';
import { AuthConfigurationService } from './auth-configuration.service';
import { AlertService, LoginService, ModalService } from '@c8y/ngx-components';
import { TenantLoginOptionsService } from '@c8y/client';
import { cloneDeep } from 'lodash-es';
import { BehaviorSubject } from 'rxjs';
import { tap, switchMap, shareReplay } from 'rxjs/operators';
import * as i0 from "@angular/core";
import * as i1 from "./auth-configuration.service";
import * as i2 from "@c8y/client";
import * as i3 from "@c8y/ngx-components";
import * as i4 from "@angular/common";
import * as i5 from "@angular/forms";
import * as i6 from "./session-configuration.component";
import * as i7 from "./login-settings.component";
import * as i8 from "./basic-auth-settings.component";
import * as i9 from "./tfa-settings.component";
export class AuthConfigurationComponent {
    constructor(authConfigurationService, tenantLoginOptionsService, modalService, loginService, alertService) {
        this.authConfigurationService = authConfigurationService;
        this.tenantLoginOptionsService = tenantLoginOptionsService;
        this.modalService = modalService;
        this.loginService = loginService;
        this.alertService = alertService;
        this.reloading$ = new BehaviorSubject(false);
        this.reload = new EventEmitter();
        this.authConfiguration$ = this.reload.pipe(tap(() => this.reloading$.next(true)), switchMap(() => this.authConfigurationService.getAuthConfiguration$()), tap(() => this.reloading$.next(false)), shareReplay(1));
    }
    ngOnInit() {
        this.authConfigurationSubscription = this.authConfiguration$.subscribe((authConfiguration) => {
            this.authConfiguration = authConfiguration;
            this.previousAuthConfiguration = cloneDeep(this.authConfiguration);
        });
        this.loadAuthConfig();
    }
    loadAuthConfig() {
        this.reload.next();
    }
    ngOnDestroy() {
        this.authConfigurationSubscription.unsubscribe();
    }
    async save() {
        try {
            await this.modalService.confirmLogout();
            await this.authConfigurationService.save(this.authConfiguration, this.previousAuthConfiguration);
            await this.loginService.logout(true);
        }
        catch (ex) {
            if (ex) {
                this.alertService.addServerFailure(ex);
            }
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: AuthConfigurationComponent, deps: [{ token: i1.AuthConfigurationService }, { token: i2.TenantLoginOptionsService }, { token: i3.ModalService }, { token: i3.LoginService }, { token: i3.AlertService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.9", type: AuthConfigurationComponent, selector: "c8y-auth-configuration", ngImport: i0, template: "<c8y-title>{{ 'Authentication' | translate }}</c8y-title>\n\n<c8y-breadcrumb>\n  <c8y-breadcrumb-item\n    [icon]=\"'cog'\"\n    [label]=\"'Settings' | translate\"\n  ></c8y-breadcrumb-item>\n  <c8y-breadcrumb-item\n    [icon]=\"'cog'\"\n    [label]=\"'Authentication' | translate\"\n  ></c8y-breadcrumb-item>\n</c8y-breadcrumb>\n\n<c8y-action-bar-item [placement]=\"'right'\">\n  <button\n    class=\"btn btn-link\"\n    title=\"{{ 'Reload' | translate }}\"\n    (click)=\"loadAuthConfig()\"\n  >\n    <i\n      c8yIcon=\"refresh\"\n      [ngClass]=\"{ 'icon-spin': reloading$ | async }\"\n    ></i>\n    {{ 'Reload' | translate }}\n  </button>\n</c8y-action-bar-item>\n\n<c8y-help src=\"/docs/authentication/basic-settings/#basic-settings\"></c8y-help>\n\n<form\n  class=\"card card--fullpage\"\n  #authConfigurationForm=\"ngForm\"\n  novalidate\n>\n  <div class=\"card-header separator\">\n    <div class=\"card-title\">\n      {{ 'Authentication' | translate }}\n    </div>\n  </div>\n  <div\n    class=\"inner-scroll\"\n    *ngIf=\"authConfiguration\"\n  >\n    <c8y-login-settings [authConfiguration]=\"authConfiguration\"></c8y-login-settings>\n    <c8y-basic-auth-settings [authConfiguration]=\"authConfiguration\"></c8y-basic-auth-settings>\n    <c8y-session-configuration [authConfiguration]=\"authConfiguration\"></c8y-session-configuration>\n    <c8y-auth-tfa [authConfiguration]=\"authConfiguration\"></c8y-auth-tfa>\n  </div>\n  <div class=\"card-footer separator\">\n    <button\n      class=\"btn btn-primary\"\n      title=\"{{ 'Save' | translate }}\"\n      type=\"submit\"\n      (click)=\"save()\"\n      [disabled]=\"!authConfigurationForm.form.valid || authConfigurationForm.form.pristine\"\n    >\n      {{ 'Save' | translate }}\n    </button>\n  </div>\n</form>\n", dependencies: [{ kind: "component", type: i3.ActionBarItemComponent, selector: "c8y-action-bar-item", inputs: ["placement", "priority", "itemClass", "injector", "groupId", "inGroupPriority"] }, { kind: "component", type: i3.BreadcrumbComponent, selector: "c8y-breadcrumb" }, { kind: "component", type: i3.BreadcrumbItemComponent, selector: "c8y-breadcrumb-item", inputs: ["icon", "translate", "label", "path", "injector"] }, { kind: "directive", type: i3.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i4.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i4.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i3.TitleComponent, selector: "c8y-title", inputs: ["pageTitleUpdate"] }, { kind: "directive", type: i5.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i5.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i5.NgForm, selector: "form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]", inputs: ["ngFormOptions"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "component", type: i3.HelpComponent, selector: "c8y-help", inputs: ["src", "isCollapsed", "priority", "icon"] }, { kind: "component", type: i6.SessionConfigurationComponent, selector: "c8y-session-configuration", inputs: ["authConfiguration"] }, { kind: "component", type: i7.LoginSettingsComponent, selector: "c8y-login-settings", inputs: ["authConfiguration"] }, { kind: "component", type: i8.BasicAuthSettingsComponent, selector: "c8y-basic-auth-settings", inputs: ["authConfiguration"] }, { kind: "component", type: i9.TfaSettingsComponent, selector: "c8y-auth-tfa", inputs: ["authConfiguration"] }, { kind: "pipe", type: i3.C8yTranslatePipe, name: "translate" }, { kind: "pipe", type: i4.AsyncPipe, name: "async" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: AuthConfigurationComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-auth-configuration', template: "<c8y-title>{{ 'Authentication' | translate }}</c8y-title>\n\n<c8y-breadcrumb>\n  <c8y-breadcrumb-item\n    [icon]=\"'cog'\"\n    [label]=\"'Settings' | translate\"\n  ></c8y-breadcrumb-item>\n  <c8y-breadcrumb-item\n    [icon]=\"'cog'\"\n    [label]=\"'Authentication' | translate\"\n  ></c8y-breadcrumb-item>\n</c8y-breadcrumb>\n\n<c8y-action-bar-item [placement]=\"'right'\">\n  <button\n    class=\"btn btn-link\"\n    title=\"{{ 'Reload' | translate }}\"\n    (click)=\"loadAuthConfig()\"\n  >\n    <i\n      c8yIcon=\"refresh\"\n      [ngClass]=\"{ 'icon-spin': reloading$ | async }\"\n    ></i>\n    {{ 'Reload' | translate }}\n  </button>\n</c8y-action-bar-item>\n\n<c8y-help src=\"/docs/authentication/basic-settings/#basic-settings\"></c8y-help>\n\n<form\n  class=\"card card--fullpage\"\n  #authConfigurationForm=\"ngForm\"\n  novalidate\n>\n  <div class=\"card-header separator\">\n    <div class=\"card-title\">\n      {{ 'Authentication' | translate }}\n    </div>\n  </div>\n  <div\n    class=\"inner-scroll\"\n    *ngIf=\"authConfiguration\"\n  >\n    <c8y-login-settings [authConfiguration]=\"authConfiguration\"></c8y-login-settings>\n    <c8y-basic-auth-settings [authConfiguration]=\"authConfiguration\"></c8y-basic-auth-settings>\n    <c8y-session-configuration [authConfiguration]=\"authConfiguration\"></c8y-session-configuration>\n    <c8y-auth-tfa [authConfiguration]=\"authConfiguration\"></c8y-auth-tfa>\n  </div>\n  <div class=\"card-footer separator\">\n    <button\n      class=\"btn btn-primary\"\n      title=\"{{ 'Save' | translate }}\"\n      type=\"submit\"\n      (click)=\"save()\"\n      [disabled]=\"!authConfigurationForm.form.valid || authConfigurationForm.form.pristine\"\n    >\n      {{ 'Save' | translate }}\n    </button>\n  </div>\n</form>\n" }]
        }], ctorParameters: () => [{ type: i1.AuthConfigurationService }, { type: i2.TenantLoginOptionsService }, { type: i3.ModalService }, { type: i3.LoginService }, { type: i3.AlertService }] });
//# sourceMappingURL=data:application/json;base64, 