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

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

const TPL = new Template(`
    <div id="container">
        <div id="name"></div>
        <div id="type"></div>
    </div>
`);

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

const STYLE = new GlobalStyle(`
    :host {
        display: flex;
        align-items: center;
        width: 100%;
        min-height: calc(2em + 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: "✓";
    }
    
    #container {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: flex-start;
        width: 100%;
        height: 100%;
        padding: 0px 12px 0px 0px;
    }
    
    #container #name {
        overflow: hidden;
        white-space: pre;
        text-overflow: ellipsis;
        font-size: 1em;
    }
    
    #container #type {
        overflow: hidden;
        white-space: pre;
        text-overflow: ellipsis;
        font-size: 0.7em;
    }
    
`);

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

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

export default class RelationSelectEntry extends CustomElement {

    #containerEl;

    #nameEl;

    #typeEl;

    constructor() {
        super();
        this.shadowRoot.append(TPL.generate());
        STYLE.apply(this.shadowRoot);
        /* --- */
        this.#containerEl = this.shadowRoot.getElementById("container");
        this.#nameEl = this.shadowRoot.getElementById("name");
        this.#typeEl = this.shadowRoot.getElementById("type");
    }

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

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

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

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

    get value() {
        return {
            "type": this.type ?? "",
            "name": this.name ?? ""
        };
    }

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

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

    static get observedAttributes() {
        return ["name", "type"];
    }

    attributeChangedCallback(name, oldValue, newValue) {
        switch (name) {
            case "name": {
                if (oldValue != newValue) {
                    this.#nameEl.innerText = newValue;
                    this.#containerEl.title = `${newValue || "---"}\n[${this.type || "---"}]`;
                }
            } break;
            case "type": {
                if (oldValue != newValue) {
                    this.#typeEl.innerText = newValue;
                    this.#containerEl.title = `${this.name || "---"}\n[${newValue || "---"}]`;
                }
            } break;
        }
    }

    get comparatorText() {
        if (!this.name || !this.type) {
            return "";
        }
        return `${this.name}\n[${this.type}]`;
    }

}

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