import { h } from '@stencil/core';
/**
* Class that represent an autocomplete element
* composed by a textfield
* and a menu with selectable items for autocompletion
*/
export class Autocomplete {
    constructor() {
        /**
        * Apply low density on the element
        */
        this.dense = false;
    }
    componentDidLoad() {
        this.watchValue();
    }
    componentWillLoad() {
        this.watchValue();
    }
    watchValue() {
        if (this.value) {
            this.value.label = this.value.label ? this.value.label : this.value.value;
        }
        else {
            this.value = {
                value: null,
                label: null
            };
        }
    }
    selectSuggestion(key) {
        this.menuElement.close();
        if (this.suggestions) {
            const newValue = {
                value: key,
                label: this.suggestions.get(key)
            };
            this.value = Object.assign({}, newValue);
        }
        this.change.emit(this.value);
    }
    execAutocomplete(event) {
        this.value.label = event.target.value;
        this.autocomplete(event.target.value).then((suggests) => this.suggestions = suggests);
        this.menuElement.open();
    }
    emptyField() {
        if (!this.textElement.value) {
            this.value = null;
            this.change.emit(this.value);
        }
    }
    render() {
        return ([
            h("materials-text-field", { ref: el => this.textElement = el, dense: this.dense, label: this.label, value: this.value.label, onInput: (ev) => this.execAutocomplete(ev), onChange: (ev) => {
                    ev.stopPropagation();
                    ev.preventDefault();
                    this.emptyField();
                } }),
            h("materials-menu", { ref: el => this.menuElement = el }, this.suggestions ? Array.from(this.suggestions.keys()).map((key) => h("materials-list-item", { onClick: () => this.selectSuggestion(key) }, this.suggestions.get(key))) : null)
        ]);
    }
    static get is() { return "materials-autocomplete"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() { return {
        "$": ["autocomplete.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["autocomplete.css"]
    }; }
    static get properties() { return {
        "autocomplete": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "(search: string) => Promise<Map<string, string>>",
                "resolved": "(search: string) => Promise<Map<string, string>>",
                "references": {
                    "Promise": {
                        "location": "global"
                    },
                    "Map": {
                        "location": "global"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Function of autocompletion to pass to the element\ncalled during onInput of the text-field"
            }
        },
        "value": {
            "type": "unknown",
            "mutable": true,
            "complexType": {
                "original": "{label?: string, value: string}",
                "resolved": "{ label?: string; value: string; }",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Value of the autocomplete text-field\ncomposed by a label to be displayed in the text-field\nand a value that is a real value\nif no label given, label = value"
            }
        },
        "dense": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Apply low density on the element"
            },
            "attribute": "dense",
            "reflect": false,
            "defaultValue": "false"
        },
        "label": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Label of the autocomplete"
            },
            "attribute": "label",
            "reflect": false
        }
    }; }
    static get states() { return {
        "suggestions": {}
    }; }
    static get events() { return [{
            "method": "change",
            "name": "change",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Change event emitted when value is selected"
            },
            "complexType": {
                "original": "{label?: string, value: string}",
                "resolved": "{ label?: string; value: string; }",
                "references": {}
            }
        }]; }
    static get watchers() { return [{
            "propName": "value",
            "methodName": "watchValue"
        }]; }
}
