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

import * as fs from 'fs';
import * as path from 'path';
export class CSSWriter {
    cssBlocks = new Map();
    outputPath;
    devMode;
    constructor(outputPath, options = {}) {
        this.outputPath = outputPath;
        this.devMode = options.devMode ?? false;
    }
    /**
     * Add CSS block with deduplication key
     * @param key - Unique key for deduplication
     * @param css - CSS content
     * @param source - Optional source file path (used in devMode)
     */
    add(key, css, source) {
        if (!this.cssBlocks.has(key)) {
            this.cssBlocks.set(key, { css, source });
        }
    }
    /**
     * Check if a key already exists
     */
    has(key) {
        return this.cssBlocks.has(key);
    }
    /**
     * Get the number of CSS blocks
     */
    get size() {
        return this.cssBlocks.size;
    }
    /**
     * Write all collected CSS to the output file
     */
    write() {
        const outputDir = path.dirname(this.outputPath);
        // Ensure directory exists
        if (!fs.existsSync(outputDir)) {
            fs.mkdirSync(outputDir, { recursive: true });
        }
        // Combine all CSS blocks with optional source comments
        const cssBlocks = [];
        for (const block of this.cssBlocks.values()) {
            if (this.devMode && block.source) {
                cssBlocks.push(`/* from: ${block.source} */\n${block.css}`);
            }
            else {
                cssBlocks.push(block.css);
            }
        }
        const css = cssBlocks.join('\n\n');
        // Add header comment
        const header = `/* Generated by @cube-dev/ui-kit/tasty/zero - DO NOT EDIT */\n\n`;
        fs.writeFileSync(this.outputPath, header + css);
    }
    /**
     * Get all CSS as string (for testing or in-memory use)
     */
    getCSS() {
        const cssBlocks = [];
        for (const block of this.cssBlocks.values()) {
            if (this.devMode && block.source) {
                cssBlocks.push(`/* from: ${block.source} */\n${block.css}`);
            }
            else {
                cssBlocks.push(block.css);
            }
        }
        return cssBlocks.join('\n\n');
    }
    /**
     * Clear all collected CSS
     */
    clear() {
        this.cssBlocks.clear();
    }
    /**
     * Get the output path
     */
    getOutputPath() {
        return this.outputPath;
    }
}

        