/**  
 * @since Last modified: 2024-09-17 00:29:54
 * @class TextareaElem
 * @description The custom element : ax-textarea .
 * @see {@link https://codepen.io/axui/pen/YzMVvWP|demo @ codepen}
 * @example
 * <ax-textarea name="demo" value="book" label="Reading"></ax-textarea>;
 */
import { T_el, 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 createEl from '../utils/createEl';
import createTools from '../utils/createTools';
import elState from '../utils/elState';
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 TextareaElem extends PlainElem {
    labelEl: any;
    inputEl: any;
    toolsEl: any;
    propsRaw: any;
    inputEvent: any;
    limitEl: any;
    constructor() {
        super();
        super.createObs();
        this.getRawData();
        this.fillWrap(this.propsProxy);
        this.inputEvent = new Event('input');
        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.inputEvent);
            this.listen({ name: 'reset' });
        }
        this.clear = () => {
            this.inputEl.value = '';
            this.inputEl.dispatchEvent(this.inputEvent);
            this.listen({ name: 'clear' });
        }
    }
    static custAttrs = ['class', 'name', 'placeholder', 'size', 'limit', 'on-init', 'on-reset', 'on-clear', 'on-change', 'on-input'];
    static boolAttrs = ['disabled', 'readonly'];
    static get observedAttributes() {
        return ['value', 'tools', ...this.custAttrs, ...this.boolAttrs];
    }
    attributeChangedCallback(name: string, oldVal: string, newVal: string) {
        this.propsProxy[name] = TextareaElem.boolAttrs.includes(name) ? getAttrBool(newVal) : name === 'tools' ? getAttrArr(newVal, 'close') : newVal;
        if (TextareaElem.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: any) => {
                    if (k.name === 'close') {
                        k.wrapEl.onclick = () => {
                            this.clear();
                        }
                    } else {
                        k.action && k.action.bind(this);
                    }
                });
            }
        } else if (name === 'value') {
            let tmp = this.inputEl.value;
            this.inputEl.value = newVal;
            if (tmp !== newVal) {
                this.listen({ name: 'change', params: [{ name, oldVal: tmp, newVal }] });
            }
        } else if (name === 'limit') {
            this.updateLimit();
        } else if (['name', 'type', 'placeholder'].includes(name)) {
            this.inputEl.setAttribute(name, newVal);
        }
    }
    connectedCallback() {
        this.render();
        this.listen({ name: 'init' });
    }
    disconnectedCallback() {
    }
    adoptedCallback() {
    }
    getRawData() {
        TextareaElem.custAttrs.forEach((k: string) => {
            this.propsRaw[k] = this.getAttribute(k);
        });
        TextareaElem.boolAttrs.forEach((k: string) => {
            this.propsRaw[k] = getAttrBool(this.getAttribute(k));
        });
        this.propsRaw.value = this.getAttribute('value') || this.innerHTML;
        this.propsRaw.tools = getAttrArr(this.getAttribute('tools'), 'close');
        for (let k in this.propsRaw) {
            this.propsProxy[k] = this.propsRaw[k]
        }
        this.innerHTML = '';
    }
    getToolsEl(data: any) {
        if (!isEmpty(data)) {
            this.toolsEl = createTools(data, this.wrapEl);
            this.toolsEl.setAttribute(alias, 'tools');
        }
    }
    fillWrap(data: T_obj) {
        this.wrapEl = createEl('div', { [alias]: 'wrap' });
        this.inputEl = createEl('textarea', { name: data.name, placeholder: data.placeholder || config.lang.form.placeholder });
        data.value && (this.inputEl.value = data.value);
        this.wrapEl.appendChild(this.inputEl);
        this.getToolsEl(data.tools);
        this.limitEl = createEl('div', { [alias]: 'limit' });
    }
    updateLimit() {
        let total = ~~this.propsProxy.limit,
            value = this.propsProxy.value,
            len = value.length,
            tmp = total - len,
            remaining = tmp <= 0 ? 0 : tmp;
        if (!total) { return this }
        this.limitEl.innerHTML = renderTpl(config.lang.form.maxLength, { total, value: len, remaining });
        if (tmp <= 0) {
            if (tmp < 0) {
                this.inputEl.value = value.slice(0, total);
                this.inputEl.dispatchEvent(this.inputEvent);
            }
            this.setAttribute('exceeded', '');
        } else {
            this.removeAttribute('exceeded');
        }
        elState(this.limitEl).isVirtual && this.appendChild(this.limitEl);
    }
    render() {
        this.appendChild(this.wrapEl);
        ~~this.propsProxy.limit && this.appendChild(this.limitEl);
        this.inputEl.addEventListener('input', (e: any) => {
            let tmp = this.propsProxy.value;
            this.propsProxy.value = this.inputEl.value;
            this.updateLimit();
            let scrolled = this.inputEl.scrollHeight > this.inputEl.clientHeight,
                data = { name: 'value', oldVal: tmp, newVal: this.inputEl.value };
            scrolled ? this.setAttribute('enough', '') : this.removeAttribute('enough');
            this.listen({ name: 'input', params: [data] });
            (tmp !== this.inputEl.value) && this.listen({ name: 'change', params: [data] });
        }, false);
    }
}
window.customElements.define("ax-textarea", TextareaElem);
export default TextareaElem;
