import { Component, Output, EventEmitter, ViewChild } from '@angular/core';
import { UserService, FetchClient, Client } from '@c8y/client';
import { AppStateService } from '../common/ui-state.service';
import { AlertService } from '../alert/alert.service';
import { ModalComponent } from '../modal/modal.component';
import { LoginService } from '../login/login.service';
import { clone } from 'lodash-es';
import { gettext } from '../i18n/gettext';
import * as i0 from "@angular/core";
import * as i1 from "@c8y/client";
import * as i2 from "../common/ui-state.service";
import * as i3 from "../alert/alert.service";
import * as i4 from "../login/login.service";
import * as i5 from "@angular/forms";
import * as i6 from "../forms/form-group.component";
import * as i7 from "../forms/message.directive";
import * as i8 from "../forms/messages.component";
import * as i9 from "../forms/required-input-placeholder.directive";
import * as i10 from "../i18n/c8y-translate.directive";
import * as i11 from "../modal/modal.component";
import * as i12 from "../i18n/c8y-translate.pipe";
export class PasswordConfirmModalComponent {
    constructor(user, ui, client, alert, loginService) {
        this.user = user;
        this.ui = ui;
        this.client = client;
        this.alert = alert;
        this.loginService = loginService;
        this.passwordConfirmedEmitter = new EventEmitter();
        this.loading = false;
    }
    async passwordConfirm() {
        if (this.password) {
            const supportUserName = this.ui.currentSupportUserName.value;
            const userId = this.ui.currentUser.value.id;
            const credentials = {
                password: this.password,
                user: `${supportUserName ? `${supportUserName}$` : ''}${userId}`,
                tenant: this.client.tenant
            };
            try {
                this.loading = true;
                const strategy = clone(this.loginService.useBasicAuth({}));
                delete credentials.tfa;
                strategy.updateCredentials(credentials);
                const newClient = this.createNewClient(strategy, this.client.baseUrl);
                await newClient.user.current();
                this.emitSuccessAndClose();
            }
            catch (e) {
                if (e.res && e.res.status === 401 && e.data && /pin|totp/i.test(e.data.message)) {
                    this.emitSuccessAndClose();
                }
                else {
                    this.alert.danger(gettext("Provided password doesn't match your current one."));
                }
            }
            finally {
                this.loading = false;
            }
        }
    }
    cancel() {
        this.passwordConfirmedEmitter.emit(false);
        this.modal._dismiss();
    }
    createNewClient(strategy, baseUrl) {
        return new Client(strategy, baseUrl);
    }
    emitSuccessAndClose() {
        this.passwordConfirmedEmitter.emit(true);
        this.modal._dismiss();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: PasswordConfirmModalComponent, deps: [{ token: i1.UserService }, { token: i2.AppStateService }, { token: i1.FetchClient }, { token: i3.AlertService }, { token: i4.LoginService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: PasswordConfirmModalComponent, selector: "c8y-password-confirm-modal", outputs: { passwordConfirmedEmitter: "passwordConfirmedEmitter" }, viewQueries: [{ propertyName: "modal", first: true, predicate: ["modal"], descendants: true }], ngImport: i0, template: "<c8y-modal [customFooter]=\"true\" [title]=\"'Confirm your current password' | translate\" #modal>\r\n  <form #confirmForm=\"ngForm\" (ngSubmit)=\"confirmForm.form.valid && passwordConfirm()\">\r\n    <div class=\"d-block p-24 p-b-0\">\r\n      <c8y-form-group [hasWarning]=\"true\">\r\n        <label translate for=\"currentPassword\">Enter your password</label>\r\n        <input\r\n          id=\"currentPassword\"\r\n          [(ngModel)]=\"password\"\r\n          type=\"password\"\r\n          name=\"password\"\r\n          class=\"form-control\"\r\n          placeholder=\"{{ 'Enter your password' | translate }}\"\r\n          required\r\n        />\r\n        <c8y-messages>\r\n          <c8y-message translate>\r\n            Enter the password of the user that you are currently logged in with.\r\n          </c8y-message>\r\n        </c8y-messages>\r\n      </c8y-form-group>\r\n    </div>\r\n    <div class=\"modal-footer separator-top bg-level-0 sticky-bottom\">\r\n      <button\r\n        title=\"{{ 'Cancel' | translate }}\"\r\n        class=\"btn btn-default\"\r\n        type=\"button\"\r\n        (click)=\"cancel()\"\r\n      >\r\n        {{ 'Cancel' | translate }}\r\n      </button>\r\n      <button\r\n        title=\"{{ 'Confirm' | translate }}\"\r\n        class=\"btn btn-primary\"\r\n        type=\"submit\"\r\n        [disabled]=\"!confirmForm.form.valid || loading\"\r\n      >\r\n        {{ 'Confirm' | translate }}\r\n      </button>\r\n    </div>\r\n  </form>\r\n</c8y-modal>\r\n", dependencies: [{ kind: "directive", type: i5.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i5.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: i5.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i5.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i5.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i5.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "directive", type: i5.NgForm, selector: "form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]", inputs: ["ngFormOptions"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "component", type: i6.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i7.MessageDirective, selector: "c8y-message", inputs: ["name", "text"] }, { kind: "component", type: i8.MessagesComponent, selector: "c8y-messages", inputs: ["show", "defaults", "helpMessage"] }, { kind: "directive", type: i9.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { kind: "directive", type: i10.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "component", type: i11.ModalComponent, selector: "c8y-modal", inputs: ["disabled", "close", "dismiss", "title", "body", "customFooter", "headerClasses", "labels"], outputs: ["onDismiss", "onClose"] }, { kind: "pipe", type: i12.C8yTranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: PasswordConfirmModalComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-password-confirm-modal', template: "<c8y-modal [customFooter]=\"true\" [title]=\"'Confirm your current password' | translate\" #modal>\r\n  <form #confirmForm=\"ngForm\" (ngSubmit)=\"confirmForm.form.valid && passwordConfirm()\">\r\n    <div class=\"d-block p-24 p-b-0\">\r\n      <c8y-form-group [hasWarning]=\"true\">\r\n        <label translate for=\"currentPassword\">Enter your password</label>\r\n        <input\r\n          id=\"currentPassword\"\r\n          [(ngModel)]=\"password\"\r\n          type=\"password\"\r\n          name=\"password\"\r\n          class=\"form-control\"\r\n          placeholder=\"{{ 'Enter your password' | translate }}\"\r\n          required\r\n        />\r\n        <c8y-messages>\r\n          <c8y-message translate>\r\n            Enter the password of the user that you are currently logged in with.\r\n          </c8y-message>\r\n        </c8y-messages>\r\n      </c8y-form-group>\r\n    </div>\r\n    <div class=\"modal-footer separator-top bg-level-0 sticky-bottom\">\r\n      <button\r\n        title=\"{{ 'Cancel' | translate }}\"\r\n        class=\"btn btn-default\"\r\n        type=\"button\"\r\n        (click)=\"cancel()\"\r\n      >\r\n        {{ 'Cancel' | translate }}\r\n      </button>\r\n      <button\r\n        title=\"{{ 'Confirm' | translate }}\"\r\n        class=\"btn btn-primary\"\r\n        type=\"submit\"\r\n        [disabled]=\"!confirmForm.form.valid || loading\"\r\n      >\r\n        {{ 'Confirm' | translate }}\r\n      </button>\r\n    </div>\r\n  </form>\r\n</c8y-modal>\r\n" }]
        }], ctorParameters: () => [{ type: i1.UserService }, { type: i2.AppStateService }, { type: i1.FetchClient }, { type: i3.AlertService }, { type: i4.LoginService }], propDecorators: { passwordConfirmedEmitter: [{
                type: Output
            }], modal: [{
                type: ViewChild,
                args: ['modal', { static: false }]
            }] } });
//# sourceMappingURL=data:application/json;base64, 