// tslint:disable:no-string-literal
import { Component, Input } from '@angular/core';
import { gettext } from '@c8y/ngx-components';
import { isEmpty } from 'lodash-es';
import { ControlContainer, NgForm } from '@angular/forms';
import { TfaStrategy, TenantLoginOptionType } from '@c8y/client';
var TfaStateEnum;
(function (TfaStateEnum) {
    TfaStateEnum[TfaStateEnum["TFA_UNDEFINED_BY_SYSTEM"] = 0] = "TFA_UNDEFINED_BY_SYSTEM";
    TfaStateEnum[TfaStateEnum["TFA_ENFORCED_FOR_GROUP"] = 1] = "TFA_ENFORCED_FOR_GROUP";
    TfaStateEnum[TfaStateEnum["TFA_ENABLED_BY_SYSTEM"] = 2] = "TFA_ENABLED_BY_SYSTEM";
    TfaStateEnum[TfaStateEnum["TFA_ENFORCED_BY_SYSTEM"] = 3] = "TFA_ENFORCED_BY_SYSTEM";
})(TfaStateEnum || (TfaStateEnum = {}));
export class TfaSettingsComponent {
    constructor() {
        this.preferredLoginOptionType = TenantLoginOptionType.BASIC;
        this.tfaStateEnum = TfaStateEnum;
        this.tfaStrategyEnum = TfaStrategy;
        this.tenantLoginOptionTypeEnum = TenantLoginOptionType;
        this.TOTP_REQUIRES_OAUTH_POPOVER = gettext('TOTP requires OAI-Secure login mode.');
        this.SMS_APP_NOT_SUBSCRIBED_POPOVER = gettext('SMS strategy requires messaging application to be subscribed.');
        this.TFA_IS_ENFORCED_BY_SYSTEM_POPOVER = gettext('The setting is enforced on the platform level.');
        this.TFA_IS_ENABLED_BY_SYSTEM_POPOVER = gettext('The setting is enabled on the platform level.');
        this.TOKEN_VALIDITY_DETERMINED_BY_JWT_POPOVER = gettext("In OAI-Secure login mode, the token's validity limit is determined by the JWT token and cannot be edited here.");
        this.TFA_IS_ENABLED_BY_ENFORCE_FOR_GROUP_POPOVER = gettext('The setting is enabled on the platform level because it is enforced for particular roles.');
    }
    ngOnChanges(changes) {
        if (changes.authConfiguration && changes.authConfiguration.currentValue) {
            this.smsGatewayAvailable = changes.authConfiguration.currentValue.smsGatewayAvailable;
            this.preferredLoginOptionType = changes.authConfiguration.currentValue.preferredLoginOptionType;
        }
    }
    ngDoCheck() {
        if (this.preferredLoginOptionType !== this.authConfiguration.preferredLoginOptionType) {
            this.preferredLoginOptionType = this.authConfiguration.preferredLoginOptionType;
            this.tenantTfaStrategy = this.tfaBySmsCanBeSet ? TfaStrategy.SMS : TfaStrategy.TOTP;
        }
    }
    get tenantTfaTokenValidity() {
        return this.authConfiguration.tenantOptions['two-factor-authentication']['token.validity'];
    }
    set tenantTfaTokenValidity(value) {
        this.authConfiguration.tenantOptions['two-factor-authentication']['token.validity'] = value;
    }
    get tenantTfaPinValidity() {
        return this.authConfiguration.tenantOptions['two-factor-authentication']['pin.validity'];
    }
    set tenantTfaPinValidity(value) {
        this.authConfiguration.tenantOptions['two-factor-authentication']['pin.validity'] = value;
    }
    get tenantTfaEnabled() {
        return this.authConfiguration.tenantOptions['two-factor-authentication']['enabled'];
    }
    set tenantTfaEnabled(value) {
        this.authConfiguration.tenantOptions['two-factor-authentication']['enabled'] = value;
    }
    get tenantTfaEnforced() {
        return this.authConfiguration.tenantOptions['two-factor-authentication']['enforced'];
    }
    set tenantTfaEnforced(value) {
        this.authConfiguration.tenantOptions['two-factor-authentication']['enforced'] = value;
    }
    get tenantTfaStrategy() {
        return this.authConfiguration.tenantOptions['two-factor-authentication']['strategy'];
    }
    set tenantTfaStrategy(value) {
        this.authConfiguration.tenantOptions['two-factor-authentication']['strategy'] = value;
    }
    get systemTfaEnforcedGroup() {
        return this.authConfiguration.systemOptions['two-factor-authentication']['enforced.group'];
    }
    get systemTfaTenantScopeSettingEnabled() {
        return this.authConfiguration.systemOptions['two-factor-authentication']['tenant-scope-settings.enabled'];
    }
    get systemTfaEnabled() {
        return this.authConfiguration.systemOptions['two-factor-authentication']['enabled'];
    }
    get systemTfaEnforced() {
        return this.authConfiguration.systemOptions['two-factor-authentication']['enforced'];
    }
    get tfaState() {
        if (this.systemTfaEnforced) {
            return this.tfaStateEnum.TFA_ENFORCED_BY_SYSTEM;
        }
        if (!isEmpty(this.systemTfaEnforcedGroup)) {
            return this.tfaStateEnum.TFA_ENFORCED_FOR_GROUP;
        }
        if (this.systemTfaEnabled) {
            return this.tfaStateEnum.TFA_ENABLED_BY_SYSTEM;
        }
        return this.tfaStateEnum.TFA_UNDEFINED_BY_SYSTEM;
    }
    get tfaBySmsCanBeSet() {
        return (this.tfaState !== this.tfaStateEnum.TFA_UNDEFINED_BY_SYSTEM || this.tenantTfaEnabled) && this.smsGatewayAvailable;
    }
    get tfaByTotpCanBeSet() {
        return ((this.tfaState !== this.tfaStateEnum.TFA_UNDEFINED_BY_SYSTEM || this.tenantTfaEnabled) &&
            this.preferredLoginOptionType === TenantLoginOptionType.OAUTH2_INTERNAL);
    }
}
TfaSettingsComponent.decorators = [
    { type: Component, args: [{
                selector: 'c8y-auth-tfa',
                template: "<div\n  class=\"card-block separator-top\"\n  *ngIf=\"preferredLoginOptionType !== tenantLoginOptionTypeEnum.OAUTH2\"\n>\n  <div class=\"col-sm-2\">\n    <h4 class=\"text-right\">{{ 'Two-factor authentication' | translate }}</h4>\n  </div>\n\n  <div class=\"col-sm-9\">\n    <c8y-form-group>\n      <label\n        class=\"c8y-switch\"\n        title=\"{{ 'Enable two-factor authentication' | translate }}\"\n        *ngIf=\"\n          tfaState === tfaStateEnum.TFA_UNDEFINED_BY_SYSTEM;\n          else enabledOrEnforcedOnSystemLevelTemplate\n        \"\n      >\n        <input type=\"checkbox\" [(ngModel)]=\"tenantTfaEnabled\" name=\"tenantTfaEnabled\" />\n        <span></span>\n        <span>{{ 'Enable' | translate }}</span>\n      </label>\n\n      <ng-template #enabledOrEnforcedOnSystemLevelTemplate>\n        <div [ngSwitch]=\"tfaState\">\n          <span *ngSwitchCase=\"tfaStateEnum.TFA_ENABLED_BY_SYSTEM\">\n            {{ 'Two-factor authentication is enabled on all users' | translate }}\n            <button\n              class=\"btn btn-clean\"\n              popover=\"{{ TFA_IS_ENABLED_BY_SYSTEM_POPOVER | translate }}\"\n              [outsideClick]=\"true\"\n              placement=\"bottom\"\n            >\n              <i [c8yIcon]=\"'question-circle-o'\" class=\"text-info\"></i>\n            </button>\n          </span>\n          <div *ngSwitchCase=\"tfaStateEnum.TFA_ENFORCED_BY_SYSTEM\">\n            <span>\n              {{ 'Two-factor authentication is enforced on all users' | translate }}\n              <button\n                class=\"btn btn-clean\"\n                popover=\"{{ TFA_IS_ENFORCED_BY_SYSTEM_POPOVER | translate }}\"\n                placement=\"bottom\"\n                [outsideClick]=\"true\"\n              >\n                <i [c8yIcon]=\"'question-circle-o'\" class=\"text-info\"></i>\n              </button>\n            </span>\n          </div>\n          <div *ngSwitchCase=\"tfaStateEnum.TFA_ENFORCED_FOR_GROUP\">\n            <span>\n              <span translate [translateParams]=\"{ role: systemTfaEnforcedGroup }\" ngNonBindable>\n                Two-factor authentication is enabled on all users and enforced on users with role\n                {{ role }}.\n              </span>\n              <button\n                class=\"btn btn-clean\"\n                popover=\"{{ TFA_IS_ENABLED_BY_ENFORCE_FOR_GROUP_POPOVER | translate }}\"\n                [outsideClick]=\"true\"\n                placement=\"bottom\"\n              >\n                <i [c8yIcon]=\"'question-circle-o'\" class=\"text-info\"></i>\n              </button>\n            </span>\n          </div>\n        </div>\n      </ng-template>\n    </c8y-form-group>\n\n    <fieldset *ngIf=\"tfaBySmsCanBeSet || tfaByTotpCanBeSet\">\n      <div class=\"row\">\n        <c8y-form-group class=\"col-sm-6\">\n          <label title=\"{{ 'TFA strategy' | translate }}\">\n            {{ 'TFA strategy' | translate }}\n            <button\n              class=\"btn btn-clean\"\n              popover=\"{{ SMS_APP_NOT_SUBSCRIBED_POPOVER | translate }}\"\n              placement=\"bottom\"\n              [outsideClick]=\"true\"\n              *ngIf=\"!tfaBySmsCanBeSet\"\n            >\n              <i [c8yIcon]=\"'question-circle-o'\" class=\"text-info\"></i>\n            </button>\n            <button\n              class=\"btn btn-clean\"\n              popover=\"{{ TOTP_REQUIRES_OAUTH_POPOVER | translate }}\"\n              placement=\"bottom\"\n              [outsideClick]=\"true\"\n              *ngIf=\"!tfaByTotpCanBeSet\"\n            >\n              <i [c8yIcon]=\"'question-circle-o'\" class=\"text-info\"></i>\n            </button>\n          </label>\n\n          <div class=\"c8y-select-wrapper\">\n            <select class=\"form-control\" [(ngModel)]=\"tenantTfaStrategy\" name=\"tenantTfaStrategy\">\n              <option value=\"{{ tfaStrategyEnum.SMS }}\" translate [disabled]=\"!tfaBySmsCanBeSet\">\n                SMS based\n              </option>\n              <option value=\"{{ tfaStrategyEnum.TOTP }}\" translate [disabled]=\"!tfaByTotpCanBeSet\">\n                Google Authenticator (TOTP)\n              </option>\n            </select>\n            <span></span>\n          </div>\n        </c8y-form-group>\n      </div>\n\n      <div\n        class=\"row\"\n        *ngIf=\"\n          tenantTfaStrategy === tfaStrategyEnum.TOTP &&\n          tfaState !== tfaStateEnum.TFA_ENFORCED_BY_SYSTEM\n        \"\n      >\n        <label title=\"{{ 'Enforcement' | translate }}\">{{ 'Enforcement' | translate }}</label>\n        <div class=\"form-control-static\">\n          <label\n            title=\"{{ 'Enforce two-factor authentication on all users' | translate }}\"\n            class=\"c8y-switch\"\n          >\n            <input type=\"checkbox\" name=\"tenantTfaEnforced\" [(ngModel)]=\"tenantTfaEnforced\" />\n            <span></span>\n            <span>{{ 'Enforce two-factor authentication on all users' | translate }}</span>\n          </label>\n        </div>\n      </div>\n\n      <div class=\"row\" *ngIf=\"tenantTfaStrategy === tfaStrategyEnum.SMS\">\n        <div class=\"col-sm-6\">\n          <c8y-form-group>\n            <label title=\"{{ 'Token validity limit' | translate }}\"\n              >{{ 'Token validity limit' | translate }}\n              <button\n                class=\"btn btn-clean\"\n                popover=\"{{ TFA_IS_ENFORCED_BY_SYSTEM_POPOVER | translate }}\"\n                placement=\"right\"\n                [outsideClick]=\"true\"\n                *ngIf=\"!systemTfaTenantScopeSettingEnabled\"\n              >\n                <i [c8yIcon]=\"'question-circle-o'\" class=\"text-info\"></i>\n              </button>\n              <button\n                class=\"btn btn-clean\"\n                popover=\"{{ TOKEN_VALIDITY_DETERMINED_BY_JWT_POPOVER | translate }}\"\n                placement=\"right\"\n                [outsideClick]=\"true\"\n                *ngIf=\"\n                  systemTfaTenantScopeSettingEnabled &&\n                  preferredLoginOptionType === tenantLoginOptionTypeEnum.OAUTH2_INTERNAL\n                \"\n              >\n                <i [c8yIcon]=\"'question-circle-o'\" class=\"text-info\"></i>\n              </button>\n            </label>\n            <div class=\"input-group\">\n              <input\n                type=\"number\"\n                class=\"form-control text-right\"\n                name=\"tenantTfaTokenValidity\"\n                [(ngModel)]=\"tenantTfaTokenValidity\"\n                [disabled]=\"\n                  !systemTfaTenantScopeSettingEnabled ||\n                  preferredLoginOptionType === tenantLoginOptionTypeEnum.OAUTH2_INTERNAL\n                \"\n                [required]=\"systemTfaTenantScopeSettingEnabled\"\n                [max]=\"999999\"\n                [min]=\"0\"\n              />\n              <span class=\"input-group-addon\" translate>minutes</span>\n            </div>\n          </c8y-form-group>\n        </div>\n        <div class=\"col-sm-6\">\n          <c8y-form-group>\n            <label title=\"{{ 'Verification code validity limit' | translate }}\">\n              {{ 'Verification code validity limit' | translate }}\n              <button\n                class=\"btn btn-clean\"\n                popover=\"{{ TFA_IS_ENFORCED_BY_SYSTEM_POPOVER | translate }}\"\n                [outsideClick]=\"true\"\n                placement=\"right\"\n                *ngIf=\"!systemTfaTenantScopeSettingEnabled\"\n              >\n                <i [c8yIcon]=\"'question-circle-o'\" class=\"text-info\"></i>\n              </button>\n            </label>\n            <div class=\"input-group\">\n              <input\n                type=\"number\"\n                class=\"form-control text-right\"\n                name=\"tenantTfaPinValidity\"\n                [(ngModel)]=\"tenantTfaPinValidity\"\n                [disabled]=\"!systemTfaTenantScopeSettingEnabled\"\n                [required]=\"systemTfaTenantScopeSettingEnabled\"\n                [max]=\"999999\"\n                [min]=\"0\"\n              />\n              <span class=\"input-group-addon\" translate>minutes</span>\n            </div>\n          </c8y-form-group>\n        </div>\n      </div>\n    </fieldset>\n    <div\n      *ngIf=\"\n        preferredLoginOptionType !== tenantLoginOptionTypeEnum.OAUTH2_INTERNAL &&\n        !smsGatewayAvailable\n      \"\n    >\n      <div class=\"alert alert-warning\">\n        <strong>{{ 'None of TFA strategy can be set.' | translate }}</strong><br />\n        {{ SMS_APP_NOT_SUBSCRIBED_POPOVER | translate }}<br />\n        {{ TOTP_REQUIRES_OAUTH_POPOVER | translate }}\n      </div>\n    </div>\n  </div>\n</div>\n",
                viewProviders: [{ provide: ControlContainer, useExisting: NgForm }]
            },] }
];
TfaSettingsComponent.propDecorators = {
    authConfiguration: [{ type: Input }]
};
//# sourceMappingURL=data:application/json;base64, 