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

import { getConfig, getGlobalInjector, isTestEnvironment, markStylesGenerated, } from '../config.js';
import { StyleInjector } from './injector.js';
/**
 * Allocate a className for a cacheKey without injecting styles yet
 */
export function allocateClassName(cacheKey, options) {
    return getGlobalInjector().allocateClassName(cacheKey, options);
}
/**
 * Inject styles and return className with dispose function
 */
export function inject(rules, options) {
    // Mark that styles have been generated (prevents configuration changes)
    markStylesGenerated();
    return getGlobalInjector().inject(rules, options);
}
/**
 * Inject global rules that should not reserve tasty class names
 */
export function injectGlobal(rules, options) {
    return getGlobalInjector().injectGlobal(rules, options);
}
/**
 * Inject raw CSS text directly without parsing
 * This is a low-overhead method for injecting raw CSS that doesn't need tasty processing.
 * The CSS is inserted into a separate style element to avoid conflicts with tasty's chunking.
 *
 * @example
 * ```tsx
 * // Inject raw CSS
 * const { dispose } = injectRawCSS(`
 *   body { margin: 0; padding: 0; }
 *   .my-class { color: red; }
 * `);
 *
 * // Later, remove the injected CSS
 * dispose();
 * ```
 */
export function injectRawCSS(css, options) {
    return getGlobalInjector().injectRawCSS(css, options);
}
/**
 * Get raw CSS text for SSR
 */
export function getRawCSSText(options) {
    return getGlobalInjector().getRawCSSText(options);
}
/**
 * Inject keyframes and return object with toString() and dispose()
 */
export function keyframes(steps, nameOrOptions) {
    return getGlobalInjector().keyframes(steps, nameOrOptions);
}
/**
 * Define a CSS @property custom property.
 * This enables advanced features like animating custom properties.
 *
 * Note: @property rules are global and persistent once defined.
 * Re-registering the same property name is a no-op.
 *
 * @param name - The custom property name (must start with --)
 * @param options - Property configuration
 *
 * @example
 * ```ts
 * // Define a color property that can be animated
 * property('--my-color', {
 *   syntax: '<color>',
 *   initialValue: 'red',
 * });
 *
 * // Define an angle property
 * property('--rotation', {
 *   syntax: '<angle>',
 *   inherits: false,
 *   initialValue: '0deg',
 * });
 * ```
 */
export function property(name, options) {
    return getGlobalInjector().property(name, options);
}
/**
 * Check if a CSS @property has already been defined
 *
 * @param name - The custom property name to check
 * @param options - Options including root
 */
export function isPropertyDefined(name, options) {
    return getGlobalInjector().isPropertyDefined(name, options);
}
/**
 * Get CSS text from all sheets (for SSR)
 */
export function getCssText(options) {
    return getGlobalInjector().getCssText(options);
}
/**
 * Collect only CSS used by a rendered subtree (like jest-styled-components).
 * Pass the container returned by render(...).
 */
export function getCssTextForNode(node, options) {
    // Collect tasty-generated class names (t<number>) from the subtree
    const classSet = new Set();
    const readClasses = (el) => {
        const cls = el.getAttribute('class');
        if (!cls)
            return;
        for (const token of cls.split(/\s+/)) {
            if (/^t\d+$/.test(token))
                classSet.add(token);
        }
    };
    // Include node itself if it's an Element
    if (node.getAttribute) {
        readClasses(node);
    }
    // Walk descendants
    const elements = node.querySelectorAll
        ? node.querySelectorAll('[class]')
        : [];
    elements && elements.forEach(readClasses);
    return getGlobalInjector().getCssTextForClasses(classSet, options);
}
/**
 * Force cleanup of unused rules
 */
export function cleanup(root) {
    return getGlobalInjector().cleanup(root);
}
/**
 * Check if we're currently running in a test environment
 */
export function getIsTestEnvironment() {
    return isTestEnvironment();
}
/**
 * Get the global injector instance for debugging
 */
export const injector = {
    get instance() {
        return getGlobalInjector();
    },
};
/**
 * Destroy all resources and clean up
 */
export function destroy(root) {
    return getGlobalInjector().destroy(root);
}
/**
 * Create a new isolated injector instance
 */
export function createInjector(config = {}) {
    const defaultConfig = getConfig();
    const fullConfig = {
        ...defaultConfig,
        // Auto-enable forceTextInjection in test environments
        forceTextInjection: config.forceTextInjection ?? isTestEnvironment(),
        ...config,
    };
    return new StyleInjector(fullConfig);
}
export { StyleInjector } from './injector.js';
export { SheetManager } from './sheet-manager.js';

        