'use strict';

var index = require('./index-C_OnxXhP.js');
var keycodes = require('./keycodes-BCQoLEdB.js');

const textEditorPickerCss = () => `:host(limebb-text-editor-picker){animation:fade-in 0.3s ease forwards;animation-delay:0.1s;display:block;max-width:calc(100vw - 2rem);padding:0.125rem;border-radius:0.5rem;background-color:rgba(var(--contrast-100));box-shadow:var(--shadow-depth-16);max-height:50vh;overflow-y:auto}limel-menu-list{position:relative}limel-spinner{margin:0 auto}.spinner-in-list{position:absolute;top:50%;left:50%;transform:translate(-50%, -50%)}@keyframes fade-in{0%{opacity:0}100%{opacity:1}}`;

const TextEditorPicker = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.cancel = index.createEvent(this, "cancel");
        this.itemSelected = index.createEvent(this, "itemSelected");
        /**
         * List of items to display
         */
        this.items = [];
        /**
         * Open state  of the item's list
         */
        this.open = false;
        /**
         * Loading state of the item's list
         */
        this.isSearching = false;
        this.emptyMessage = '';
        this.handleCancel = (event) => {
            if (event instanceof KeyboardEvent && event.key !== keycodes.ESCAPE) {
                return;
            }
            event.stopPropagation();
            event.preventDefault();
            this.cancel.emit();
        };
        this.selectOnInteract = (event) => {
            event.stopPropagation();
            event.preventDefault();
            this.itemSelected.emit(event.detail);
        };
    }
    watchOpen(newValue) {
        if (newValue === true) {
            this.setupHandlers();
        }
        else if (newValue === false) {
            this.teardownHandlers();
        }
    }
    connectedCallback() {
        if (this.open) {
            this.setupHandlers();
        }
    }
    disconnectedCallback() {
        this.teardownHandlers();
    }
    setupHandlers() {
        this.host.addEventListener('keyup', this.handleCancel);
    }
    teardownHandlers() {
        if (this.host) {
            this.host.removeEventListener('keyup', this.handleCancel);
        }
    }
    render() {
        if (this.items.length === 0) {
            if (this.isSearching) {
                return this.renderSpinner();
            }
            return (index.h(index.Host, { onMouseDown: this.handleMouseDown }, index.h("limebb-empty-state", { value: this.emptyMessage, icon: {
                    name: 'search_more',
                    color: 'rgb(var(--contrast-800))',
                } })));
        }
        return (index.h(index.Host, { onMouseDown: this.handleMouseDown }, [
            index.h("div", null, index.h("limel-list", { type: "selectable", items: this.getItems(), onInteract: this.selectOnInteract }), index.h("div", { class: "spinner-in-list" }, this.renderSpinner())),
        ]));
    }
    renderSpinner() {
        if (this.isSearching) {
            return index.h("limel-spinner", { size: "small", class: "search-spinner" });
        }
    }
    handleMouseDown(event) {
        event.preventDefault();
    }
    getItems() {
        return this.items.map((item) => {
            if ('separator' in item) {
                return item;
            }
            return Object.assign(Object.assign({}, item), { disabled: item.disabled || this.isSearching });
        });
    }
    get host() { return index.getElement(this); }
    static get watchers() { return {
        "open": [{
                "watchOpen": 0
            }]
    }; }
};
TextEditorPicker.style = textEditorPickerCss();

exports.limebb_text_editor_picker = TextEditorPicker;
