/**  
 * @since Last modified: 2024-09-17 00:28:08
 * @class CheckboxElem
 * @description The custom element : ax-checkbox .
 * @see {@link https://codepen.io/axui/pen/YzMVvWP|demo @ codepen}
 * @example
 * <ax-checkbox name="demo" value="book" label="Reading"></ax-checkbox>;
 */
import { T_el, T_obj } from '../../../types/ax';
import alias from '../global/alias';
import createEl from '../utils/createEl';
import getAttrBool from '../utils/getAttrBool';
import PlainElem from './PlainElem';
class CheckboxElem extends PlainElem {
    labelEl: any;
    inputEl: any;
    legendEl: any;
    check:any;
    constructor() {
        super();
        super.createObs();
        this.getRawData();
        this.fillWrap(this.propsProxy);
        this.reset = () => {
            for (let k in this.propsRaw) {
                (this.propsRaw[k] !== null && this.propsRaw[k] !== false) ? this.setAttribute(k, this.propsRaw[k]) : this.removeAttribute(k);
            }
            this.listen({ name: 'reset' });
        }
        this.clear = () => {
            this.setAttribute('check', '');
            this.listen({ name: 'clear' });
        }
        this.check = (val: string) => {
            this.setAttribute('check', val);
            this.listen({ name: 'check', params: [val] });
        }
        this.observer.ins.on('set', (data: T_obj) => {
            if(data.key ==='check'){
                this.listen({ name: 'change', params: [data.value] });
            }
            this.initialized && this.updateCache({ [data.key]: data.value });
        });
    }
    static lazyAttrs = ['stor-name'];
    static custAttrs = [
        'class',
        'size', 'value', 'type', 'name', 'check',
        'on-init', 'on-reset', 'on-clear', 'on-change', 'on-set'
    ];
    static boolAttrs = ['disabled', 'readonly'];
    static get observedAttributes() {
        return ['label', ...this.custAttrs, ...this.boolAttrs];
    }
    attributeChangedCallback(name: string, oldVal: string, newVal: string) {
        this.propsProxy[name] = CheckboxElem.boolAttrs.includes(name) ? getAttrBool(newVal) : newVal;
        if (['disabled', 'readonly', 'value', 'name'].includes(name)) {
            this.inputEl[name] = this.propsProxy[name];
            if (name = 'value') {
                (!newVal && this.propsProxy.label) && (this.propsProxy.value = this.propsProxy.label);
            }
        } else if (name === 'label') {
            this.labelEl.innerHTML = newVal;
            (newVal && !this.propsProxy.value) && (this.propsProxy.value = newVal);
        } else if (name === 'check') {
            this.inputEl.checked = (newVal === 'ed' ? true : false);
        }
    }
    connectedCallback() {
        this.render();
        this.initialized = true;
        this.listen({ name: 'init' });
    }
    disconnectedCallback() {
    }
    adoptedCallback() {
    }
    getRawData() {
        [...CheckboxElem.custAttrs, ...CheckboxElem.lazyAttrs].forEach((k: string) => {
            this.propsRaw[k] = this.getAttribute(k);
        });
        CheckboxElem.boolAttrs.forEach((k: string) => {
            this.propsRaw[k] = getAttrBool(this.getAttribute(k));
        });
        this.propsRaw.label = this.getAttribute('label') || this.innerHTML;
        for (let k in this.propsRaw) {
            this.propsProxy[k] = this.propsRaw[k];
        }
        this.innerHTML = '';
    }
    fillWrap(data: T_obj) {
        this.wrapEl = createEl('label', { [alias]: 'wrap' });
        this.inputEl = createEl('input', { type: 'checkbox', value: (data.value === null || data.value === '') ? data.label : data.value });
        this.legendEl = createEl('u');
        this.labelEl = createEl('i', '', data.label);
        this.wrapEl.append(this.inputEl, this.legendEl, this.labelEl);
    }
    render() {
        this.appendChild(this.wrapEl);
        this.inputEl.addEventListener('change', (e: any) => {
            let val = e.target.checked ? 'ed' : '';
            this.setAttribute('check', val);
        }, false);
    }
}
window.customElements.define("ax-checkbox", CheckboxElem);
export default CheckboxElem;
