"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenParser = void 0;
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs-extra"));
class TokenParser {
    /**
     * Parse CSS variables from a file (e.g., globals.css)
     */
    static async parseCssVariables(filePath) {
        try {
            if (!(await fs.pathExists(filePath)))
                return [];
            const content = await fs.readFile(filePath, "utf8");
            const tokens = [];
            // match --variable-name: #value or color-func
            const regex = /--([\w-]+):\s*([^;]+);/g;
            let match;
            while ((match = regex.exec(content)) !== null) {
                const name = match[1];
                const value = match[2];
                if (name && value) {
                    const cleanValue = value.trim();
                    tokens.push({
                        name: `--${name}`,
                        value: cleanValue,
                        sourceFile: filePath,
                        type: this.guessType(cleanValue)
                    });
                }
            }
            return tokens;
        }
        catch (e) {
            console.error(chalk_1.default.red(`[TokenParser] Error parsing CSS in ${filePath}:`), e);
            return [];
        }
    }
    /**
     * Parse exported constants from a TS/JS file
     */
    static async parseConstants(filePath) {
        try {
            if (!(await fs.pathExists(filePath)))
                return [];
            const content = await fs.readFile(filePath, "utf8");
            const tokens = [];
            // match export const NAME = "value" or 'value'
            const regex = /export\s+const\s+(\w+)\s*=\s*['"]([^'"]+)['"]/g;
            let match;
            while ((match = regex.exec(content)) !== null) {
                const name = match[1];
                const value = match[2];
                if (name && value) {
                    tokens.push({
                        name,
                        value,
                        sourceFile: filePath,
                        isExported: true
                    });
                }
            }
            return tokens;
        }
        catch (e) {
            console.error(chalk_1.default.red(`[TokenParser] Error parsing constants in ${filePath}:`), e);
            return [];
        }
    }
    static guessType(value) {
        if (value.startsWith("#") || value.startsWith("rgb") || value.startsWith("hsl") || /^[a-zA-Z]+$/.test(value)) {
            return "color";
        }
        if (value.endsWith("px") || value.endsWith("rem") || value.endsWith("em") || value.endsWith("%")) {
            return "spacing";
        }
        return "other";
    }
    /**
     * Calculate color distance (simple Euclidean in RGB space)
     */
    static getColorDistance(color1, color2) {
        const rgb1 = this.toRgb(color1);
        const rgb2 = this.toRgb(color2);
        if (!rgb1 || !rgb2)
            return Infinity;
        return Math.sqrt(Math.pow(rgb1.r - rgb2.r, 2) +
            Math.pow(rgb1.g - rgb2.g, 2) +
            Math.pow(rgb1.b - rgb2.b, 2));
    }
    static toRgb(color) {
        // Basic Hex parser
        if (color.startsWith("#")) {
            let hex = color.substring(1);
            if (hex.length === 3) {
                const r = hex[0] || '0';
                const g = hex[1] || '0';
                const b = hex[2] || '0';
                hex = r + r + g + g + b + b;
            }
            const r = parseInt(hex.substring(0, 2), 16);
            const g = parseInt(hex.substring(2, 4), 16);
            const b = parseInt(hex.substring(4, 6), 16);
            return { r, g, b };
        }
        // TODO: Add rgb() and hsl() support if needed
        return null;
    }
}
exports.TokenParser = TokenParser;
//# sourceMappingURL=tokenParser.js.map