"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupAliases = setupAliases;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
async function setupAliases(projectRoot) {
    await setupViteAliases(projectRoot);
    await setupTsConfigAliases(projectRoot);
}
async function setupViteAliases(projectRoot) {
    const viteConfigPath = path_1.default.join(projectRoot, 'vite.config.js');
    const viteConfigTsPath = path_1.default.join(projectRoot, 'vite.config.ts');
    // Check which config file exists
    const configPath = await fs_extra_1.default.pathExists(viteConfigTsPath) ? viteConfigTsPath : viteConfigPath;
    const isTypeScript = configPath.endsWith('.ts');
    const aliasConfig = `
      '@constants': path.resolve(__dirname, 'src/constants'),
      '@formickConstants': path.resolve(__dirname, 'src/constants'),
      '@utils': path.resolve(__dirname, 'src/utils'),
      '@formickUtils': path.resolve(__dirname, 'src/utils'),
      '@components': path.resolve(__dirname, 'src/components'),
      '@formickComponents': path.resolve(__dirname, 'src/components'),
      '@types': path.resolve(__dirname, 'src/types'),
      '@formickTypes': path.resolve(__dirname, 'src/types'),
      '@hooks': path.resolve(__dirname, 'src/hooks'),
      '@formickHooks': path.resolve(__dirname, 'src/hooks'),
      '@assets': path.resolve(__dirname, 'src/assets'),
      '@formickAssets': path.resolve(__dirname, 'src/assets'),
      '@mocks': path.resolve(__dirname, 'src/mocks'),
      '@formickMocks': path.resolve(__dirname, 'src/mocks'),`;
    if (await fs_extra_1.default.pathExists(configPath)) {
        let content = await fs_extra_1.default.readFile(configPath, 'utf-8');
        // Check if aliases are already configured
        if (content.includes('@formickConstants') || content.includes('@formickUtils')) {
            return; // Already configured
        }
        // Add path import if not present
        if (!content.includes("import path from 'path'") && !content.includes('import path from "path"')) {
            content = content.replace(/import { defineConfig } from ['"]vite['"];?/, `import { defineConfig } from 'vite';\nimport path from 'path';`);
        }
        // Add alias configuration
        if (content.includes('resolve:')) {
            // If resolve already exists, add alias to it
            if (content.includes('alias:')) {
                // If alias already exists, merge with existing
                content = content.replace(/alias:\s*{([^}]*)}/, `alias: {$1${aliasConfig}
      }`);
            }
            else {
                // Add alias to existing resolve
                content = content.replace(/resolve:\s*{/, `resolve: {
    alias: {${aliasConfig}
    },`);
            }
        }
        else {
            // Add entire resolve configuration
            content = content.replace(/export default defineConfig\(\{/, `export default defineConfig({
  resolve: {
    alias: {${aliasConfig}
    }
  },`);
        }
        await fs_extra_1.default.writeFile(configPath, content);
    }
    else {
        // Create new vite config file
        const configContent = isTypeScript ? `
import { defineConfig } from 'vite';
import path from 'path';

export default defineConfig({
  resolve: {
    alias: {${aliasConfig}
    }
  }
});
` : `
import { defineConfig } from 'vite';
import path from 'path';

export default defineConfig({
  resolve: {
    alias: {${aliasConfig}
    }
  }
});
`;
        await fs_extra_1.default.writeFile(configPath, configContent);
    }
}
async function setupTsConfigAliases(projectRoot) {
    const tsConfigPath = path_1.default.join(projectRoot, 'tsconfig.json');
    if (await fs_extra_1.default.pathExists(tsConfigPath)) {
        let content = await fs_extra_1.default.readFile(tsConfigPath, 'utf-8');
        // Parse JSON (with comments support)
        let tsConfig;
        try {
            // Remove comments for parsing
            const cleanContent = content.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '');
            tsConfig = JSON.parse(cleanContent);
        }
        catch (error) {
            console.warn('Could not parse tsconfig.json, skipping TypeScript alias setup');
            return;
        }
        // Check if aliases are already configured
        if (tsConfig.compilerOptions?.paths?.['@formickConstants/*']) {
            return; // Already configured
        }
        // Ensure compilerOptions exists
        if (!tsConfig.compilerOptions) {
            tsConfig.compilerOptions = {};
        }
        // Set baseUrl if not present
        if (!tsConfig.compilerOptions.baseUrl) {
            tsConfig.compilerOptions.baseUrl = '.';
        }
        // Add paths
        if (!tsConfig.compilerOptions.paths) {
            tsConfig.compilerOptions.paths = {};
        }
        // Add alias paths
        Object.assign(tsConfig.compilerOptions.paths, {
            '@constants/*': ['src/constants/*'],
            '@formickConstants/*': ['src/constants/*'],
            '@utils/*': ['src/utils/*'],
            '@formickUtils/*': ['src/utils/*'],
            '@components/*': ['src/components/*'],
            '@formickComponents/*': ['src/components/*'],
            '@types/*': ['src/types/*'],
            '@formickTypes/*': ['src/types/*'],
            '@hooks/*': ['src/hooks/*'],
            '@formickHooks/*': ['src/hooks/*'],
            '@assets/*': ['src/assets/*'],
            '@formickAssets/*': ['src/assets/*'],
            '@mocks/*': ['src/mocks/*'],
            '@formickMocks/*': ['src/mocks/*']
        });
        // Write back to file with proper formatting
        await fs_extra_1.default.writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2));
    }
}
//# sourceMappingURL=aliasSetup.js.map