import createEl from '../utils/createEl';
import elState from '../utils/elState';
import getAttrBool from '../utils/getAttrBool';
import PlainElem from './PlainElem';
class GoodElem extends PlainElem {
    constructor() {
        super();
        this.createObs();
        this.getRawData();
        this.fillWrap(this.propsProxy);
        this.increase = (step = 1, cb) => {
            let value = parseInt(this.properties.value) + step, tips = this.getAttribute('tips');
            this.setAttribute('value', String(value));
            tips ? this.setAttribute('tips', tips) : null;
            this.setAttribute('done', '');
            cb && cb.call(this, value);
        };
    }
    static get observedAttributes() {
        return ['value', ...this.custAttrs, ...this.boolAttrs];
    }
    attributeChangedCallback(name, oldVal, newVal) {
        this.propsProxy[name] = GoodElem.boolAttrs.includes(name) ? getAttrBool(newVal) : newVal;
        if (name === 'value') {
            this.valueEl.innerHTML = newVal;
        }
        else if (name === 'label') {
            this.labelEl.innerHTML = newVal;
        }
        else if (name === 'tips') {
            this.tipsEl.innerHTML = newVal;
        }
        else if (name === 'icon') {
            if (newVal === null) {
                this.iconEl.remove();
            }
            else {
                this.iconEl.setAttribute('class', newVal);
                elState(this.iconEl).isVirtual && this.wrapEl.insertAdjacentElement('afterBegin', this.iconEl);
            }
        }
    }
    connectedCallback() {
        this.render();
        this.initialized = true;
        this.listen({ name: 'init' });
    }
    getRawData() {
        [...GoodElem.custAttrs, ...GoodElem.lazyAttrs].forEach((k) => {
            this.propsRaw[k] = this.getAttribute(k);
        });
        GoodElem.boolAttrs.forEach((k) => {
            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) {
        this.wrapEl = createEl('span', { role: 'wrap' });
        this.labelEl = createEl('i', { role: 'label' }, data.label || '');
        this.iconEl = createEl('i', { class: data.icon || '', role: 'icon' });
        this.valueEl = createEl('i', { role: 'value' }, data.value || '');
        this.tipsEl = createEl('i', { role: 'tips' }, data.tips || '');
        this.wrapEl.appendChild(this.valueEl);
    }
    render(data) {
        this.append(this.labelEl, this.wrapEl, this.tipsEl);
    }
}
GoodElem.lazyAttrs = ['stor-name'];
GoodElem.custAttrs = [
    'class',
    'icon', 'label', 'tips',
    'on-init', 'on-reset', 'on-set',
];
GoodElem.boolAttrs = ['done', 'disabled'];
window.customElements.define("ax-good", GoodElem);
export default GoodElem;
