import ax from '../ax';
import createEl from '../utils/createEl';
import createTools from '../utils/createTools';
import getAttrArr from '../utils/getAttrArr';
import getAttrBool from '../utils/getAttrBool';
import isEmpty from '../utils/isEmpty';
import renderTpl from '../utils/renderTpl';
import PlainElem from './PlainElem';
class FileElem extends PlainElem {
    constructor() {
        super();
        super.createObs();
        this.getRawData();
        this.fillWrap(this.propsProxy);
        this.clickEvent = () => {
            this.inputEl.click();
        };
        this.observer.ins.on('set', (data) => {
            /* if (data.key === 'value') {
                let len = this.propsProxy.value.split(',').length;
                if (len === 0) {
                    this.classList.remove(`${prefix}filled`);
                    this.labelEl.innerHTML = ax.lang.form.fileLabel;
                } else {
                    this.classList.add(`${prefix}filled`);
                    this.labelEl.innerHTML = (len === 1) ? this.propsProxy.value : renderTpl(ax.lang.form.fileMulti, { data: len }) + this.propsProxy.value;
                }
            } */
            this.initialized && this.updateCache({ [data.key]: data.value });
        });
        this.changeEvent = new Event('change');
        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.inputEl.value = '';
            this.inputEl.dispatchEvent(this.changeEvent);
            this.listen({ name: 'reset' });
        };
        this.clear = () => {
            this.inputEl.value = '';
            this.inputEl.dispatchEvent(this.changeEvent);
            this.listen({ name: 'clear' });
        };
    }
    static get observedAttributes() {
        return ['placeholder', ...this.custAttrs, ...this.boolAttrs];
    }
    attributeChangedCallback(name, oldVal, newVal) {
        this.propsProxy[name] = FileElem.boolAttrs.includes(name) ? getAttrBool(newVal) : name === 'tools' ? getAttrArr(newVal, 'close') : newVal;
        if (FileElem.boolAttrs.includes(name)) {
            this.inputEl[name === 'readonly' ? 'readOnly' : name] = this.propsProxy[name];
        }
        else if (name === 'tools') {
            this.toolsEl && this.toolsEl.remove();
            if (this.propsProxy.tools.length > 0) {
                this.getToolsEl(this.propsProxy.tools);
                this.propsProxy.tools.forEach((k) => {
                    if (k.name === 'close') {
                        k.wrapEl.onclick = () => {
                            this.clear();
                        };
                    }
                    else {
                        k.action && k.action.bind(this);
                    }
                });
            }
        }
        else if (name === 'placeholder') {
            this.holderEl.innerHTML = newVal;
        }
        else if (['name', 'accept'].includes(name)) {
            this.inputEl.setAttribute(name, newVal);
        }
        if (oldVal !== newVal) {
            this.listen({ name: 'change', params: [{ name, oldVal, newVal }] });
        }
    }
    connectedCallback() {
        this.render();
        this.initialized = true;
        this.listen({ name: 'init' });
    }
    disconnectedCallback() {
    }
    adoptedCallback() {
    }
    getRawData() {
        [...FileElem.custAttrs, ...FileElem.lazyAttrs].forEach((k) => {
            this.propsRaw[k] = this.getAttribute(k);
        });
        FileElem.boolAttrs.forEach((k) => {
            this.propsRaw[k] = getAttrBool(this.getAttribute(k));
        });
        this.propsRaw.placeholder = this.getAttribute('placeholder') || this.innerHTML || ax.lang.form.fileLabel;
        for (let k in this.propsRaw) {
            this.propsProxy[k] = this.propsRaw[k];
        }
        this.propsProxy['tools'] = getAttrArr(this.getAttribute('tools'), 'close');
        this.propsProxy.value = '';
        this.innerHTML = '';
    }
    getToolsEl(data) {
        if (!isEmpty(data)) {
            this.toolsEl = createTools(data);
            this.toolsEl.setAttribute('role', 'tools');
            this.holderEl.insertAdjacentElement('afterEnd', this.toolsEl);
        }
    }
    fillWrap(data) {
        this.wrapEl = createEl('div', { role: 'wrap' });
        this.inputEl = createEl('input', { type: 'file', name: data.name });
        this.inputEl.multiple = data.multiple;
        this.holderEl = createEl('i', { role: 'placeholder' }, data.placeholder);
        this.labelEl = createEl('i', { role: 'label' });
        this.meanEl = createEl('i', { role: 'mean' }, 'file');
        this.wrapEl.append(this.inputEl, this.labelEl, this.holderEl, this.meanEl);
    }
    render() {
        this.appendChild(this.wrapEl);
        this.inputEl.addEventListener('change', (e) => {
            let files = [...this.inputEl.files], names = files.map((k) => k.name);
            files.length > 0 && (this.propsProxy.value = names.join(','));
            this.labelEl.innerHTML = (files.length === 0) ? '' : (files.length === 1) ? this.inputEl.value : renderTpl(ax.lang.form.fileMulti, { data: files.length }) + this.propsProxy.value;
        }, false);
        this.labelEl.addEventListener('click', this.clickEvent);
        this.holderEl.addEventListener('click', this.clickEvent);
        this.meanEl.addEventListener('click', this.clickEvent);
    }
}
FileElem.lazyAttrs = ['stor-name'];
FileElem.custAttrs = [
    'class',
    'name', 'accept', 'size', 'tools',
    'on-init', 'on-reset', 'on-clear', 'on-change', 'on-set'
];
FileElem.boolAttrs = ['disabled', 'readonly', 'multiple'];
window.customElements.define("ax-file", FileElem);
export default FileElem;
