/**  
 * @since Last modified: 2024-09-17 00:31:05
 * @class LineElem
 * @description The custom element : ax-line .
 * @see {@link https://codepen.io/axui/pen/VwNXxNe|demo @ codepen}
 * @example
 * <ax-line></ax-line>;
 */
import { T_el, T_obj } from '../../../types/ax';
import ax from '../ax';
import alias from '../global/alias';
import createEl from '../utils/createEl';
import elState from '../utils/elState';
import getAttrBool from '../utils/getAttrBool';
import tplToEl from '../utils/tplToEl';
import PlainElem from './PlainElem';
class LineElem extends PlainElem {
    labelEl: any;
    startEl: any;
    endEl: any;
    contRaw: any;
    constructor() {
        super();
        super.createObs();
        this.getRawData();
        this.fillWrap(this.propsProxy);
    }
    static lazyAttrs = ['stor-name'];
    static custAttrs = [
        'class',
        'type', 'theme', 'size', 'align',
        'on-init', 'on-reset',
    ];
    static boolAttrs = ['v'];
    static get observedAttributes() {
        return ['label', ...this.custAttrs, ...this.boolAttrs];
    }
    attributeChangedCallback(name: string, oldVal: string, newVal: string) {
        this.propsProxy[name] = LineElem.boolAttrs.includes(name) ? getAttrBool(newVal) : newVal;
        if (name === 'label') {
            this.labelEl.innerHTML = newVal;
            if (newVal) {
                (elState(this.labelEl).isVirtual) ? this.wrapEl.append(this.labelEl, this.endEl) : null;
            } else {
                this.labelEl.remove();
                this.endEl.remove();
            }
        }
    }
    connectedCallback() {
        this.render();
        this.initialized = true;
        this.listen({ name: 'init' });
    }
    getRawData() {
        [...LineElem.custAttrs, ...LineElem.lazyAttrs].forEach((k: string) => {
            this.propsRaw[k] = this.getAttribute(k);
        });
        LineElem.boolAttrs.forEach((k: string) => {
            this.propsRaw[k] = getAttrBool(this.getAttribute(k));
        });
        this.propsRaw.label = this.textContent?.trim() || this.getAttribute('label');
        for (let k in this.propsRaw) {
            this.propsProxy[k] = this.propsRaw[k]
        }
        this.innerHTML = '';
    }
    fillWrap(data: T_obj) {
        this.wrapEl = createEl('div', { [alias]: 'wrap' }, this.labelEl);
        this.startEl = createEl('i', { [alias]: 'start' });
        this.endEl = createEl('i', { [alias]: 'end' });
        this.labelEl = createEl('i', { [alias]: 'label' }, data.label);
        this.wrapEl.appendChild(this.startEl);
        data.label ? this.wrapEl.append(this.labelEl, this.endEl) : null;
    }
    render(data?: T_obj) {
        this.appendChild(this.wrapEl);
    }
}
window.customElements.define("ax-line", LineElem);
export default LineElem;
