/**
 * @since Last modified: 2024-07-11 09:30:49
 * @function createBtns
 * @description Create a sortable buttons.
 * @param {array} data  - The data of btns.Supports abbreviations:'cancel','close' and 'confirm'.
 * @param {getEl} parent - The host node of insert.
 * @returns {object} - The box of btns dom.
 * @see {@link https://codepen.io/axui/pen/RwvEZjJ|demo @ codepen}
 * @example
 * ax.createBtns([{},{},'close','cancel','confirm']);
 * <!--return a node of btns-->
 * data = [
             {
                 name: 'man',
                 attrs: {
                    type:'',
                    theme:'',
                    label:ax.lang.button.cancel,
                    size:'',
                    width:'',
                    shape:'',
                    check:'',
                    icon:'',
                    tail:'',
                    mean:'',
                    disabled:'',
                    shaded:'',
                    grad:'',
                },
                 classes:'',
                 action: null,
             },
             'confirm',
             'cancel',
             'close'
         ]
 */
'use strict';
import ax from '../ax';
import prefix from '../global/prefix';
import classes from './classes';
import createEl from './createEl';
import extend from './extend';
import getEl from './getEl';
import isEmpty from './isEmpty';
import isNull from './isNull';
const createBtns = (data, parent, settings = {}) => {
    /*!  data = [
             {
                 name: 'cancel',
                 classes:'',
                 attrs: {},
                    type:'',
                    theme:'',
                    label:ax.lang.button.cancel,
                    href:'',
                    target:'',
                    rel:'',
                    size:'',
                    width:'',
                    shape:'',
                    check:'',
                    icon:'',
                    tail:'',
                    mean:'',
                    disabled:'',
                    shaded:'',
                    grad:'',
                 action: null,
                 url:'',异步操作时需要的后台地址
                 event:'',事件类型，可能是submit，reset等
                 el:'',节点，在操作中自动添加
             },
         ] */
    let target = getEl(parent), wrapper = createEl('div', { class: `${prefix}box-btns` }), baseChidren = {
        close: {
            name: 'close',
            label: ax.lang.button.close,
        },
        cancel: {
            name: 'cancel',
            label: ax.lang.button.cancel,
        },
        confirm: {
            name: 'confirm',
            label: ax.lang.button.confirm,
        },
    }, renderDom = (props) => {
        let dft = {
            label: Object.keys(ax.lang.button).includes(props.name) && !props.label ? ax.lang.button[props.name] : (props.label || ax.lang.button.default)
        };
        props.name ? dft.role = props.name : null;
        props.type ? dft.type = props.type : null;
        props.theme ? dft.theme = props.theme : null;
        props.size ? dft.size = props.size : null;
        props.width ? dft.width = props.width : null;
        props.shape ? dft.shape = props.shape : null;
        props.check ? dft.check = props.check : null;
        props.icon ? dft.icon = props.icon : null;
        props.tail ? dft.tail = props.tail : null;
        props.disk ? dft.disk = props.disk : null;
        props.cube ? dft.cube = props.cube : null;
        props.hasOwnProperty('badge') ? dft.badge = props.badge : null;
        props.tips ? dft.tips = props.tips : null;
        props.mean ? dft.mean = props.mean : null;
        props.disabled ? dft.disabled = props.disabled : null;
        props.shaded ? dft.shaded = props.shaded : null;
        props.grad ? dft.grad = props.grad : null;
        !isNull(props.href) ? dft.href = props.href : null;
        props.target && !isNull(props.href) ? dft.target = props.target : null;
        props.rel && !isNull(props.href) ? dft.rel = props.rel : null;
        props.el = createEl('ax-btn', Object.assign({ ...settings }, dft, props.attrs));
        !isEmpty(props.classes) ? classes.add(props.el, props.classes) : null;
    };
    data.forEach((k, i) => {
        let result = k === 'confirm' ? baseChidren.confirm :
            k.name === 'confirm' ? extend({ target: baseChidren.confirm, source: k }) :
                k === 'close' ? baseChidren.close :
                    k.name === 'close' ? extend({ target: baseChidren.close, source: k }) :
                        k === 'cancel' ? baseChidren.cancel :
                            k.name === 'cancel' ? extend({ target: baseChidren.cancel, source: k }) : k;
        renderDom(result);
        wrapper.appendChild(result.el);
        result.action ? result.action(result) : null;
        data[i] = result;
    });
    if (target) {
        let tmp = target.querySelector(`.${prefix}box-btns`);
        tmp && tmp.remove();
        target.appendChild(wrapper);
    }
    return wrapper;
};
export default createBtns;
