import createEl from '../utils/createEl';
import elState from '../utils/elState';
import getAttrBool from '../utils/getAttrBool';
import PlainElem from './PlainElem';
class AlarmElem extends PlainElem {
    constructor() {
        super();
        this.createObs();
        this.getRawData();
        this.fillWrap(this.propsProxy);
    }
    static get observedAttributes() {
        return ['label', ...this.custAttrs, ...this.boolAttrs];
    }
    attributeChangedCallback(name, oldVal, newVal) {
        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) => {
            this.propsRaw[k] = this.getAttribute(k);
        });
        AlarmElem.boolAttrs.forEach((k) => {
            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) {
        this.wrapEl = createEl('span', { role: 'wrap' });
        this.labelEl = createEl('i', { role: 'label' }, data.label || '');
        this.iconEl = createEl('i', { class: data.icon || '', role: 'icon' });
        this.imageEl = createEl('img', { src: data.image || '', role: 'image' });
        this.wrapEl.appendChild(this.labelEl);
    }
    render(data) {
        this.append(this.wrapEl);
    }
}
AlarmElem.lazyAttrs = ['stor-name'];
AlarmElem.custAttrs = [
    'class',
    'icon', 'image', 'theme', 'size',
    'on-init', 'on-reset', 'on-set',
];
AlarmElem.boolAttrs = [];
window.customElements.define("ax-alarm", AlarmElem);
export default AlarmElem;
