/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-d1bbdd3
 * Released under the MIT license.
 */

import { isSelector } from '../pipeline/index.js';
export function mergeStyles(...objects) {
    let styles = objects[0] ? { ...objects[0] } : {};
    let pos = 1;
    while (pos in objects) {
        const selectorKeys = Object.keys(styles).filter((key) => isSelector(key) && styles[key]);
        const newStyles = objects[pos];
        if (newStyles) {
            const resultStyles = { ...styles, ...newStyles };
            for (let key of selectorKeys) {
                if (newStyles?.[key] === false) {
                    // Remove sub-element styles when explicitly set to false
                    delete resultStyles[key];
                }
                else if (newStyles?.[key] == null) {
                    // Nullish values (null/undefined) are ignored - restore original styles
                    resultStyles[key] = styles[key];
                }
                else if (newStyles?.[key]) {
                    resultStyles[key] = {
                        ...styles[key],
                        ...newStyles[key],
                    };
                }
            }
            styles = resultStyles;
        }
        pos++;
    }
    return styles;
}

        