import { Component, Input, ViewChild } from '@angular/core';
import { FormBuilder, Validators, } from '@angular/forms';
import { ModalController } from '@ionic/angular';
import { CurrentDeviceService, getFormFieldOrError, TaskManagerComponent, TaskManagerService, } from '@iotize/ionic';
import * as i0 from "@angular/core";
import * as i1 from "@iotize/ionic";
import * as i2 from "@ionic/angular";
import * as i3 from "@angular/forms";
import * as i4 from "@angular/common";
const TASK_ID = 'change-user-password';
export class ChangePasswordComponent {
    constructor(currentDevice, modalController, formBuilder, taskManager) {
        this.currentDevice = currentDevice;
        this.modalController = modalController;
        this.formBuilder = formBuilder;
        this.taskManager = taskManager;
        this.confirmPassword = false;
        this.askCurrentPassword = false;
        this.header = true;
        this.userId = undefined;
        this.loading = false;
        this.form = this.formBuilder.group({
            password: ['', Validators.required],
            confirmPassword: [''],
            currentPassword: [''],
        });
        this.changePasswordTask = this.taskManager.createTask({
            id: TASK_ID,
            info: {
                title: 'Change user password',
                feedback: 'User password changed!',
            },
            exec: () => {
                return this.changePassword();
            },
        });
    }
    get tap() {
        return this.currentDevice.tap;
    }
    get password() {
        return getFormFieldOrError(this.form, 'password');
    }
    get currentPassword() {
        return getFormFieldOrError(this.form, 'currentPassword');
    }
    submit() {
        this.error = undefined;
        this.changePasswordTaskComponent.execTask();
    }
    async changePassword() {
        try {
            const newPassword = this.password.value;
            if (this.askCurrentPassword) {
                await this.tap.auth.login({
                    username: this.tap.auth.sessionStateSnapshot.name,
                    password: this.currentPassword.value,
                });
            }
            this.promise$ = this.tap.auth.changePassword(newPassword, this.userId);
            await this.promise$;
            await this.modalController.dismiss();
        }
        catch (err) {
            this.showError(err);
        }
    }
    async dismiss() {
        await this.modalController.dismiss();
    }
    showError(err) {
        this.error = err;
    }
}
/** @nocollapse */ /** @nocollapse */ ChangePasswordComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.0", ngImport: i0, type: ChangePasswordComponent, deps: [{ token: i1.CurrentDeviceService }, { token: i2.ModalController }, { token: i3.FormBuilder }, { token: i1.TaskManagerService }], target: i0.ɵɵFactoryTarget.Component });
/** @nocollapse */ /** @nocollapse */ ChangePasswordComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.3.0", type: ChangePasswordComponent, selector: "tap-change-password", inputs: { confirmPassword: "confirmPassword", askCurrentPassword: "askCurrentPassword", header: "header", userId: "userId" }, viewQueries: [{ propertyName: "changePasswordTaskComponent", first: true, predicate: ["changePasswordTaskComponent"], descendants: true, static: true }], ngImport: i0, template: "<ion-header *ngIf=\"header\">\n  <ion-toolbar color=\"primary\">\n    <ion-buttons slot=\"start\">\n      <ion-button (click)=\"modalController.dismiss()\">\n        <ion-icon name=\"arrow-back\"></ion-icon>\n      </ion-button>\n    </ion-buttons>\n    <ion-title class=\"text-center\"> Change profile password </ion-title>\n  </ion-toolbar>\n</ion-header>\n<ion-content>\n  <ion-grid>\n    <ion-row>\n      <ion-col class=\"ion-align-self-center\">\n        <iotize-feedback-error [error]=\"error\"></iotize-feedback-error>\n        <form\n          (ngSubmit)=\"submit()\"\n          [formGroup]=\"form\"\n          (keydown.enter)=\"submit()\"\n        >\n          <ion-item *ngIf=\"askCurrentPassword\">\n            <ion-input\n              [formControlName]=\"'currentPassword'\"\n              placeholder=\"Current password\"\n              required\n              type=\"password\"\n            ></ion-input>\n          </ion-item>\n          <ion-item>\n            <ion-input\n              [formControlName]=\"'password'\"\n              placeholder=\"New password\"\n              required\n              type=\"password\"\n            ></ion-input>\n          </ion-item>\n          <ion-item *ngIf=\"confirmPassword\">\n            <ion-input\n              [formControlName]=\"'confirmPassword'\"\n              placeholder=\"Confirm new password\"\n              required\n              type=\"password\"\n            ></ion-input>\n          </ion-item>\n          <app-task-manager\n            #changePasswordTaskComponent\n            [task]=\"changePasswordTask\"\n          >\n            <app-task-manager-action>\n              <ion-item lines=\"none\">\n                <ion-button\n                  size=\"default\"\n                  slot=\"end\"\n                  [disabled]=\"form['invalid']\"\n                  expand=\"block\"\n                  type=\"submit\"\n                >\n                  <ion-icon name=\"create\"></ion-icon>\n                  Change password\n                </ion-button>\n              </ion-item>\n            </app-task-manager-action>\n            <app-task-manager-delayed>\n              <ion-item lines=\"none\">\n                <ion-text class=\"ion-text-wrap\" slot=\"end\" color=\"warning\">\n                  <ion-icon name=\"alert\"></ion-icon>\n                  Please reconnect Tap to change password\n                </ion-text>\n                <ion-button\n                  size=\"default\"\n                  color=\"warning\"\n                  app-task-manager-action-cancel\n                >\n                  <ion-icon name=\"close\"></ion-icon>\n                  Cancel\n                </ion-button>\n              </ion-item>\n            </app-task-manager-delayed>\n            <app-task-manager-running>\n              <ion-item lines=\"none\">\n                <ion-progress-bar type=\"indeterminate\"></ion-progress-bar>\n              </ion-item>\n            </app-task-manager-running>\n          </app-task-manager>\n        </form>\n      </ion-col>\n    </ion-row>\n  </ion-grid>\n</ion-content>\n", styles: [""], components: [{ type: i2.IonHeader, selector: "ion-header", inputs: ["collapse", "mode", "translucent"] }, { type: i2.IonToolbar, selector: "ion-toolbar", inputs: ["color", "mode"] }, { type: i2.IonButtons, selector: "ion-buttons", inputs: ["collapse"] }, { type: i2.IonButton, selector: "ion-button", inputs: ["buttonType", "color", "disabled", "download", "expand", "fill", "href", "mode", "rel", "routerAnimation", "routerDirection", "shape", "size", "strong", "target", "type"] }, { type: i2.IonIcon, selector: "ion-icon", inputs: ["color", "flipRtl", "icon", "ios", "lazy", "md", "mode", "name", "sanitize", "size", "src"] }, { type: i2.IonTitle, selector: "ion-title", inputs: ["color", "size"] }, { type: i2.IonContent, selector: "ion-content", inputs: ["color", "forceOverscroll", "fullscreen", "scrollEvents", "scrollX", "scrollY"] }, { type: i2.IonGrid, selector: "ion-grid", inputs: ["fixed"] }, { type: i2.IonRow, selector: "ion-row" }, { type: i2.IonCol, selector: "ion-col", inputs: ["offset", "offsetLg", "offsetMd", "offsetSm", "offsetXl", "offsetXs", "pull", "pullLg", "pullMd", "pullSm", "pullXl", "pullXs", "push", "pushLg", "pushMd", "pushSm", "pushXl", "pushXs", "size", "sizeLg", "sizeMd", "sizeSm", "sizeXl", "sizeXs"] }, { type: i1.ErrorFeedbackComponent, selector: "iotize-feedback-error", inputs: ["error", "icon"] }, { type: i2.IonItem, selector: "ion-item", inputs: ["button", "color", "counter", "detail", "detailIcon", "disabled", "download", "fill", "href", "lines", "mode", "rel", "routerAnimation", "routerDirection", "shape", "target", "type"] }, { type: i2.IonInput, selector: "ion-input", inputs: ["accept", "autocapitalize", "autocomplete", "autocorrect", "autofocus", "clearInput", "clearOnEdit", "color", "debounce", "disabled", "enterkeyhint", "inputmode", "max", "maxlength", "min", "minlength", "mode", "multiple", "name", "pattern", "placeholder", "readonly", "required", "size", "spellcheck", "step", "type", "value"] }, { type: i1.TaskManagerComponent, selector: "app-task-manager", inputs: ["task"] }, { type: i1.TaskManagerActionComponent, selector: "app-task-manager-action", inputs: ["context"] }, { type: i1.TaskManagerDelayedComponent, selector: "app-task-manager-delayed", inputs: ["context"] }, { type: i2.IonText, selector: "ion-text", inputs: ["color", "mode"] }, { type: i1.TaskManagerRunningComponent, selector: "app-task-manager-running", inputs: ["context"] }, { type: i2.IonProgressBar, selector: "ion-progress-bar", inputs: ["buffer", "color", "mode", "reversed", "type", "value"] }], directives: [{ type: i4.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i3.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { type: i3.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { type: i3.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { type: i2.TextValueAccessor, selector: "ion-input:not([type=number]),ion-textarea,ion-searchbar" }, { type: i3.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { type: i3.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { type: i3.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }, { type: i1.TaskManagerActionCancelDirective, selector: "[app-task-manager-action-cancel]", outputs: ["onClick"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.0", ngImport: i0, type: ChangePasswordComponent, decorators: [{
            type: Component,
            args: [{ selector: 'tap-change-password', template: "<ion-header *ngIf=\"header\">\n  <ion-toolbar color=\"primary\">\n    <ion-buttons slot=\"start\">\n      <ion-button (click)=\"modalController.dismiss()\">\n        <ion-icon name=\"arrow-back\"></ion-icon>\n      </ion-button>\n    </ion-buttons>\n    <ion-title class=\"text-center\"> Change profile password </ion-title>\n  </ion-toolbar>\n</ion-header>\n<ion-content>\n  <ion-grid>\n    <ion-row>\n      <ion-col class=\"ion-align-self-center\">\n        <iotize-feedback-error [error]=\"error\"></iotize-feedback-error>\n        <form\n          (ngSubmit)=\"submit()\"\n          [formGroup]=\"form\"\n          (keydown.enter)=\"submit()\"\n        >\n          <ion-item *ngIf=\"askCurrentPassword\">\n            <ion-input\n              [formControlName]=\"'currentPassword'\"\n              placeholder=\"Current password\"\n              required\n              type=\"password\"\n            ></ion-input>\n          </ion-item>\n          <ion-item>\n            <ion-input\n              [formControlName]=\"'password'\"\n              placeholder=\"New password\"\n              required\n              type=\"password\"\n            ></ion-input>\n          </ion-item>\n          <ion-item *ngIf=\"confirmPassword\">\n            <ion-input\n              [formControlName]=\"'confirmPassword'\"\n              placeholder=\"Confirm new password\"\n              required\n              type=\"password\"\n            ></ion-input>\n          </ion-item>\n          <app-task-manager\n            #changePasswordTaskComponent\n            [task]=\"changePasswordTask\"\n          >\n            <app-task-manager-action>\n              <ion-item lines=\"none\">\n                <ion-button\n                  size=\"default\"\n                  slot=\"end\"\n                  [disabled]=\"form['invalid']\"\n                  expand=\"block\"\n                  type=\"submit\"\n                >\n                  <ion-icon name=\"create\"></ion-icon>\n                  Change password\n                </ion-button>\n              </ion-item>\n            </app-task-manager-action>\n            <app-task-manager-delayed>\n              <ion-item lines=\"none\">\n                <ion-text class=\"ion-text-wrap\" slot=\"end\" color=\"warning\">\n                  <ion-icon name=\"alert\"></ion-icon>\n                  Please reconnect Tap to change password\n                </ion-text>\n                <ion-button\n                  size=\"default\"\n                  color=\"warning\"\n                  app-task-manager-action-cancel\n                >\n                  <ion-icon name=\"close\"></ion-icon>\n                  Cancel\n                </ion-button>\n              </ion-item>\n            </app-task-manager-delayed>\n            <app-task-manager-running>\n              <ion-item lines=\"none\">\n                <ion-progress-bar type=\"indeterminate\"></ion-progress-bar>\n              </ion-item>\n            </app-task-manager-running>\n          </app-task-manager>\n        </form>\n      </ion-col>\n    </ion-row>\n  </ion-grid>\n</ion-content>\n", styles: [""] }]
        }], ctorParameters: function () { return [{ type: i1.CurrentDeviceService }, { type: i2.ModalController }, { type: i3.FormBuilder }, { type: i1.TaskManagerService }]; }, propDecorators: { changePasswordTaskComponent: [{
                type: ViewChild,
                args: ['changePasswordTaskComponent', { static: true }]
            }], confirmPassword: [{
                type: Input
            }], askCurrentPassword: [{
                type: Input
            }], header: [{
                type: Input
            }], userId: [{
                type: Input
            }] } });
//# sourceMappingURL=data:application/json;base64, 