/**
 * @since Last modified: 2024-07-11 09:30:49
 * @function createTools
 * @description Create the icon button tools group.
 * @param {array} data  - The data of tools.Supports abbreviations:data:['toggle','close','update','remove','enlarge','widen','heighten'].
 * @param {getEl} parent - The host node of insert.
 * @returns {object} - The box of tools dom.
 * @see {@link https://codepen.io/axui/pen/WNmOJrJ|demo @ codepen}
 * @example
 * ax.createTools([{},{},'toggle','close']);
 * <!--return a node of tools-->
 * data = [
             {
                 name: 'man',
                 nodeName: 'span',
                 icon:'',
                 attrs: {},
                 classes:'',
                 text:'',
                 action: null,
             },
             'close',
             'toggle'
         ]
 */
'use strict';
import { T_sel } from '../../../types/ax';
import { T_tool_uion } from '../../../types/utils';
import ax from '../ax';
import prefix from '../global/prefix';
import classes from './classes';
import createEl from './createEl';
import getEl from './getEl';
import isEmpty from './isEmpty';
const createTools = (data: T_tool_uion[], parent?: T_sel) => {
    /*!  data = [
             {
                 name: 'close',
                 nodeName: 'span',
                 icon:'',
                 attrs: {},
                 classes:'',
                 text:'',
                 action: null,
             },
             {
                 name: 'max',
                 nodeName: 'span',
                 icon:'',
                 attrs: {},
                 classes:'',
                 text:'',
                 action: null,
             },
         ] */
    let target = getEl(parent),
        wrapper = createEl('span', { class: `${prefix}box-tools` }),
        baseChidren = {
            close: {
                name: 'close',
                icon: `${prefix}icon-close`,
            },
            remove: {
                name: 'remove',
                icon: `${prefix}icon-trash`,
            },
            update: {
                name: 'update',
                icon: `${prefix}icon-refresh`,
            },
            move: {
                name: 'move',
                icon: `${prefix}icon-move`,
            },
            toggle: {
                name: 'toggle',
                icon: `${prefix}icon-max`,
                swap: `${prefix}icon-min`,
            },
            enlarge: {
                name: 'enlarge',
                icon: `${prefix}icon-expand`,
                swap: `${prefix}icon-collapse`,
            },
            widen: {
                name: 'widen',
                icon: `${prefix}icon-expand-h`,
                swap: `${prefix}icon-collapse-h`,
            },
            heighten: {
                name: 'heighten',
                icon: `${prefix}icon-expand-v`,
                swap: `${prefix}icon-collapse-v`,
            }
        },
        renderDom = (props: any) => {
            let dft: any = {},
                iconStr = props.icon ? `<i class="${props.icon}" role="icon"></i>` : '',
                label = props.label ? `<i role="label">${props.label}</i>` : '',
                html = iconStr + label;
            props.name ? dft.role = props.name : null;
            props.wrapEl = createEl(props.nodeName || 'span', Object.assign(dft, props.attrs), html);
            props.iconEl = props.wrapEl.querySelector(`[role="icon"]`);
            props.labelEl = props.wrapEl.querySelector(`[role="label"]`);
            !isEmpty(props.classes) ? classes.add(props.wrapEl, props.classes) : null;
        };
    data.forEach((k: any, i: number) => {
        let result = k === 'close' ? baseChidren.close :
            k.name === 'close' ? Object.assign(baseChidren.close, k) :
                k === 'toggle' ? baseChidren.toggle :
                    k.name === 'toggle' ? Object.assign(baseChidren.toggle, k) :
                        k === 'move' ? baseChidren.move :
                            k.name === 'move' ? Object.assign(baseChidren.move, k) :
                                k === 'remove' ? baseChidren.remove :
                                    k.name === 'remove' ? Object.assign(baseChidren.remove, k) :
                                        k === 'update' ? baseChidren.update :
                                            k.name === 'update' ? Object.assign(baseChidren.update, k) :
                                                k === 'enlarge' ? baseChidren.enlarge :
                                                    k.name === 'enlarge' ? Object.assign(baseChidren.enlarge, k) :
                                                        k === 'widen' ? baseChidren.widen :
                                                            k.name === 'widen' ? Object.assign(baseChidren.widen, k) :
                                                                k === 'heighten' ? baseChidren.heighten :
                                                                    k.name === 'heighten' ? Object.assign(baseChidren.heighten, k) : k;
        renderDom(result);
        wrapper.appendChild(result.wrapEl);
        result.action ? result.action(result) : null;
        data[i] = result;
    });
    if (target) {
        let tmp = target.querySelector(`.${prefix}box-tools`);
        tmp && tmp.remove();
        target.appendChild(wrapper)
    }
    return wrapper;
}
export default createTools;
