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

import { useInsertionEffect, useMemo, useRef } from 'react';
import { injectRawCSS } from '../injector/index.js';
// Implementation
export function useRawCSS(cssOrFactory, depsOrOptions, options) {
    // Detect which overload is being used
    const isFactory = typeof cssOrFactory === 'function';
    // Parse arguments based on overload
    const deps = isFactory && Array.isArray(depsOrOptions) ? depsOrOptions : undefined;
    const opts = isFactory
        ? options
        : depsOrOptions;
    // Memoize CSS - for factory functions, use provided deps; for strings, use the string itself
    const css = useMemo(() => isFactory ? cssOrFactory() : cssOrFactory, isFactory ? deps ?? [] : [cssOrFactory]);
    const disposeRef = useRef(null);
    useInsertionEffect(() => {
        // Dispose previous injection if any
        disposeRef.current?.();
        if (!css.trim()) {
            disposeRef.current = null;
            return;
        }
        // Inject new CSS
        const { dispose } = injectRawCSS(css, opts);
        disposeRef.current = dispose;
        // Cleanup on unmount or when css changes
        return () => {
            disposeRef.current?.();
            disposeRef.current = null;
        };
    }, [css, opts?.root]);
}

        