/**
 * @since Last modified: 2024-01-18 16:17:08
 * @function serialize
 * @description Serialize a form and get the values of all form fields.
 * @param {object} obj  - Parameters.
 * @param {getEl|getEls} obj.elem - Form dom or inputs array.
 * @param {""|"string"|"formdata"|"json"} [obj.format=""] - Output value format (array)."string"=>`?name=wang&age=18&like[]=game&name[]=boll`;"formdata"=>return formData format;""=>array data;"json"=>{name:'',age:''...}.
 * @returns  {*} - The output values.
 * @see {@link https://codepen.io/axui/pen/yLwYwdE|demo @ codepen}
 * @example
 * ax.serialize({elem:'#demo'});
 * <!--Return *-->
 */
'use strict';
import ax from "../ax";
import getEl from "./getEl";
import getEls from "./getEls";
import multiField from "./multiField";
const serialize = (elem, format = '') => {
    let inputs, items = [], output;
    try {
        inputs = Array.isArray(elem) ? getEls(elem) : [...getEl(elem).querySelectorAll('[name]')].filter(k => ['INPUT', 'SELECT', 'TEXTAREA'].includes(k.nodeName) && k.name && !k.name.includes(ax.nameSfx));
        inputs.forEach((i) => {
            let have = items.find((k) => k.name === i.name);
            if (have && (i.type === 'checkbox' || i.type === 'radio')) {
                have.doms.push(i);
            }
            else {
                items.push({ name: i.name, type: i.type, doms: [i] });
            }
        });
        items.forEach(k => {
            k.value = (k.type == 'file') ? multiField.get({ elem: k.doms[0] }) :
                k.type.includes('select') ? multiField.get({ elem: k.doms[0] }) :
                    (k.type == 'checkbox' || k.type === 'radio') ? multiField.get({ elem: k.doms }) : k.doms[0].value;
        });
    }
    catch {
    }
    if (format === 'string') {
        output = '';
        items.forEach((k, i) => {
            let symbol = i === 0 ? '' : '&';
            if (Array.isArray(k.value)) {
                if (k.value.length > 0) {
                    k.value.forEach((x) => {
                        output += symbol + k.name + '=' + (k.type === 'file' ? x.name : x);
                    });
                }
                else {
                    output += symbol + k.name + '=';
                }
            }
            else {
                output += symbol + k.name + '=' + (k.type === 'file' ? k.value?.name || '' : k.value);
            }
        });
    }
    else if (format === 'formdata') {
        output = new FormData();
        items.forEach(k => {
            if (Array.isArray(k.value)) {
                if (k.value.length > 0) {
                    k.value.forEach((x) => {
                        output.append(k.name, x);
                    });
                }
                else {
                    output.append(k.name, '');
                }
            }
            else {
                output.append(k.name, k.value);
            }
        });
    }
    else if (format === 'json') {
        output = {};
        items.forEach(k => {
            output[k.name] = k.value;
        });
    }
    else {
        output = items;
    }
    return output;
};
export default serialize;
