/**
 * @since Last modified: 2024/03/04 15:57:45
 * @function formUpdate
 * @description Update form based on data, type=file is invalid.
 * @param {getEl} el - Form node.
 * @param {object} [data] - The data of updated.
 * @param {function} [cb] - The call back of reset.Paramsters are "parent" and "fileds".
 * @returns {null} - Return null.
 * @see {@link https://codepen.io/axui/pen/XWGNgwN|demo @ codepen}
 * @example
 * ax.form.update('#demo');
 * <!--return null-->
 */
'use strict';
import getEl from "./getEl";
import isEmpty from "./isEmpty";
import multiField from "./multiField";
const formUpdate = (el: any, data = {}, cb?: T_fn) => {
    let parent = getEl(el),
        fields = [...parent.querySelectorAll('input:not([type="submit"],[type="reset"],[type="file"]),select,textarea')];
    if (!parent) { return }
    if (isEmpty(data)) { this.reset(parent)}
    for (let [k, v] of Object.entries(data)) {
        let field = fields.find((i: any) => i.name === k);
        if (field) {
            let type = field.type;
            if (['select-one', 'select-multiple', 'checkbox', 'radio'].includes(type)) {
                multiField.set({
                    el: field,
                    value: v,
                    parent,
                });
            } else if (['number', 'range'].includes(type)) {
                field.value = field.min ? field.min : '0';
            } else {
                field.value = '';
            }
        }
    }
    cb && cb(parent, fields);
}
export default formUpdate;
