'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const core = require('./core-c57a47e8.js');

const Autocomplete = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        /**
        * Apply low density on the element
        */
        this.dense = false;
        this.showSuggestions = false;
        this.selectedIndex = -1;
        this.change = core.createEvent(this, "change", 7);
    }
    componentDidLoad() {
        this.watchValue();
        this.textElement.addEventListener('keydown', event => this.navigateSuggestions(event));
    }
    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
            };
        }
    }
    navigateSuggestions(event) {
        if (this.suggestions && this.suggestions.size > 0) {
            let newIndex;
            const suggestionKeys = Array.from(this.suggestions.keys());
            switch (event.key) {
                case 'ArrowDown':
                    newIndex = this.selectedIndex !== null ? this.selectedIndex + 1 : 0;
                    if (newIndex >= this.suggestions.size) {
                        newIndex = 0;
                    }
                    this.selectedIndex = newIndex;
                    break;
                case 'ArrowUp':
                    newIndex = this.selectedIndex !== null ? this.selectedIndex - 1 : this.suggestions.size - 1;
                    if (newIndex < 0) {
                        newIndex = this.suggestions.size - 1;
                    }
                    this.selectedIndex = newIndex;
                    break;
                case 'Enter':
                    // Select first value with enter if none selected
                    if (this.selectedIndex === null) {
                        this.selectedIndex = 0;
                    }
                    this.selectSuggestion(suggestionKeys[this.selectedIndex]);
                    this.change.emit(this.value);
                    this.clearSuggestions();
                    return;
                default:
                    return;
            }
            event.preventDefault();
        }
    }
    clearSuggestions() {
        this.selectedIndex = -1;
        this.suggestions = null;
    }
    selectSuggestion(key) {
        this.showSuggestions = false;
        if (this.suggestions) {
            const newValue = {
                value: key,
                label: this.suggestions.get(key)
            };
            this.value = Object.assign({}, newValue);
        }
    }
    execAutocomplete(event) {
        this.value.label = event.target.value;
        this.autocomplete(event.target.value).then((suggests) => this.suggestions = suggests);
        this.showSuggestions = this.suggestions && this.suggestions.size > 0;
    }
    handleChange() {
        if (!this.textElement.value) {
            this.value = null;
            this.change.emit(this.value);
        }
        else {
            this.change.emit(this.value);
        }
    }
    render() {
        return (core.h(core.Host, { style: { 'position': 'relative' }, onBlur: () => {
                this.change.emit(this.value);
                this.showSuggestions = false;
            } }, core.h("materials-text-field", { ref: el => this.textElement = el, "trailing-icon": this.trailingIcon, dense: this.dense, label: this.label, focused: !!this.value.label, value: this.value.label, onInput: (ev) => this.execAutocomplete(ev), onChange: (ev) => {
                ev.stopPropagation();
                ev.preventDefault();
                this.handleChange();
            } }), this.showSuggestions &&
            core.h("materials-list", { style: { 'position': 'absolute', 'top': (this.textElement.getBoundingClientRect().bottom - this.textElement.getBoundingClientRect().top) + 'px' } }, Array.from(this.suggestions.keys()).map((key, index) => index < this.maxSuggestions ?
                core.h("materials-list-item", { selected: index === this.selectedIndex, onClick: () => {
                        this.selectSuggestion(key);
                        this.change.emit(this.value);
                        this.clearSuggestions();
                    }, label: this.suggestions.get(key) }) : null))));
    }
    static get watchers() { return {
        "value": ["watchValue"]
    }; }
    static get style() { return ":host{display:block}materials-list{z-index:1;background-color:#fff;width:100%;overflow-y:visible;-webkit-box-shadow:0 2px 4px 0 #aaa;box-shadow:0 2px 4px 0 #aaa}"; }
};

exports.materials_autocomplete = Autocomplete;
