import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { EventEmitter, Component, Self, Optional, Output, Input, HostBinding, ContentChild, 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 { coerceBooleanProperty } from '@angular/cdk/coercion';

/*
 *  @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 DejaChipsComponent {
    constructor(control) {
        this.control = control;
        // eslint-disable-next-line @angular-eslint/no-output-native
        this.close = new EventEmitter();
        this._disabled = null;
        this._readonly = false;
        this.onChangeCallback = (_a) => undefined;
        this.onTouchedCallback = () => undefined;
        if (this.control) {
            this.control.valueAccessor = this;
        }
    }
    /** Lecture seule */
    set readonly(value) {
        this._readonly = coerceBooleanProperty(value) || null;
    }
    get readonly() {
        return this._readonly;
    }
    /** Retourne ou definit si le selecteur est desactivé. */
    set disabled(value) {
        this._disabled = coerceBooleanProperty(value) || null;
    }
    get disabled() {
        return this._disabled;
    }
    set items(value) {
        this.writeValue(value);
    }
    get items() {
        return this._items;
    }
    get itemTemplate() {
        return this.itemTemplateExternal || this.itemTemplateInternal;
    }
    get insertTemplate() {
        return this.insertTemplateExternal || this.insertTemplateInternal;
    }
    // ************* ControlValueAccessor Implementation **************
    // set accessor including call the onchange callback
    set value(value) {
        this.writeValue(value);
        this.onChangeCallback(value);
    }
    // get accessor
    get value() {
        return this._items;
    }
    // From ControlValueAccessor interface
    writeValue(value) {
        this._items = value;
    }
    // From ControlValueAccessor interface
    registerOnChange(fn) {
        this.onChangeCallback = fn;
    }
    // From ControlValueAccessor interface
    registerOnTouched(fn) {
        this.onTouchedCallback = fn;
    }
    setDisabledState(isDisabled) {
        this.disabled = isDisabled;
    }
    // ************* End of ControlValueAccessor Implementation **************
    // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
    getTextValue(value) {
        if (!value) {
            return '';
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        }
        else if (this.textField && value.model && value.model[this.textField] !== undefined) {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
            return value.model[this.textField];
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        }
        else if (this.textField && value[this.textField] !== undefined) {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
            return value[this.textField];
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        }
        else if (value.displayName) {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
            return typeof value.displayName === 'string' ? value.displayName : value.displayName();
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        }
        else if (typeof value.toString === 'function') {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
            return value.toString();
        }
        else {
            return '';
        }
    }
    onClose(closeEvent, item, index) {
        const newEvent = new CustomEvent('DejaChipsCloseEvent', {});
        newEvent.item = item;
        newEvent.index = index;
        this.items.splice(index, 1);
        this.onChangeCallback(this.items);
        this.close.emit(newEvent);
        closeEvent.stopPropagation();
        return false;
    }
}
DejaChipsComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaChipsComponent, deps: [{ token: i1.NgControl, optional: true, self: true }], target: i0.ɵɵFactoryTarget.Component });
DejaChipsComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.1.3", type: DejaChipsComponent, selector: "deja-chips", inputs: { _items: "_items", textField: "textField", itemTemplateExternal: "itemTemplateExternal", insertTemplateExternal: "insertTemplateExternal", readonly: "readonly", disabled: "disabled", items: "items" }, outputs: { close: "close" }, host: { properties: { "attr.disabled": "this._disabled" } }, queries: [{ propertyName: "itemTemplateInternal", first: true, predicate: ["itemTemplate"], descendants: true }, { propertyName: "insertTemplateInternal", first: true, predicate: ["insertTemplate"], descendants: true }], ngImport: i0, template: "<span class=\"chips-item\" *ngFor=\"let item of items; let index = index\">\n    <span *ngIf=\"itemTemplate;else caption\">\n        <ng-template [ngTemplateOutlet]=\"itemTemplate\" [ngTemplateOutletContext]=\"{ $implicit: item, index: index }\"></ng-template>\n    </span>\n    <ng-template #caption>\n        <span>{{ getTextValue(item) }}</span>\n    </ng-template>\n    <mat-icon id=\"close-button\" *ngIf=\"!readonly && !disabled\" (click)=\"onClose($event, item, index)\">close</mat-icon>\n</span>\n<span class=\"insert-item\" *ngIf=\"insertTemplate\">\n    <ng-template [ngTemplateOutlet]=\"insertTemplate\" [ngTemplateOutletContext]=\"{ }\"></ng-template>\n</span>\n", styles: [":host{margin:0;padding:0;overflow:hidden;display:flex;flex-wrap:wrap;align-items:center}:host[disabled]>span.chips-item>span{font-style:italic}:host>span.insert-item{flex:1 1 auto}:host>span.chips-item{margin:.1rem;padding:.38rem;flex:0 0 auto;border-radius:.88rem;display:flex;align-items:center;text-decoration:none;transition:.3s;cursor:default}:host>span.chips-item #close-button{zoom:72%;z-index:3;cursor:pointer}\n"], dependencies: [{ kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i2.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "component", type: i3.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaChipsComponent, decorators: [{
            type: Component,
            args: [{ selector: 'deja-chips', template: "<span class=\"chips-item\" *ngFor=\"let item of items; let index = index\">\n    <span *ngIf=\"itemTemplate;else caption\">\n        <ng-template [ngTemplateOutlet]=\"itemTemplate\" [ngTemplateOutletContext]=\"{ $implicit: item, index: index }\"></ng-template>\n    </span>\n    <ng-template #caption>\n        <span>{{ getTextValue(item) }}</span>\n    </ng-template>\n    <mat-icon id=\"close-button\" *ngIf=\"!readonly && !disabled\" (click)=\"onClose($event, item, index)\">close</mat-icon>\n</span>\n<span class=\"insert-item\" *ngIf=\"insertTemplate\">\n    <ng-template [ngTemplateOutlet]=\"insertTemplate\" [ngTemplateOutletContext]=\"{ }\"></ng-template>\n</span>\n", styles: [":host{margin:0;padding:0;overflow:hidden;display:flex;flex-wrap:wrap;align-items:center}:host[disabled]>span.chips-item>span{font-style:italic}:host>span.insert-item{flex:1 1 auto}:host>span.chips-item{margin:.1rem;padding:.38rem;flex:0 0 auto;border-radius:.88rem;display:flex;align-items:center;text-decoration:none;transition:.3s;cursor:default}:host>span.chips-item #close-button{zoom:72%;z-index:3;cursor:pointer}\n"] }]
        }], ctorParameters: function () {
        return [{ type: i1.NgControl, decorators: [{
                        type: Self
                    }, {
                        type: Optional
                    }] }];
    }, propDecorators: { close: [{
                type: Output
            }], _items: [{
                type: Input
            }], textField: [{
                type: Input
            }], itemTemplateExternal: [{
                type: Input
            }], insertTemplateExternal: [{
                type: Input
            }], _disabled: [{
                type: HostBinding,
                args: ['attr.disabled']
            }], itemTemplateInternal: [{
                type: ContentChild,
                args: ['itemTemplate']
            }], insertTemplateInternal: [{
                type: ContentChild,
                args: ['insertTemplate']
            }], readonly: [{
                type: Input
            }], disabled: [{
                type: Input
            }], items: [{
                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 DejaChipsModule {
}
DejaChipsModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaChipsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
DejaChipsModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.3", ngImport: i0, type: DejaChipsModule, declarations: [DejaChipsComponent], imports: [CommonModule,
        FormsModule,
        MatIconModule], exports: [DejaChipsComponent] });
DejaChipsModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaChipsModule, imports: [CommonModule,
        FormsModule,
        MatIconModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.3", ngImport: i0, type: DejaChipsModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [DejaChipsComponent],
                    exports: [DejaChipsComponent],
                    imports: [
                        CommonModule,
                        FormsModule,
                        MatIconModule
                    ]
                }]
        }] });

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

export { DejaChipsComponent, DejaChipsModule };
//# sourceMappingURL=deja-js-component-chips.mjs.map
