import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { Component, Self, Optional, HostBinding, Input, NgModule } from '@angular/core';
import * as i1 from '@angular/forms';
import { FormsModule } from '@angular/forms';
import * as i3 from '@angular/material/icon';
import { MatIconModule } from '@angular/material/icon';
import * as i5 from '@angular/material/input';
import { MatInputModule } from '@angular/material/input';
import * as i2 from '@deja-js/component/chips';
import { DejaChipsModule } from '@deja-js/component/chips';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { KeyCodes } from '@deja-js/component/core';
import * as i4 from '@angular/material/form-field';

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
/**
 * Deja Tag Component for Angular
 *
 * This component allow you to manage element into an array of string
 */
class DejaTagComponent {
    constructor(control) {
        this.control = control;
        this._disabled = null;
        /**
         * Current value into the input
         */
        this.text = '';
        /**
         * Current value of the array of string
         */
        this.items = [];
        // NgModel implementation
        this.onTouchedCallback = () => undefined;
        this.onChangeCallback = (_a) => undefined;
        this.onValidatorChangeCallback = (_a) => undefined;
        if (this.control) {
            this.control.valueAccessor = this;
        }
    }
    /** Allow to disabled the component */
    set disabled(value) {
        const disabled = coerceBooleanProperty(value);
        this._disabled = disabled || null;
    }
    /**
     * Get disable value
     */
    get disabled() {
        return this.control ? this.control.disabled : this._disabled;
    }
    // ************* ControlValueAccessor Implementation **************
    get value() {
        return this.items;
    }
    set value(val) {
        this.writeValue(val);
        this.onChangeCallback(val);
        this.onTouchedCallback();
    }
    writeValue(value) {
        this.items = value ? value : [];
    }
    registerOnChange(fn) {
        this.onChangeCallback = fn;
    }
    registerOnTouched(fn) {
        this.onTouchedCallback = fn;
    }
    setDisabledState(isDisabled) {
        this.disabled = isDisabled;
    }
    // ************* End of ControlValueAccessor Implementation **************
    /**
     * Trigerred when user press key into the component
     */
    onKeyDown(e) {
        if (e.code === KeyCodes.Enter) {
            const target = e.target;
            this.onAddItem(target.value);
        }
    }
    /**
     * Add item into the the list
     * @param val : Value of the text to add
     */
    onAddItem(val) {
        if (val) {
            this.items.push(val);
            this.value = this.items;
            this.text = '';
        }
    }
    onRemoveItem() {
        this.onChangeCallback(this.value);
    }
}
DejaTagComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTagComponent, deps: [{ token: i1.NgControl, optional: true, self: true }], target: i0.ɵɵFactoryTarget.Component });
DejaTagComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.1.3", type: DejaTagComponent, selector: "deja-tag", inputs: { placeholder: "placeholder", disabled: "disabled" }, host: { properties: { "attr.disabled": "this._disabled" } }, ngImport: i0, template: "<deja-chips [items]=\"items\" [disabled]=\"disabled\" (close)=\"onRemoveItem()\"></deja-chips>\n<mat-form-field>\n    <input matInput [(ngModel)]=\"text\" matInput (keydown)=\"onKeyDown($event)\" [placeholder]=\"placeholder\" [disabled]=\"disabled\" autocomplete=\"off\">\n    <div (click)=\"onAddItem(text)\" matSuffix><mat-icon class=\"matSuffix\">add</mat-icon></div>\n</mat-form-field>", styles: [":host .mat-form-field{display:block;width:100%}:host .mat-form-field i:hover{cursor:pointer}\n"], dependencies: [{ kind: "component", type: i2.DejaChipsComponent, selector: "deja-chips", inputs: ["_items", "textField", "itemTemplateExternal", "insertTemplateExternal", "readonly", "disabled", "items"], outputs: ["close"] }, { kind: "directive", type: i1.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: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "component", type: i3.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }, { kind: "component", type: i4.MatFormField, selector: "mat-form-field", inputs: ["color", "appearance", "hideRequiredMarker", "hintLabel", "floatLabel"], exportAs: ["matFormField"] }, { kind: "directive", type: i4.MatSuffix, selector: "[matSuffix]" }, { kind: "directive", type: i5.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"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTagComponent, decorators: [{
            type: Component,
            args: [{ selector: 'deja-tag', template: "<deja-chips [items]=\"items\" [disabled]=\"disabled\" (close)=\"onRemoveItem()\"></deja-chips>\n<mat-form-field>\n    <input matInput [(ngModel)]=\"text\" matInput (keydown)=\"onKeyDown($event)\" [placeholder]=\"placeholder\" [disabled]=\"disabled\" autocomplete=\"off\">\n    <div (click)=\"onAddItem(text)\" matSuffix><mat-icon class=\"matSuffix\">add</mat-icon></div>\n</mat-form-field>", styles: [":host .mat-form-field{display:block;width:100%}:host .mat-form-field i:hover{cursor:pointer}\n"] }]
        }], ctorParameters: function () { return [{ type: i1.NgControl, decorators: [{
                    type: Self
                }, {
                    type: Optional
                }] }]; }, propDecorators: { _disabled: [{
                type: HostBinding,
                args: ['attr.disabled']
            }], placeholder: [{
                type: Input
            }], disabled: [{
                type: Input
            }] } });

/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
class DejaTagModule {
}
DejaTagModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTagModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
DejaTagModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.3", ngImport: i0, type: DejaTagModule, declarations: [DejaTagComponent], imports: [CommonModule,
        DejaChipsModule,
        FormsModule,
        MatIconModule,
        MatInputModule], exports: [DejaTagComponent] });
DejaTagModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTagModule, imports: [CommonModule,
        DejaChipsModule,
        FormsModule,
        MatIconModule,
        MatInputModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaTagModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [DejaTagComponent],
                    exports: [DejaTagComponent],
                    imports: [
                        CommonModule,
                        DejaChipsModule,
                        FormsModule,
                        MatIconModule,
                        MatInputModule
                    ],
                    providers: []
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { DejaTagComponent, DejaTagModule };
//# sourceMappingURL=deja-js-component-tag.mjs.map
