/**
 * @since Last modified: 2024-08-21 12:02:55
 * @function setSingleSel
 * @description One-dimensional array deduplication.
 * @param {getEls|getEls[]} target - The target elements or a selector.
 * @param {string} sel - The element selector.
 * @returns  {null} - null.
 * @see {@link https://codepen.io/axui/pen/vYqRyMx|demo @ codepen}
 * @example
 * ax.setSingleSel('.classname','.success');
 * <!--Return null->
 */
'use strict';
import { T_el, T_sel } from "../../../types/ax";
import getEls from "./getEls";
const setSingleSel = (target: T_sel | T_sel[], sel: string) => {
    let arr = Array.isArray(target) ? target : [target],
        els = arr.map((k: any) => getEls(k)).flat().filter(Boolean),
        setFn = (el: T_el) => {
            if (sel.trim().startsWith('#')) {
                el.id = sel.slice(1);
            } else if (sel.trim().startsWith('.')) {
                el.classList.add(sel.slice(1));
            } else if (sel.trim().startsWith('[') && sel.trim().endsWith(']')) {
                let tmp = sel.slice(1, -1).replace('"', '').replace("'", '').split('=');
                el.setAttribute(tmp[0], tmp[1] || '');
            }
        };
    if (els.length === 0 || !sel || typeof sel !== 'string') return;
    for (let k of els) {
        setFn(k);
    }
}
export default setSingleSel;
