/**  
 * @since Last modified: 2024-09-17 00:27:32
 * @class AlarmElem
 * @description The custom element : ax-alarm .
 * @see {@link https://codepen.io/axui/pen/PogdRPp|demo @ codepen}
 * @example
 * <ax-alarm label="click">256</ax-alarm>;
 */
import { T_obj } from '../../../types/ax';
import ax from '../ax';
import config from '../global/config';
import prefix from '../global/prefix';
import alias from '../global/alias';
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 AlarmElem extends PlainElem {
    wrapEl: any;
    labelEl: any;
    iconEl: any;
    imageEl:any;
    constructor() {
        super();
        this.createObs();
        this.getRawData();
        this.fillWrap(this.propsProxy);
    }
    static lazyAttrs = ['stor-name'];
    static custAttrs = [
        'class',
        'icon','image', 'theme', 'size',
        'on-init', 'on-reset', 'on-set',
    ];
    static boolAttrs:string[] = [];
    static get observedAttributes() {
        return ['label', ...this.custAttrs, ...this.boolAttrs];
    }
    attributeChangedCallback(name: string, oldVal: string, newVal: string) {
        this.propsProxy[name] = AlarmElem.boolAttrs.includes(name) ? getAttrBool(newVal) : newVal;
         if (name === 'label') {
            if (newVal === null) {
                this.labelEl.remove();
            } else {
                this.labelEl.innerHTML = newVal;
                if (elState(this.labelEl).isVirtual) {
                    this.wrapEl.appendChild(this.labelEl);
                    this.iconEl.remove();
                    this.imageEl.remove();
                }
            }
        } else if (name === 'icon') {
            if (newVal === null) {
                this.iconEl.remove();
            } else {
                this.iconEl.setAttribute('class', newVal);
                if (elState(this.iconEl).isVirtual) {
                    this.wrapEl.appendChild(this.iconEl);
                    this.labelEl.remove();
                    this.imageEl.remove();
                }
            }
        }else if (name === 'image') {
            if (newVal === null) {
                this.imageEl.remove();
            } else {
                this.imageEl.src= newVal;
                if (elState(this.imageEl).isVirtual) {
                    this.wrapEl.appendChild(this.imageEl);
                    this.labelEl.remove();
                    this.iconEl.remove();
                }
            }
        }
    }
    connectedCallback() {
        this.render();
        this.initialized = true;
        this.listen({ name: 'init' });
    }
    getRawData() {
        [...AlarmElem.custAttrs, ...AlarmElem.lazyAttrs].forEach((k: string) => {
            this.propsRaw[k] = this.getAttribute(k);
        });
        AlarmElem.boolAttrs.forEach((k: string) => {
            this.propsRaw[k] = getAttrBool(this.getAttribute(k));
        });
        this.propsRaw.label = this.innerHTML || this.getAttribute('label');
        for (let k in this.propsRaw) {
            this.propsProxy[k] = this.propsRaw[k]
        }
        this.innerHTML = '';
    }
    fillWrap(data: T_obj) {
        this.wrapEl = createEl('span', { [alias]: 'wrap' });
        this.labelEl = createEl('i', { [alias]: 'label' }, data.label || '');
        this.iconEl = createEl('i', { class: data.icon || '', [alias]: 'icon' });
        this.imageEl = createEl('img', { src: data.image || '', [alias]: 'image' });
        this.wrapEl.appendChild(this.labelEl);
    }
    render(data?: T_obj) {
        this.append(this.wrapEl);
    }
}
window.customElements.define("ax-alarm", AlarmElem);
export default AlarmElem;
