/**  
 * @since Last modified: 2024-07-11 09:30:49
 * @class BuoyElem
 * @description The custom element : ax-buoy .
 * @see {@link https://codepen.io/axui/pen/PogdRPp|demo @ codepen}
 * @example
 * <ax-buoy label="click">256</ax-buoy>;
 */
import { T_obj } from '../../../types/ax';
import ax from '../ax';
import config from '../global/config';
import prefix from '../global/prefix';
import Observe from '../modules/Observe';
import attrJoinVal from '../utils/attrJoinVal';
import attrValBool from '../utils/attrValBool';
import createEl from '../utils/createEl';
import elState from '../utils/elState';
import extend from '../utils/extend';
import getAttrBool from '../utils/getAttrBool';
import icons from '../utils/icons';
import tplToEl from '../utils/tplToEl';
import PlainElem from './PlainElem';
class BuoyElem extends PlainElem {
    wrapEl: any;
    headEl: any;
    labelEl: any;
    iconEl: any;
    arrowEl: any;
    valueEl: any;
    reset: any;
    constructor() {
        super();
        this.createObs();
        this.getRawData();
        this.fillWrap(this.propsProxy);
    }
    static lazyAttrs = ['stor-name'];
    static custAttrs = [
        'class',
        'icon', 'label', 'theme', 'size',
        'on-init', 'on-reset', 'on-set',
    ];
    static boolAttrs = ['reverse', 'arrow'];
    static get observedAttributes() {
        return ['value', ...this.custAttrs, ...this.boolAttrs];
    }
    attributeChangedCallback(name: string, oldVal: string, newVal: string) {
        this.propsProxy[name] = BuoyElem.boolAttrs.includes(name) ? getAttrBool(newVal) : newVal;
        if (name === 'value') {
            this.valueEl.innerHTML = newVal;
        } else if (name === 'label') {
            if (newVal === null) {
                this.labelEl.remove();
            } else {
                this.labelEl.innerHTML = newVal;
                if (elState(this.labelEl).isVirtual) {
                    if (!elState(this.iconEl).isVirtual) {
                        this.iconEl.insertAdjacentElement('afterEnd', this.labelEl);
                    } else {
                        this.headEl.insertAdjacentElement('afterBegin', this.labelEl);
                    }
                }
            }
        } else if (name === 'icon') {
            if (newVal === null) {
                this.iconEl.remove();
            } else {
                this.iconEl.setAttribute('class', newVal);
                if (elState(this.iconEl).isVirtual) {
                    this.headEl.insertAdjacentElement('afterBegin', this.iconEl);
                }
            }
        } else if (name === 'arrow') {
            if (!attrValBool(newVal)) {
                this.arrowEl.remove();
            } else {
                if (elState(this.arrowEl).isVirtual) {
                    this.headEl.appendChild(this.arrowEl);
                }
            }
        }
    }
    connectedCallback() {
        this.render();
        this.initialized = true;
        this.listen({ name: 'init' });
    }
    getRawData() {
        [...BuoyElem.custAttrs, ...BuoyElem.lazyAttrs].forEach((k: string) => {
            this.propsRaw[k] = this.getAttribute(k);
        });
        BuoyElem.boolAttrs.forEach((k: string) => {
            this.propsRaw[k] = getAttrBool(this.getAttribute(k));
        });
        this.propsRaw.value = this.textContent?.trim() || this.getAttribute('value');
        for (let k in this.propsRaw) {
            this.propsProxy[k] = this.propsRaw[k]
        }
        this.innerHTML = '';
    }
    fillWrap(data: T_obj) {
        this.wrapEl = createEl('span', { role: 'wrap' });
        this.headEl = createEl('span', { role: 'head' });
        this.labelEl = createEl('i', { role: 'label' }, data.label || '');
        this.iconEl = createEl('i', { class: data.icon || '', role: 'icon' });
        this.arrowEl = createEl('i', { class: `${prefix}icon-down`, role: 'arrow' });
        this.valueEl = createEl('i', { role: 'value' }, data.value || '');
        data.label ? this.headEl.appendChild(this.labelEl) : null;
        data.icon ? this.headEl.insertAdjacentElement('afterBegin', this.iconEl) : null;
        data.arrow ? this.headEl.insertAdjacentElement('beforeEnd', this.arrowEl) : null;
        this.wrapEl.append(this.headEl, this.valueEl);
    }
    render(data?: T_obj) {
        this.append(this.wrapEl);
    }
}
window.customElements.define("ax-buoy", BuoyElem);
export default BuoyElem;
