import createEl from "../utils/createEl";
import extend from "../utils/extend";
import getEl from "../utils/getEl";
import instance from "../utils/instance";
import pipe from "../utils/pipe";
import PubBase from "./PubBase";
import PubListen from "./PubListen";
import ax from "../ax";
import PubCommon from "./PubCommon";
class Validate extends pipe(PubListen, PubCommon)(PubBase) {
    constructor(elem, options) {
        super();
        this.targetEl = getEl(elem);
        if (!this.targetEl) {
            return this;
        }
        ;
        if (!this.targetEl.getAttribute('name')) {
            throw new Error('The element to be verified must have name attribute!');
        }
        this.options = extend({
            insName: '',
            label: '',
            mode: 'newline',
            url: '',
            iconShow: true,
            type: '',
            parent: '',
            failure: '',
            success: '通过验证！',
            rule: {},
            digital: '',
            max: '',
            min: '',
            length: '',
            maxLength: '',
            minLength: '',
            include: [],
            exclude: [],
            date: '',
            earliest: '',
            latest: '',
            compare: '',
            compareLabel: '',
            least: 1,
            most: 2,
            separator: '',
            successShow: true,
            message: {},
            popup: {},
            format: '',
            inputClasses: {
                succ: 'ax-check-primary',
                fail: 'ax-check-error',
            },
            boxClasses: {
                succ: 'ax-color-primary',
                fail: 'ax-color-error',
            },
            regLocal: '',
            regChars: '',
            charsShow: false,
            trigger: 'blur',
            ajaxType: 'post',
            breakpoints: {},
            immediately: true,
            lang: {},
            onTrigger: '',
            onChange: '',
            onInit: '',
        }, options, this.targetEl, this.constructor.name);
        this.plans = {};
        this.isFocus = false;
        this.init();
    }
    init(cb) {
        instance.push(this, this.options.insName, 'valid');
        this.destroyed = false;
        this.placeholder = this.getPlaceholder();
        this.setAttrs();
        this.render();
        super.listen({ name: 'init', cb });
        return this;
    }
    getPlaceholder() {
        let inputHolder = this.inputDom.getAttribute('placeholder');
        return this.options.placeholder ? this.options.placeholder :
            inputHolder ? inputHolder : ax.lang.form.placeholder;
    }
    render() {
        if (!this.labelDom) {
            this.labelDom = createEl('label', '', this.placeholder);
            this.inputDom.removeAttribute("placeholder");
            this.inputDom.insertAdjacentElement('afterend', this.labelDom);
        }
    }
    setAttrs() {
        this.targetEl.classList.add(`${ax.pfx}dodge`);
        this.inputDom.setAttribute('readonly', '');
        this.inputDom.classList.contains("ax-lg") ? this.targetEl.classList.add("ax-lg") : null;
    }
    set(value, cb) {
        if (this.destroyed) {
            return this;
        }
        if (value) {
            this.inputDom.value = value;
            this.toggle.focus();
        }
        else {
            this.inputDom.focus();
        }
        super.listen({ name: 'set', cb, parmas: [value] });
        return this;
    }
    cancel(cb) {
        if (this.destroyed) {
            return this;
        }
        this.inputDom.value = '';
        this.inputDom.blur();
        this.toggle.blur();
        super.listen({ name: 'cancel', cb });
        return this;
    }
    destroy(cb) {
        if (this.destroyed) {
            return this;
        }
        this.labelDom.onclick = null;
        this.inputDom.removeEventListener('focus', this.toggle.focus);
        this.inputDom.removeEventListener('blur', this.toggle.blur);
        this.destroyed = true;
        super.listen({ name: 'destroy', cb });
        return this;
    }
}
export default Validate;
