/**
 * @constant getValsFromAttrs
 * @description Get attribute values from tree nodes, allowing filtering of "label" and "value" attributes.
 * @param {HTMLElement} el  - Node selector.
 * @returns {object} - A new object containing the values of the attributes.There must be label and value properties.
 * @see {@link https://codepen.io/axui/pen/GRLJdje|demo @ codepen}
 * @example
 * ax.getValsFromAttrs('#id');
 */



'use strict';

import { T_obj, T_sel } from "../../../types/ax";
import getEl from "./getEl";
const getValsFromAttrs = (el: T_sel): T_obj => {
    let elem = getEl(el),
        obj: T_obj = {};
    if (elem) {
        
        obj.label = elem.textContent.trim();
        let attrs = [...elem.attributes];
        attrs.forEach(k => {
            
            
            obj[k.name] = (['selected', 'disabled', 'readonly', 'active', 'checked', 'expanded', 'draggable'].includes(k.name)) ? true : k.value.trim();
        });
        
        !obj.hasOwnProperty('value') ? obj.value = obj.label : null;
        
        obj.value && !obj.label ? obj.label = obj.value : null;
    }
    return obj;
};
export default getValsFromAttrs;
