import { __decorate, __metadata } from "tslib";
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { CITIES } from '../../../../../const/city.const';
import { API, DEF } from '../../../../../api';
var IdentityUserUsersCreateComponent = /** @class */ (function () {
    function IdentityUserUsersCreateComponent() {
        this.cities = CITIES;
        this.formGroup = new FormGroup({
            userName: new FormControl('', {
                validators: Validators.required,
            }),
            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}$/)],
            }),
            email: new FormControl('', {
                validators: [Validators.required, Validators.email],
            }),
            initialPassword: new FormControl('', {
                validators: Validators.required,
            }),
        });
        this.initialPasswordVisible = false;
        this.ok = new EventEmitter();
        this.saving = false;
        this.visible = false;
        this.visibleChange = new EventEmitter();
    }
    IdentityUserUsersCreateComponent.prototype.handleCancel = function () {
        this.formGroup.reset({
            cities: [],
        });
        this.visibleChange.emit(false);
    };
    IdentityUserUsersCreateComponent.prototype.handleOk = function () {
        var _this = this;
        if (this.formGroup.invalid) {
            return;
        }
        this.saving = true;
        var applicationUser = new DEF.Identity.ApplicationUser(this.formGroup.getRawValue());
        //region country / state / city
        {
            var formControl = this.formGroup.get('cities');
            if (formControl) {
                var 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;
        }
        //endregion
        API.Identity.UserController.AddUser(undefined, applicationUser).subscribe(function () {
            _this.formGroup.reset({
                cities: [],
            });
            _this.saving = false;
            _this.visibleChange.emit(false);
            _this.ok.emit();
        }, function () {
            _this.saving = false;
        });
    };
    __decorate([
        Output(),
        __metadata("design:type", EventEmitter)
    ], IdentityUserUsersCreateComponent.prototype, "ok", void 0);
    __decorate([
        Input(),
        __metadata("design:type", Boolean)
    ], IdentityUserUsersCreateComponent.prototype, "visible", void 0);
    __decorate([
        Output(),
        __metadata("design:type", EventEmitter)
    ], IdentityUserUsersCreateComponent.prototype, "visibleChange", void 0);
    IdentityUserUsersCreateComponent = __decorate([
        Component({
            selector: 'kg---identity--user--users--create',
            template: "<nz-modal\n  (nzOnCancel)=\"handleCancel()\"\n  (nzOnOk)=\"handleOk()\"\n  [nzCancelDisabled]=\"saving\"\n  [nzClosable]=\"!saving\"\n  [nzKeyboard]=\"false\"\n  [nzMaskClosable]=\"false\"\n  [nzOkDisabled]=\"!formGroup.valid || saving\"\n  [nzOkLoading]=\"saving\"\n  [nzVisible]=\"visible\"\n  nzTitle=\"{{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.add-user' | i18n }}\"\n  nzWidth=\"748px\"\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'\" [nzRequired]=\"true\"> {{ 'app__user-name' | i18n }}: </nz-form-label>\n          <nz-form-control nzErrorTip=\"{{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.username-is-required' | i18n }}\">\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]=\"12\" [nzSm]=\"24\" nz-col>\n        <nz-form-item>\n          <nz-form-label [nzFor]=\"'email'\" [nzRequired]=\"true\"> {{ 'app__email' | i18n }}: </nz-form-label>\n          <nz-form-control [nzErrorTip]=\"emailErrorTipTpl\">\n            <ng-template #emailErrorTipTpl let-control>\n              <ng-container *ngIf=\"control.hasError('email')\">\n                {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.invalid-email' | i18n }}\n              </ng-container>\n              <ng-container *ngIf=\"control.hasError('required')\">\n                {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.email-is-required' | i18n }}\n              </ng-container>\n            </ng-template>\n\n            <input [attr.id]=\"'email'\" [formControlName]=\"'email'\" 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]=\"'initialPassword'\" [nzRequired]=\"true\">\n            {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.initial-password' | i18n }}:\n          </nz-form-label>\n          <nz-form-control nzErrorTip=\"{{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.initial-password-is-required' | i18n }}\">\n            <nz-input-group [nzSuffix]=\"nzSuffixTpl\">\n              <input [attr.id]=\"'initialPassword'\" [formControlName]=\"'initialPassword'\" [type]=\"initialPasswordVisible ? 'text' : 'password'\" nz-input />\n            </nz-input-group>\n\n            <ng-template #nzSuffixTpl>\n              <i\n                (click)=\"initialPasswordVisible = !initialPasswordVisible\"\n                [ngStyle]=\"{\n                  cursor: 'pointer'\n                }\"\n                [nzType]=\"initialPasswordVisible ? 'eye-invisible' : 'eye'\"\n                nz-icon\n              ></i>\n            </ng-template>\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</nz-modal>\n"
        })
    ], IdentityUserUsersCreateComponent);
    return IdentityUserUsersCreateComponent;
}());
export { IdentityUserUsersCreateComponent };
//# sourceMappingURL=data:application/json;base64, 