import { Component, Input } from '@angular/core';
import { TenantLoginOptionType } from '@c8y/client';
import { ControlContainer, NgForm } from '@angular/forms';
import { UserAgent, isBasic } from './basic-settings.model';
import * as i0 from "@angular/core";
import * as i1 from "@angular/forms";
import * as i2 from "@angular/common";
import * as i3 from "@c8y/ngx-components";
export class BasicAuthSettingsComponent {
    constructor(controlContainer) {
        this.controlContainer = controlContainer;
        this.preferredLoginOptionType = TenantLoginOptionType.BASIC;
        this.tenantLoginOptionTypeEnum = TenantLoginOptionType;
    }
    ngOnChanges(changes) {
        if (changes.authConfiguration && changes.authConfiguration.currentValue) {
            this.preferredLoginOptionType =
                changes.authConfiguration.currentValue.preferredLoginOptionType;
        }
    }
    ngDoCheck() {
        if (this.preferredLoginOptionType !== this.authConfiguration.preferredLoginOptionType) {
            this.preferredLoginOptionType = this.authConfiguration.preferredLoginOptionType;
            if (this.preferredLoginOptionType === TenantLoginOptionType.BASIC) {
                this.forbiddenWebBrowsers = false;
            }
        }
    }
    get forbiddenWebBrowsers() {
        return this.authenticationRestrictions.forbiddenClients.includes('WEB_BROWSERS');
    }
    set forbiddenWebBrowsers(value) {
        this.authenticationRestrictions.forbiddenClients = value ? ['WEB_BROWSERS'] : [];
    }
    forbiddenUserAgentsRemove(id) {
        this.authenticationRestrictions.forbiddenUserAgents = this.remove(this.authenticationRestrictions.forbiddenUserAgents, id);
        this.controlContainer.control.markAsDirty();
    }
    trustedUserAgentsRemove(id) {
        this.authenticationRestrictions.trustedUserAgents = this.remove(this.authenticationRestrictions.trustedUserAgents, id);
        this.controlContainer.control.markAsDirty();
    }
    get authenticationRestrictions() {
        return this.authConfiguration.loginOptions.find(isBasic).authenticationRestrictions;
    }
    add(collection) {
        collection.push(new UserAgent(''));
    }
    remove(collection, id) {
        const newArray = collection.filter(obj => obj.id !== id);
        if (!newArray.length) {
            newArray.push(new UserAgent(''));
        }
        return newArray;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BasicAuthSettingsComponent, deps: [{ token: i1.ControlContainer }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: BasicAuthSettingsComponent, selector: "c8y-basic-auth-settings", inputs: { authConfiguration: "authConfiguration" }, usesOnChanges: true, ngImport: i0, template: "<div\n  class=\"card-block separator-top\"\n  *ngIf=\"authConfiguration.preferredLoginOptionType !== 'BASIC'\"\n>\n  <div class=\"col-sm-2\">\n    <div class=\"h4 text-normal text-right text-left-xs\">\n      {{ 'Basic Auth restrictions' | translate }}\n    </div>\n  </div>\n\n  <div class=\"col-sm-9\">\n    <div class=\"row\">\n      <div class=\"col-sm-6\">\n        <c8y-form-group>\n          <label class=\"c8y-switch\" title=\"{{ 'Forbidden for web browsers' | translate }}\">\n            <input type=\"checkbox\" name=\"forbiddenWebBrowsers\" [(ngModel)]=\"forbiddenWebBrowsers\" />\n            <span></span>\n            <span>{{ 'Forbidden for web browsers' | translate }}</span>\n          </label>\n          <div\n            class=\"alert alert-warning\"\n            *ngIf=\"\n              preferredLoginOptionType === tenantLoginOptionTypeEnum.BASIC && forbiddenWebBrowsers\n            \"\n          >\n            {{\n              'You are about to forbid browsers from using Basic authentication. This will prevent users from using web applications on your tenant because you are going to set Basic authentication as the preferred login mode.'\n                | translate\n            }}\n          </div>\n        </c8y-form-group>\n      </div>\n    </div>\n\n    <div class=\"row\">\n      <div class=\"col-sm-6\">\n        <label title=\"{{ 'Forbidden user agents' | translate }}\">\n          {{ 'Forbidden user agents' | translate }}\n        </label>\n        <div\n          class=\"input-group m-t-8\"\n          *ngFor=\"\n            let forbiddenUserAgent of authenticationRestrictions.forbiddenUserAgents;\n            last as isLast;\n            first as isFirst\n          \"\n        >\n          <input\n            type=\"text\"\n            [name]=\"'forbiddenUserAgent' + forbiddenUserAgent.id\"\n            [(ngModel)]=\"forbiddenUserAgent.value\"\n            class=\"form-control\"\n            data-cy=\"c8y-basic-auth--forbidden-agent\"\n            placeholder=\"{{ 'e.g.' | translate }} forbidden-agent\"\n          />\n          <div class=\"input-group-btn col-sm-2\">\n            <button\n              *ngIf=\"!(isFirst && isLast && forbiddenUserAgent.value === '')\"\n              title=\"{{ 'Remove' | translate }}\"\n              [name]=\"'forbiddenUserAgentRemove' + forbiddenUserAgent.id\"\n              type=\"button\"\n              class=\"btn btn-dot text-primary\"\n              (click)=\"forbiddenUserAgentsRemove(forbiddenUserAgent.id)\"\n            >\n              <i class=\"dlt-c8y-icon-minus-circle text-danger\"></i>\n            </button>\n            <button\n              title=\"{{ 'Add' | translate }}\"\n              type=\"button\"\n              class=\"btn btn-dot text-primary\"\n              (click)=\"add(authenticationRestrictions.forbiddenUserAgents)\"\n              *ngIf=\"isLast\"\n            >\n              <i class=\"dlt-c8y-icon-plus-circle\"></i>\n            </button>\n          </div>\n        </div>\n      </div>\n      <div class=\"col-sm-6\">\n        <label title=\"{{ 'Trusted user agents' | translate }}\">\n          {{ 'Trusted user agents' | translate }}\n        </label>\n        <div\n          class=\"input-group m-t-8\"\n          *ngFor=\"\n            let trustedUserAgent of authenticationRestrictions.trustedUserAgents;\n            last as isLast;\n            first as isFirst\n          \"\n        >\n          <input\n            type=\"text\"\n            [name]=\"'trustedUserAgent' + trustedUserAgent.id\"\n            class=\"form-control\"\n            placeholder=\"{{ 'e.g.' | translate }} trusted-agent\"\n            data-cy=\"c8y-basic-auth--trusted-agent\"\n            [(ngModel)]=\"trustedUserAgent.value\"\n          />\n          <div class=\"input-group-btn col-sm-2\">\n            <button\n              *ngIf=\"!(isFirst && isLast && trustedUserAgent.value === '')\"\n              title=\"{{ 'Remove' | translate }}\"\n              [name]=\"'trustedUserAgentRemove' + trustedUserAgent.id\"\n              type=\"button\"\n              class=\"btn btn-dot btn-dot--danger text-primary\"\n              (click)=\"trustedUserAgentsRemove(trustedUserAgent.id)\"\n            >\n              <i class=\"dlt-c8y-icon-minus-circle\"></i>\n            </button>\n            <button\n              title=\"{{ 'Add' | translate }}\"\n              type=\"button\"\n              (click)=\"add(authenticationRestrictions.trustedUserAgents)\"\n              class=\"btn btn-dot text-primary\"\n              *ngIf=\"isLast\"\n            >\n              <i class=\"dlt-c8y-icon-plus-circle\"></i>\n            </button>\n          </div>\n        </div>\n      </div>\n    </div>\n  </div>\n</div>\n", dependencies: [{ kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i1.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i1.CheckboxControlValueAccessor, selector: "input[type=checkbox][formControlName],input[type=checkbox][formControl],input[type=checkbox][ngModel]" }, { kind: "directive", type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "component", type: i3.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "pipe", type: i3.C8yTranslatePipe, name: "translate" }], viewProviders: [{ provide: ControlContainer, useExisting: NgForm }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: BasicAuthSettingsComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-basic-auth-settings', viewProviders: [{ provide: ControlContainer, useExisting: NgForm }], template: "<div\n  class=\"card-block separator-top\"\n  *ngIf=\"authConfiguration.preferredLoginOptionType !== 'BASIC'\"\n>\n  <div class=\"col-sm-2\">\n    <div class=\"h4 text-normal text-right text-left-xs\">\n      {{ 'Basic Auth restrictions' | translate }}\n    </div>\n  </div>\n\n  <div class=\"col-sm-9\">\n    <div class=\"row\">\n      <div class=\"col-sm-6\">\n        <c8y-form-group>\n          <label class=\"c8y-switch\" title=\"{{ 'Forbidden for web browsers' | translate }}\">\n            <input type=\"checkbox\" name=\"forbiddenWebBrowsers\" [(ngModel)]=\"forbiddenWebBrowsers\" />\n            <span></span>\n            <span>{{ 'Forbidden for web browsers' | translate }}</span>\n          </label>\n          <div\n            class=\"alert alert-warning\"\n            *ngIf=\"\n              preferredLoginOptionType === tenantLoginOptionTypeEnum.BASIC && forbiddenWebBrowsers\n            \"\n          >\n            {{\n              'You are about to forbid browsers from using Basic authentication. This will prevent users from using web applications on your tenant because you are going to set Basic authentication as the preferred login mode.'\n                | translate\n            }}\n          </div>\n        </c8y-form-group>\n      </div>\n    </div>\n\n    <div class=\"row\">\n      <div class=\"col-sm-6\">\n        <label title=\"{{ 'Forbidden user agents' | translate }}\">\n          {{ 'Forbidden user agents' | translate }}\n        </label>\n        <div\n          class=\"input-group m-t-8\"\n          *ngFor=\"\n            let forbiddenUserAgent of authenticationRestrictions.forbiddenUserAgents;\n            last as isLast;\n            first as isFirst\n          \"\n        >\n          <input\n            type=\"text\"\n            [name]=\"'forbiddenUserAgent' + forbiddenUserAgent.id\"\n            [(ngModel)]=\"forbiddenUserAgent.value\"\n            class=\"form-control\"\n            data-cy=\"c8y-basic-auth--forbidden-agent\"\n            placeholder=\"{{ 'e.g.' | translate }} forbidden-agent\"\n          />\n          <div class=\"input-group-btn col-sm-2\">\n            <button\n              *ngIf=\"!(isFirst && isLast && forbiddenUserAgent.value === '')\"\n              title=\"{{ 'Remove' | translate }}\"\n              [name]=\"'forbiddenUserAgentRemove' + forbiddenUserAgent.id\"\n              type=\"button\"\n              class=\"btn btn-dot text-primary\"\n              (click)=\"forbiddenUserAgentsRemove(forbiddenUserAgent.id)\"\n            >\n              <i class=\"dlt-c8y-icon-minus-circle text-danger\"></i>\n            </button>\n            <button\n              title=\"{{ 'Add' | translate }}\"\n              type=\"button\"\n              class=\"btn btn-dot text-primary\"\n              (click)=\"add(authenticationRestrictions.forbiddenUserAgents)\"\n              *ngIf=\"isLast\"\n            >\n              <i class=\"dlt-c8y-icon-plus-circle\"></i>\n            </button>\n          </div>\n        </div>\n      </div>\n      <div class=\"col-sm-6\">\n        <label title=\"{{ 'Trusted user agents' | translate }}\">\n          {{ 'Trusted user agents' | translate }}\n        </label>\n        <div\n          class=\"input-group m-t-8\"\n          *ngFor=\"\n            let trustedUserAgent of authenticationRestrictions.trustedUserAgents;\n            last as isLast;\n            first as isFirst\n          \"\n        >\n          <input\n            type=\"text\"\n            [name]=\"'trustedUserAgent' + trustedUserAgent.id\"\n            class=\"form-control\"\n            placeholder=\"{{ 'e.g.' | translate }} trusted-agent\"\n            data-cy=\"c8y-basic-auth--trusted-agent\"\n            [(ngModel)]=\"trustedUserAgent.value\"\n          />\n          <div class=\"input-group-btn col-sm-2\">\n            <button\n              *ngIf=\"!(isFirst && isLast && trustedUserAgent.value === '')\"\n              title=\"{{ 'Remove' | translate }}\"\n              [name]=\"'trustedUserAgentRemove' + trustedUserAgent.id\"\n              type=\"button\"\n              class=\"btn btn-dot btn-dot--danger text-primary\"\n              (click)=\"trustedUserAgentsRemove(trustedUserAgent.id)\"\n            >\n              <i class=\"dlt-c8y-icon-minus-circle\"></i>\n            </button>\n            <button\n              title=\"{{ 'Add' | translate }}\"\n              type=\"button\"\n              (click)=\"add(authenticationRestrictions.trustedUserAgents)\"\n              class=\"btn btn-dot text-primary\"\n              *ngIf=\"isLast\"\n            >\n              <i class=\"dlt-c8y-icon-plus-circle\"></i>\n            </button>\n          </div>\n        </div>\n      </div>\n    </div>\n  </div>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.ControlContainer }], propDecorators: { authConfiguration: [{
                type: Input
            }] } });
//# sourceMappingURL=data:application/json;base64, 