/**
 * @since Last modified: 2024-07-20 20:19:25
 * @function setTransform
 * @description Set the node's style.transform property from the transform object.
 * @param {object} opt - Parameters.
 * @param {getEl} opt.el - Target selector.
 * @param {array} opt.data  - The data to be set.
 * @param {boolean} [opt.instead=false]  - Whether to use the positioned left and top values instead of x and y values.
 * @param {boolean} [opt.cb]  - A callback function that accepts a single parameter:transformTxt.
 * @returns {null} - Return null;
 * @see {@link https://codepen.io/axui/pen/ExzmYJw|demo @ codepen}
 * @example
 * ax.setTransform({el:'#demo',data:{}});
 * <!--return null-->
 */
'use strict';
import getEl from "./getEl";
import getTransform from "./getTransform";
import isEmpty from "./isEmpty";
import isNull from "./isNull";
const setTransform = ({ el, data, instead = false, cb }) => {
    let dom = getEl(el), now = getTransform(dom), transformTxt = dom?.style.transform || '', getReg = (type) => { return new RegExp(`${type}\\(([^\\)]+)\\)`, 'gmi'); }, re = (val, label, prop) => isNull(val[prop]) ? now[label][prop] : val[prop];
    if (!dom && isEmpty(data)) {
        return;
    }
    for (let k in data) {
        if (data.hasOwnProperty(k)) {
            let value = '';
            if (k === 'translate') {
                if (instead) {
                    !isNaN(data[k]) ?
                        (dom.style.left = `${data[k]}px`) :
                        (dom.style.left = `${re(data[k], 'translate', 'x')}px`, dom.style.top = `${re(data[k], 'translate', 'y')}px`);
                }
                else {
                    value = !isNaN(data[k]) ? `${k}(${data[k]}px)` : `${k}(${re(data[k], 'translate', 'x')}px,${re(data[k], 'translate', 'y')}px)`;
                    if (transformTxt.includes(k)) {
                        if (transformTxt.startsWith('translate')) {
                            transformTxt = transformTxt.replace(getReg(k), value);
                        }
                        else {
                            transformTxt = transformTxt.replace(getReg(k), '');
                            transformTxt = `${value} ${transformTxt}`;
                        }
                    }
                    else {
                        transformTxt = `${value} ${transformTxt}`;
                    }
                }
            }
            else {
                if (k === 'scale') {
                    value = !isNaN(data[k]) ? `${k}(${data[k]})` : `${k}(${re(data[k], 'scale', 'x')},${re(data[k], 'scale', 'y')})`;
                }
                else if (k === 'rotate') {
                    value = `${k}(${data[k]}deg)`;
                }
                else if (k === 'skew') {
                    value = !isNaN(data[k]) ? `${k}(${data[k]}deg)` : `${k}(${re(data[k], 'skew', 'x')}deg,${re(data[k], 'skew', 'y')}deg)`;
                }
                if (transformTxt.includes(k)) {
                    transformTxt = transformTxt.replace(getReg(k), value);
                }
                else {
                    transformTxt += ` ${value}`;
                }
            }
        }
    }
    dom.style.transform = transformTxt;
    cb && cb(transformTxt);
};
export default setTransform;
