/**
 * @since Last modified: 2024-08-08 18:58:45
 * @namespace classes
 * @description Add and remove classes in batches.
 * @see {@link https://codepen.io/axui/pen/mdvwQYo|demo @ codepen}
 */
'use strict';
import getClasses from './getClasses';
import getEl from './getEl';
const classes = {
    /**
    * @memberOf classes
    * @description Get classes array.
    * @param {string|string[]} classes - Classes resource.
    * @returns {string[]} Return a classes array.
    * @see {@link https://codepen.io/axui/pen/Jjxqedr|demo @ codepen}
    * @example
    * ax.classes.get('demo01,demo02');
    */
    get: function (classes) {
        return getClasses(classes);
    },
    /**
    * @memberOf classes
    * @description Add classes.
    * @param {*} target - The host of classes,the same to getEl.
    * @param {string|string[]} classes - Classes resource.
    * @param {function} [intercept] - The call back intercept add classes.
    * @returns {string[]} Return classes constant.
    * @see {@link https://codepen.io/axui/pen/Jjxqedr|demo @ codepen}
    * @example
    * ax.classes.add('demo01,demo02');
    */
    add: function (target, classes, intercept) {
        let el = getEl(target), arr = this.get(classes);
        if (!el || arr.length === 0) {
            return false;
        }
        ;
        arr.forEach((k) => {
            let tmp;
            if (intercept) {
                tmp = intercept(k);
                tmp === true ? el.classList.add(k) :
                    (typeof tmp === 'string' && tmp) ? el.classList.add(tmp) : null;
            }
            else {
                el.classList.add(k);
            }
        });
        return this;
    },
    /**
    * @memberOf classes
    * @description Remove classes.
    * @param {*} target - The host of classes,the same to getEl.
    * @param {string|string[]} classes - Classes resource.
    * @param {function} [intercept] - The call back before remove classes.
    * @returns {string[]} Return classes constant.
    * @see {@link https://codepen.io/axui/pen/Jjxqedr|demo @ codepen}
    * @example
    * ax.classes.remove('demo01,demo02');
    */
    remove: function (target, classes, intercept) {
        let el = getEl(target), arr = this.get(classes);
        if (!el || arr.length === 0) {
            return false;
        }
        arr.forEach((k) => {
            let tmp;
            if (intercept) {
                tmp = intercept(k);
                tmp === true ? el.classList.remove(k) :
                    (typeof tmp === 'string' && tmp) ? el.classList.remove(tmp) : null;
            }
            else {
                el.classList.remove(k);
            }
        });
        return this;
    },
    replace: (target, newClass, oldClass) => {
        let el = getEl(target);
        if (el && newClass && oldClass) {
            el.classList.remove(oldClass);
            el.classList.add(newClass);
        }
    }
};
export default classes;
