/* ===================== AUTOGENERATED SOURCE IMPORT ===================== */

import Template from "/emcJS/util/html/template/HTMLTemplate.js";

const TPL = new Template(`
    <emc-i18n-label id="label"></emc-i18n-label>
    
`);

import GlobalStyle from "/emcJS/util/html/template/CSSTemplate.js";

const STYLE = new GlobalStyle(`
    :host {
        display: flex;
        align-items: center;
        width: 100%;
        min-height: calc(1em + 16px);
        padding: 8px;
        color: var(--button-text-color, #000000);
        user-select: none;
        white-space: normal;
        cursor: pointer;
    }
    
    :host::after {
        display: inline-flex;
        justify-content: center;
        align-items: center;
        appearance: none;
        flex-grow: 0;
        width: 1em;
        height: 1em;
        padding: 2px;
        margin: 0px;
        color: var(--datagrid-select-cell-text-color, #000000);
        border-style: solid;
        border-width: 2px;
        border-color: var(--datagrid-select-cell-border-color, #000000);
        border-radius: 0.3em;
        font-size: 1em;
        line-height: 1em;
        font-family: monospace;
        font-weight: bold;
        content: " ";
    }
    
    :host([selected]:not([selected="false"]))::after {
        content: "✓";
    }
    
    #label {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: flex-start;
        width: 100%;
        height: 100%;
        padding: 0px 12px 0px 0px;
        overflow: hidden;
        white-space: pre;
        text-overflow: ellipsis;
    }
    
`);

/* ======================================================================= */

import CustomElement from "../../../element/CustomElement.js";
import "../../../i18n/I18nLabel.js";

export default class SelectEntry extends CustomElement {

    #labelEl;

    constructor() {
        super();
        this.shadowRoot.append(TPL.generate());
        STYLE.apply(this.shadowRoot);
        /* --- */
        this.#labelEl = this.shadowRoot.getElementById("label");
    }

    set value(value) {
        this.setAttribute("value", value);
    }

    get value() {
        return this.getAttribute("value");
    }

    set label(value) {
        this.setAttribute("label", value);
    }

    get label() {
        return this.getAttribute("label");
    }

    set selected(value) {
        this.setBooleanAttribute("selected", value);
    }

    get selected() {
        return this.getBooleanAttribute("selected");
    }

    static get observedAttributes() {
        return ["label"];
    }

    attributeChangedCallback(name, oldValue, newValue) {
        switch (name) {
            case "label": {
                if (oldValue != newValue) {
                    this.#labelEl.i18nValue = newValue;
                }
            } break;
        }
    }

    get comparatorText() {
        return this.#labelEl.innerText;
    }

    static create(value, label) {
        const el = new SelectEntry();
        el.value = value;
        el.label = label ?? value;
        return el;
    }

}

customElements.define("emc-select-entry", SelectEntry);
