/**
 * @license
 * Copyright ASW (A Software World) All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file
 */
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { AswConfirmDialog } from '@asoftwareworld/form-builder/form-control/confirm-dialog';
import { Constants, ObjectUtils } from '@asoftwareworld/form-builder/form-control/core';
import { AswNumberDialog } from './number-dialog';
import * as i0 from "@angular/core";
import * as i1 from "@angular/material/dialog";
import * as i2 from "@angular/material/form-field";
import * as i3 from "@angular/material/button";
import * as i4 from "@angular/material/icon";
import * as i5 from "@angular/common";
import * as i6 from "@angular/material/input";
import * as i7 from "@angular/forms";
import * as i8 from "@angular/material/tooltip";
export class AswNumber {
    constructor(dialog) {
        this.dialog = dialog;
        this.constants = Constants;
        this.objectUtils = ObjectUtils;
        /**
         * Number control
         */
        this.control = null;
        this.formControls = [];
        this.isPreviewTemplate = false;
        this.numberUpdateEvent = new EventEmitter();
        this.numberDeleteEvent = new EventEmitter();
        this.aswModelChange = new EventEmitter();
        this.duplicateControl = new EventEmitter();
    }
    ngOnInit() {
        // tslint:disable-next-line:no-bitwise
        const id = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        if (this.control) {
            this.control.guid = this.control.guid ? this.control.guid : id;
        }
    }
    deleteNumberDialog(control, controlIndex) {
        const dialogRef = this.dialog.open(AswConfirmDialog, {
            width: '350px',
            data: { name: control.controlType, message: this.constants.messages.waringMessage }
        });
        dialogRef.afterClosed().subscribe(result => {
            if (result !== undefined) {
                this.numberDeleteEvent.emit(controlIndex);
            }
        });
    }
    editNumberDialog(control, controlIndex) {
        const dialogRef = this.dialog.open(AswNumberDialog, {
            disableClose: true,
            width: '744px',
            data: control
        });
        dialogRef.afterClosed().subscribe(result => {
            if (result !== undefined) {
                this.numberUpdateEvent.emit({ control: result, index: controlIndex });
            }
        });
    }
    onChange(control) {
        const calculationControls = this.formControls.filter(x => x.controlType === 'calculation');
        calculationControls.forEach(calculation => {
            const updateControls = calculation.operations.filter((x) => x.id === control.guid);
            updateControls.forEach((y) => {
                y.label = control.label;
                y.value = control.value;
            });
            calculation.value = this.objectUtils.calculateValue(calculation.operations);
        });
        this.aswModelChange.emit(control);
    }
    duplicateNumberControl(control) {
        this.duplicateControl.emit(control);
    }
}
AswNumber.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.4", ngImport: i0, type: AswNumber, deps: [{ token: i1.MatDialog }], target: i0.ɵɵFactoryTarget.Component });
AswNumber.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.2.4", type: AswNumber, selector: "asw-number", inputs: { control: "control", formControls: "formControls", controlIndex: "controlIndex", isPreviewTemplate: "isPreviewTemplate" }, outputs: { numberUpdateEvent: "numberUpdateEvent", numberDeleteEvent: "numberDeleteEvent", aswModelChange: "aswModelChange", duplicateControl: "duplicateControl" }, ngImport: i0, template: "<ng-container *ngIf=\"control as control\">\r\n    <mat-form-field hintLabel=\"Max {{control.maxlength}} characters\" [appearance]=\"control.style\" class=\"asw-mat-form-field {{control.customClass}}\">\r\n        <mat-label>{{control.label}}</mat-label>\r\n        <input matInput type=\"number\"\r\n                        [id]=\"control.id\"\r\n                        [placeholder]=\"control.label\"\r\n                        [(ngModel)]=\"control.value\"                    \r\n                        #input=\"ngModel\"\r\n                        (change)=\"onChange(control)\"\r\n                        (keypress)=\"objectUtils.keyPressNumbersWithDecimal($event)\"\r\n                        [matTooltip]=\"control.tooltip\"\r\n                        [maxlength]=\"control.maxlength\"\r\n                        [minlength]=\"control.minlength\"\r\n                        [required]=\"control.isRequired\">\r\n        <mat-hint align=\"end\">{{input.value?.length || 0}}/{{control.maxlength}}</mat-hint>\r\n        <mat-error class=\"asw-mat-error\" *ngIf=\"input.invalid && (input.dirty || input.touched)\">\r\n            <mat-error class=\"asw-mat-error\" *ngIf=\"input?.errors?.required\">\r\n                {{control.label}} is required\r\n            </mat-error>\r\n            <mat-error class=\"asw-mat-error\" *ngIf=\"input?.errors?.minlength\">\r\n                Sorry, {{control.label}} must be at least {{control.minlength}} characters long.\r\n            </mat-error>\r\n        </mat-error>\r\n    </mat-form-field>\r\n    <ng-template [ngIf]=\"isPreviewTemplate\">\r\n        <div class=\"row mb-2\">\r\n            <div class=\"col-md-12\">\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Duplicate\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"duplicateNumberControl(control)\">\r\n                    <mat-icon>content_copy</mat-icon>\r\n                </button>\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Edit\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"editNumberDialog(control, controlIndex)\">\r\n                    <mat-icon>edit</mat-icon>\r\n                </button>\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Delete\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"deleteNumberDialog(control, controlIndex)\">\r\n                    <mat-icon>delete</mat-icon>\r\n                </button>\r\n            </div>                                            \r\n        </div>\r\n    </ng-template>\r\n</ng-container>", components: [{ type: i2.MatFormField, selector: "mat-form-field", inputs: ["color", "appearance", "hideRequiredMarker", "hintLabel", "floatLabel"], exportAs: ["matFormField"] }, { type: i3.MatButton, selector: "button[mat-button], button[mat-raised-button], button[mat-icon-button],             button[mat-fab], button[mat-mini-fab], button[mat-stroked-button],             button[mat-flat-button]", inputs: ["disabled", "disableRipple", "color"], exportAs: ["matButton"] }, { type: i4.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }], directives: [{ type: i5.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i2.MatLabel, selector: "mat-label" }, { type: i6.MatInput, selector: "input[matInput], textarea[matInput], select[matNativeControl],      input[matNativeControl], textarea[matNativeControl]", inputs: ["disabled", "id", "placeholder", "name", "required", "type", "errorStateMatcher", "aria-describedby", "value", "readonly"], exportAs: ["matInput"] }, { type: i7.NumberValueAccessor, selector: "input[type=number][formControlName],input[type=number][formControl],input[type=number][ngModel]" }, { type: i7.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { type: i7.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { type: i7.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { type: i8.MatTooltip, selector: "[matTooltip]", exportAs: ["matTooltip"] }, { type: i7.MaxLengthValidator, selector: "[maxlength][formControlName],[maxlength][formControl],[maxlength][ngModel]", inputs: ["maxlength"] }, { type: i7.MinLengthValidator, selector: "[minlength][formControlName],[minlength][formControl],[minlength][ngModel]", inputs: ["minlength"] }, { type: i7.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { type: i2.MatHint, selector: "mat-hint", inputs: ["align", "id"] }, { type: i2.MatError, selector: "mat-error", inputs: ["id"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.4", ngImport: i0, type: AswNumber, decorators: [{
            type: Component,
            args: [{ selector: 'asw-number', template: "<ng-container *ngIf=\"control as control\">\r\n    <mat-form-field hintLabel=\"Max {{control.maxlength}} characters\" [appearance]=\"control.style\" class=\"asw-mat-form-field {{control.customClass}}\">\r\n        <mat-label>{{control.label}}</mat-label>\r\n        <input matInput type=\"number\"\r\n                        [id]=\"control.id\"\r\n                        [placeholder]=\"control.label\"\r\n                        [(ngModel)]=\"control.value\"                    \r\n                        #input=\"ngModel\"\r\n                        (change)=\"onChange(control)\"\r\n                        (keypress)=\"objectUtils.keyPressNumbersWithDecimal($event)\"\r\n                        [matTooltip]=\"control.tooltip\"\r\n                        [maxlength]=\"control.maxlength\"\r\n                        [minlength]=\"control.minlength\"\r\n                        [required]=\"control.isRequired\">\r\n        <mat-hint align=\"end\">{{input.value?.length || 0}}/{{control.maxlength}}</mat-hint>\r\n        <mat-error class=\"asw-mat-error\" *ngIf=\"input.invalid && (input.dirty || input.touched)\">\r\n            <mat-error class=\"asw-mat-error\" *ngIf=\"input?.errors?.required\">\r\n                {{control.label}} is required\r\n            </mat-error>\r\n            <mat-error class=\"asw-mat-error\" *ngIf=\"input?.errors?.minlength\">\r\n                Sorry, {{control.label}} must be at least {{control.minlength}} characters long.\r\n            </mat-error>\r\n        </mat-error>\r\n    </mat-form-field>\r\n    <ng-template [ngIf]=\"isPreviewTemplate\">\r\n        <div class=\"row mb-2\">\r\n            <div class=\"col-md-12\">\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Duplicate\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"duplicateNumberControl(control)\">\r\n                    <mat-icon>content_copy</mat-icon>\r\n                </button>\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Edit\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"editNumberDialog(control, controlIndex)\">\r\n                    <mat-icon>edit</mat-icon>\r\n                </button>\r\n                <button mat-icon-button \r\n                        type=\"button\" \r\n                        matTooltip=\"Delete\" \r\n                        [matTooltipPosition]=\"'below'\" \r\n                        (click)=\"deleteNumberDialog(control, controlIndex)\">\r\n                    <mat-icon>delete</mat-icon>\r\n                </button>\r\n            </div>                                            \r\n        </div>\r\n    </ng-template>\r\n</ng-container>" }]
        }], ctorParameters: function () { return [{ type: i1.MatDialog }]; }, propDecorators: { control: [{
                type: Input
            }], formControls: [{
                type: Input
            }], controlIndex: [{
                type: Input
            }], isPreviewTemplate: [{
                type: Input
            }], numberUpdateEvent: [{
                type: Output
            }], numberDeleteEvent: [{
                type: Output
            }], aswModelChange: [{
                type: Output
            }], duplicateControl: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 