/**
 * @since Last modified: 2024-01-18 16:17:08
 * @function getFormData
 * @description Get array data of multi-valued form fields, including select-one, select-multiple, checkbox, radio and file types.
 * @param {object} obj  - Parameters.
 * @param {getEl} obj.elem  - Dom selector of input.
 * @param {select|file|checkbox|radio} [obj.type="checkbox"]  - The input type.If elem itself is not a form field, you need to use this parameter to specify the form field type.
 * @param {""|"string"|"array"} [obj.format=""]  - Output value type.
 * @param {getEl} [obj.parent=document.body]  - The parent Dom of input.If elem itself is not a form field, you need to use this parameter to specify the parent node of the form field.
 * @param {string} [obj.separator=","]  - The separator character of Output value.if format=false,can use the parameter.
 * @returns {array|string} Return a value array of value string.
 * @see {@link https://codepen.io/axui/pen/jOJbJMy|demo @ codepen}
 * @example
 * ax.getFormData('select');
 * <!--return ['BJ','SH','TJ']-->
 */
'use strict';
import getDataType from './getDataType';
import getEl from './getEl';
const getFormData = ({ elem, type = 'checkbox', format = '', parent, separator = ',' }) => {
    let items, values = [], parentDom = parent ? getEl(parent) : document.body, none = format === 'array' ? [] : format === 'string' ? '' : null, elemType = getDataType(elem), input, inputType, isMulti = false, result;
    try {
        if (elemType === 'Array' || elemType === 'NodeList') {
            if (elem.length > 0) {
                let node = elem[0].type;
                if (['checkbox', 'radio'].includes(node)) {
                    type = node;
                    items = [...elem];
                    isMulti = type === 'checkbox' ? true : false;
                }
                else {
                    items = [];
                }
            }
            else {
                return none;
            }
        }
        else {
            input = getEl(elem);
            inputType = input?.type;
            if (input) {
                if (['select-one', 'select-multiple', 'file', 'checkbox', 'radio'].includes(inputType)) {
                    items = (inputType.includes('select')) ? [...input.options] : //select类型即是其选项节点
                        (inputType === 'file') ? [...input.files] : //file类型即是其文件数据
                            (inputType === 'checkbox' || inputType === 'radio') ? [...parentDom.querySelectorAll(`[name="${input.name}"]`)] : [];
                    type = (inputType.includes('select')) ? 'select' : inputType;
                    isMulti = (type === 'checkbox') ? true :
                        (type === 'radio') ? false : input.multiple;
                }
                else {
                    let host = (type === 'select') ? input.querySelector('select') :
                        (type === 'file') ? input.querySelector(`input[type="${type}"]`) : { multiple: false }, temp = (type === 'select') ? host?.options :
                        (type === 'file') ? host?.files :
                            (type === 'checkbox' || type === 'radio') ? input.querySelectorAll(`input[type="${type}"]`) : null;
                    if (temp) {
                        items = [...temp];
                        isMulti = (type === 'checkbox') ? true :
                            (type === 'radio') ? false : host.multiple;
                    }
                    else {
                        items = [];
                    }
                }
            }
            else {
                return none;
            }
        }
        if (type === 'select') {
            values = items.filter((k) => k.selected && k.getAttribute('value')).map(k => k.value.trim()).filter(Boolean);
        }
        else if (type === 'radio' || type === 'checkbox') {
            values = items.filter((k) => k.checked && k.getAttribute('value')).map(k => k.value.trim()).filter(Boolean);
        }
        else if (type === 'file') {
            values = items;
        }
        if (format === 'string') {
            result = (type === 'file') ? values.map((k) => k.name.trim()).join(separator) : values.join(separator);
        }
        else if (format === 'array') {
            result = values;
        }
        else {
            result = (type === 'file' && !isMulti) ? (values[0] || null) :
                (!isMulti) ? (values[0] || '') : values;
        }
        return result;
    }
    catch {
        return none;
    }
};
export default getFormData;
