"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProjectAliases = getProjectAliases;
exports.findMatchingAlias = findMatchingAlias;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
/**
 * Read existing aliases from vite.config.ts or vite.config.js
 */
async function readViteAliases(cwd) {
    const viteConfigPaths = [
        path_1.default.resolve(cwd, 'vite.config.ts'),
        path_1.default.resolve(cwd, 'vite.config.js'),
    ];
    for (const configPath of viteConfigPaths) {
        if (await fs_extra_1.default.pathExists(configPath)) {
            try {
                const content = await fs_extra_1.default.readFile(configPath, 'utf-8');
                // Extract alias object using regex (simple approach)
                const aliasMatch = content.match(/alias:\s*\{([\s\S]*?)\}/);
                if (aliasMatch) {
                    const aliasContent = aliasMatch[1];
                    const aliases = {};
                    // Extract key-value pairs
                    const aliasRegex = /['"]([^'"]+)['"]:\s*path\.resolve\([^)]+['"]([^'"]+)['"]\)/g;
                    let match;
                    while ((match = aliasRegex.exec(aliasContent)) !== null) {
                        aliases[match[1]] = match[2];
                    }
                    return aliases;
                }
            }
            catch (error) {
                // If parsing fails, return empty object
                console.warn(`Could not parse vite config: ${error}`);
            }
        }
    }
    return {};
}
/**
 * Read existing path mappings from tsconfig.json
 */
async function readTsconfigAliases(cwd) {
    const tsconfigPath = path_1.default.resolve(cwd, 'tsconfig.json');
    if (await fs_extra_1.default.pathExists(tsconfigPath)) {
        try {
            const tsconfig = JSON.parse(await fs_extra_1.default.readFile(tsconfigPath, 'utf-8'));
            if (tsconfig.compilerOptions?.paths) {
                return tsconfig.compilerOptions.paths;
            }
        }
        catch (error) {
            console.warn(`Could not parse tsconfig.json: ${error}`);
        }
    }
    return {};
}
/**
 * Get all existing aliases from the project
 */
async function getProjectAliases(cwd) {
    const [viteAliases, tsconfigAliases] = await Promise.all([
        readViteAliases(cwd),
        readTsconfigAliases(cwd),
    ]);
    return {
        vite: viteAliases,
        tsconfig: tsconfigAliases,
    };
}
/**
 * Find the best matching alias for a given path
 */
function findMatchingAlias(filePath, aliases) {
    // Normalize the path
    const normalizedPath = filePath.replace(/\\/g, '/');
    // Try to find exact match first
    for (const [alias, aliasPath] of Object.entries(aliases)) {
        const normalizedAliasPath = aliasPath.replace(/\\/g, '/');
        if (normalizedPath === normalizedAliasPath || normalizedPath.startsWith(normalizedAliasPath + '/')) {
            return { alias, path: aliasPath };
        }
    }
    // Try to find partial match
    for (const [alias, aliasPath] of Object.entries(aliases)) {
        const normalizedAliasPath = aliasPath.replace(/\\/g, '/');
        if (normalizedPath.includes(normalizedAliasPath)) {
            return { alias, path: aliasPath };
        }
    }
    return null;
}
//# sourceMappingURL=projectAliases.js.map