import { __decorate, __metadata } from "tslib";
import { Component, Input } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { CITIES } from '../../../../../const/city.const';
import { API, DEF } from '../../../../../api';
let IdentityUserUsersUpdateComponent = class IdentityUserUsersUpdateComponent {
    constructor(nzModalRef) {
        this.nzModalRef = nzModalRef;
        this.cities = CITIES;
        this.formGroup = new FormGroup({
            userName: new FormControl({ value: '', disabled: true }),
            name: new FormControl('', {
                validators: Validators.required,
            }),
            cities: new FormControl([]),
            street: new FormControl(''),
            zipCode: new FormControl('', {
                validators: [Validators.pattern(/^[0-9]{5,6}$/)],
            }),
            phoneNumber: new FormControl(''),
            cardNumber: new FormControl(''),
            cardHolderName: new FormControl(''),
            expiration: new FormControl('', {
                validators: [Validators.pattern(/^[0-9]{2}\/[0-9]{2}$/)],
            }),
            securityNumber: new FormControl('', {
                validators: [Validators.pattern(/^[0-9]{3}$/)],
            }),
        });
        this.saving = false;
    }
    handleCancel() {
        this.nzModalRef.destroy();
    }
    handleOk() {
        if (this.formGroup.invalid) {
            return;
        }
        this.saving = true;
        const applicationUser = new DEF.Identity.ApplicationUser(this.formGroup.getRawValue());
        //region country / state / city
        {
            const formControl = this.formGroup.get('cities');
            if (formControl) {
                const citiesValue = formControl.value;
                if (citiesValue.length === 3) {
                    applicationUser.country = citiesValue[0];
                    applicationUser.state = citiesValue[1];
                    applicationUser.city = citiesValue[2];
                }
                else if (citiesValue.length === 2) {
                    applicationUser.country = citiesValue[0];
                    applicationUser.city = citiesValue[1];
                }
            }
        }
        //endregion
        //region delete redundant fields
        {
            delete applicationUser.id;
            delete applicationUser.accessFailedCount;
            delete applicationUser.concurrencyStamp;
            delete applicationUser.emailConfirmed;
            delete applicationUser.lockoutEnabled;
            delete applicationUser.lockoutEnd;
            delete applicationUser.normalizedEmail;
            delete applicationUser.normalizedUserName;
            delete applicationUser.permissions;
            delete applicationUser.passwordHash;
            delete applicationUser.phoneNumberConfirmed;
            delete applicationUser.securityStamp;
            delete applicationUser.twoFactorEnabled;
            delete applicationUser.firstName;
            delete applicationUser.lastName;
            delete applicationUser.email;
            delete applicationUser.initialPassword;
        }
        //endregion
        API.Identity.UserController.UpdateUser(undefined, applicationUser).subscribe(() => {
            this.formGroup.reset();
            this.saving = false;
            this.nzModalRef.close(Object.assign(Object.assign({}, this.record), applicationUser));
        }, () => {
            this.saving = false;
        });
    }
    ngOnInit() {
        this.formGroup.patchValue(Object.assign(Object.assign({}, this.record), { cities: !this.record.state && this.record.city ? [this.record.country, this.record.city] : [this.record.country, this.record.state, this.record.city] }));
    }
};
IdentityUserUsersUpdateComponent.ctorParameters = () => [
    { type: NzModalRef }
];
__decorate([
    Input(),
    __metadata("design:type", DEF.Identity.ApplicationUser)
], IdentityUserUsersUpdateComponent.prototype, "record", void 0);
IdentityUserUsersUpdateComponent = __decorate([
    Component({
        selector: 'kg---identity--user--users--update',
        template: "<div class=\"modal-header\">\n  <div class=\"modal-title\">\n    {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.update-user' | i18n }}\n  </div>\n</div>\n\n<form (ngSubmit)=\"handleOk()\" [formGroup]=\"formGroup\" [nzNoAnimation]=\"true\" nz-form nzLayout=\"vertical\">\n  <div [nzGutter]=\"12\" nz-row>\n    <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'userName'\"> {{ 'app__user-name' | i18n }}: </nz-form-label>\n        <nz-form-control>\n          <input [attr.id]=\"'userName'\" [formControlName]=\"'userName'\" nz-input />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'name'\" [nzRequired]=\"true\"> {{ 'app__name' | i18n }}: </nz-form-label>\n        <nz-form-control nzErrorTip=\"{{ 'app__form.name.required' | i18n }}\">\n          <input [attr.id]=\"'name'\" [formControlName]=\"'name'\" nz-input />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"24\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'country'\">\n          {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.country-state-city' | i18n }}:\n        </nz-form-label>\n        <nz-form-control>\n          <nz-cascader\n            [nzOptions]=\"cities\"\n            [nzShowSearch]=\"true\"\n            formControlName=\"cities\"\n            nzPlaceHolder=\"{{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.country-state-city' | i18n }}\"\n          >\n          </nz-cascader>\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"24\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'street'\"> {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.street' | i18n }}: </nz-form-label>\n        <nz-form-control>\n          <input [attr.id]=\"'street'\" [formControlName]=\"'street'\" nz-input />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'zipCode'\"> {{ 'app__zip-code' | i18n }}: </nz-form-label>\n        <nz-form-control nzErrorTip=\"{{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.invalid-zip-code' | i18n }}\">\n          <input [attr.id]=\"'zipCode'\" [formControlName]=\"'zipCode'\" nz-input />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'phoneNumber'\"> {{ 'app__phone-number' | i18n }}: </nz-form-label>\n        <nz-form-control>\n          <input [attr.id]=\"'phoneNumber'\" [formControlName]=\"'phoneNumber'\" nz-input />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'cardNumber'\">\n          {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.card-number' | i18n }}:\n        </nz-form-label>\n        <nz-form-control>\n          <input [attr.id]=\"'cardNumber'\" [formControlName]=\"'cardNumber'\" nz-input />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'cardHolderName'\">\n          {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.card-holder-name' | i18n }}:\n        </nz-form-label>\n        <nz-form-control>\n          <input [attr.id]=\"'cardHolderName'\" [formControlName]=\"'cardHolderName'\" nz-input />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'expiration'\">\n          {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.expiration' | i18n }}:\n        </nz-form-label>\n        <nz-form-control nzErrorTip=\"{{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.invalid-expiration' | i18n }}\">\n          <input [attr.id]=\"'expiration'\" [formControlName]=\"'expiration'\" nz-input placeholder=\"MM/YY\" />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n\n    <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\n      <nz-form-item>\n        <nz-form-label [nzFor]=\"'securityNumber'\">\n          {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.security-number' | i18n }}:\n        </nz-form-label>\n        <nz-form-control nzErrorTip=\"{{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.invalid-security-number' | i18n }}\">\n          <input [attr.id]=\"'securityNumber'\" [formControlName]=\"'securityNumber'\" nz-input placeholder=\"XXX\" />\n        </nz-form-control>\n      </nz-form-item>\n    </div>\n  </div>\n\n  <button [hidden]=\"true\"></button>\n</form>\n\n<div class=\"modal-footer\">\n  <button (click)=\"handleCancel()\" [disabled]=\"saving\" [nzType]=\"'default'\" nz-button>\n    {{ 'app__cancel' | i18n }}\n  </button>\n\n  <button (click)=\"handleOk()\" [disabled]=\"!formGroup.valid || saving\" [nzLoading]=\"saving\" [nzType]=\"'primary'\" nz-button>\n    {{ 'app__save' | i18n }}\n  </button>\n</div>\n"
    }),
    __metadata("design:paramtypes", [NzModalRef])
], IdentityUserUsersUpdateComponent);
export { IdentityUserUsersUpdateComponent };
//# sourceMappingURL=data:application/json;base64, 