"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformImports = transformImports;
exports.transformCssVariables = transformCssVariables;
// Import transformers for component files
const projectAliases_1 = require("./projectAliases");
const path_1 = __importDefault(require("path"));
/**
 * Resolve a relative import path to an alias using project aliases
 */
function resolveToAlias(relativePath, currentFileDir, basePath, projectAliases) {
    if (!projectAliases?.vite || !basePath) {
        return null;
    }
    // Resolve the relative path to absolute
    const resolvedPath = path_1.default.resolve(currentFileDir, relativePath).replace(/\\/g, '/');
    const basePathResolved = path_1.default.resolve(basePath).replace(/\\/g, '/');
    // Make it relative to basePath
    const relativeToBase = path_1.default.relative(basePathResolved, resolvedPath).replace(/\\/g, '/');
    // Find matching alias
    const match = (0, projectAliases_1.findMatchingAlias)(relativeToBase, projectAliases.vite);
    if (match) {
        // Replace the path part with the alias
        const remainingPath = relativeToBase.replace(match.path.replace(/\\/g, '/'), '').replace(/^\//, '');
        return remainingPath ? `${match.alias}/${remainingPath}` : match.alias;
    }
    return null;
}
function transformImports(content, config) {
    let transformedContent = content;
    const projectAliases = config.projectAliases;
    const basePath = config.basePath;
    // First, handle direct @ imports that need to be transformed to #formick* aliases
    // Transform @constants imports (most common pattern)
    transformedContent = transformedContent.replace(/@constants\/([^"']+)/g, '#formickConstants/$1');
    transformedContent = transformedContent.replace(/@constants/g, '#formickConstants');
    // Transform @utils imports
    transformedContent = transformedContent.replace(/@utils\/([^"']+)/g, '#formickUtils/$1');
    transformedContent = transformedContent.replace(/@utils/g, '#formickUtils');
    // Transform @components imports
    transformedContent = transformedContent.replace(/@components\/formbuilder\/([^"']+)/g, '#formickComponents/formbuilder/$1');
    transformedContent = transformedContent.replace(/@components\/([^"']+)/g, '#formickComponents/$1');
    transformedContent = transformedContent.replace(/@components/g, '#formickComponents');
    // Transform @hooks imports
    transformedContent = transformedContent.replace(/@hooks\/([^"']+)/g, '#formickHooks/$1');
    transformedContent = transformedContent.replace(/@hooks/g, '#formickHooks');
    // Transform @assets imports
    transformedContent = transformedContent.replace(/@assets\/([^"']+)/g, '#formickAssets/$1');
    transformedContent = transformedContent.replace(/@assets/g, '#formickAssets');
    // Transform @mocks imports
    transformedContent = transformedContent.replace(/@mocks\/([^"']+)/g, '#formickMocks/$1');
    transformedContent = transformedContent.replace(/@mocks/g, '#formickMocks');
    // Transform @types imports
    transformedContent = transformedContent.replace(/@types\/([^"']+)/g, '#formickTypes/$1');
    transformedContent = transformedContent.replace(/@types/g, '#formickTypes');
    // Transform all relative path imports to #formick* alias imports
    // 1. Transform types imports (all variations)
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/types["']/g, 'from "#formickTypes"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/types["']/g, 'from "#formickTypes"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/types["']/g, 'from "#formickTypes"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/types["']/g, 'from "#formickTypes"');
    // 2. Transform constants imports (all variations)
    // Try to use existing project aliases first, fallback to #formickConstants
    const constantsAlias = projectAliases?.vite && Object.keys(projectAliases.vite).find(alias => projectAliases.vite[alias]?.includes('constants')) || '#formickConstants';
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/constants\/(.*?)["']/g, (_, constantPath) => `from "${constantsAlias}/${constantPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/constants\/(.*?)["']/g, (_, constantPath) => `from "${constantsAlias}/${constantPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/constants\/(.*?)["']/g, (_, constantPath) => `from "${constantsAlias}/${constantPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/constants\/(.*?)["']/g, (_, constantPath) => `from "${constantsAlias}/${constantPath}"`);
    // Handle constants without specific file
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/constants["']/g, `from "${constantsAlias}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/constants["']/g, `from "${constantsAlias}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/constants["']/g, `from "${constantsAlias}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/constants["']/g, `from "${constantsAlias}"`);
    // 3. Transform utils imports (all variations)
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/utils\/(.*?)["']/g, (_, utilPath) => `from "#formickUtils/${utilPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/utils\/(.*?)["']/g, (_, utilPath) => `from "#formickUtils/${utilPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/utils\/(.*?)["']/g, (_, utilPath) => `from "#formickUtils/${utilPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/utils\/(.*?)["']/g, (_, utilPath) => `from "#formickUtils/${utilPath}"`);
    // Handle utils without specific file
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/utils["']/g, 'from "#formickUtils"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/utils["']/g, 'from "#formickUtils"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/utils["']/g, 'from "#formickUtils"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/utils["']/g, 'from "#formickUtils"');
    // 4. Transform hooks imports (all variations)
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/hooks\/(.*?)["']/g, (_, hookPath) => `from "#formickHooks/${hookPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/hooks\/(.*?)["']/g, (_, hookPath) => `from "#formickHooks/${hookPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/hooks\/(.*?)["']/g, (_, hookPath) => `from "#formickHooks/${hookPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/hooks\/(.*?)["']/g, (_, hookPath) => `from "#formickHooks/${hookPath}"`);
    // 5. Transform assets imports (all variations)
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/assets\/(.*?)["']/g, (_, assetPath) => `from "#formickAssets/${assetPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/assets\/(.*?)["']/g, (_, assetPath) => `from "#formickAssets/${assetPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/assets\/(.*?)["']/g, (_, assetPath) => `from "#formickAssets/${assetPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/assets\/(.*?)["']/g, (_, assetPath) => `from "#formickAssets/${assetPath}"`);
    // 6. Transform mocks imports (all variations)
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/mocks\/(.*?)["']/g, (_, mockPath) => `from "#formickMocks/${mockPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/mocks\/(.*?)["']/g, (_, mockPath) => `from "#formickMocks/${mockPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/mocks\/(.*?)["']/g, (_, mockPath) => `from "#formickMocks/${mockPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/mocks\/(.*?)["']/g, (_, mockPath) => `from "#formickMocks/${mockPath}"`);
    // Handle mocks without specific file
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/\.\.\/mocks["']/g, 'from "#formickMocks"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/\.\.\/mocks["']/g, 'from "#formickMocks"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/\.\.\/mocks["']/g, 'from "#formickMocks"');
    transformedContent = transformedContent.replace(/from ["']\.\.\/mocks["']/g, 'from "#formickMocks"');
    // 7. Transform component imports (formbuilder -> formtheme, shared, etc.)
    transformedContent = transformedContent.replace(/from ["']\.\.\/formtheme\/(.*?)["']/g, (_, componentPath) => `from "#formickComponents/formtheme/${componentPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/shared\/(.*?)["']/g, (_, componentPath) => `from "#formickComponents/shared/${componentPath}"`);
    transformedContent = transformedContent.replace(/from ["']\.\.\/formbuilder\/(.*?)["']/g, (_, componentPath) => `from "#formickComponents/formbuilder/${componentPath}"`);
    // 8. Transform relative component imports within the same component
    transformedContent = transformedContent.replace(/from ["']\.\.\/formbuilder["']/g, 'from "#formickComponents/formbuilder"');
    // 9. Handle @components imports that need to be corrected
    transformedContent = transformedContent.replace(/from ["']@components\/formbuilder\/formbuilder["']/g, 'from "#formickComponents/formbuilder"');
    transformedContent = transformedContent.replace(/from ["']@components\/formbuilder\/(.*?)["']/g, (_, componentPath) => `from "#formickComponents/formbuilder/${componentPath}"`);
    // 10. Handle direct @components/formbuilder imports (for SignaturePad, etc.)
    transformedContent = transformedContent.replace(/import\s+(.*?)\s+from\s+["']@components\/formbuilder\/(.*?)["']/g, (_, importName, componentPath) => `import ${importName} from "#formickComponents/formbuilder/${componentPath}"`);
    // Also handle the from pattern for @components/formbuilder (all variations)
    transformedContent = transformedContent.replace(/from\s*["']@components\/formbuilder\/(.*?)["']/g, (_, componentPath) => `from "#formickComponents/formbuilder/${componentPath}"`);
    transformedContent = transformedContent.replace(/["']@components\/formbuilder\/(.*?)["']/g, (_, componentPath) => `"#formickComponents/formbuilder/${componentPath}"`);
    // 11. Handle image imports (all variations)
    transformedContent = transformedContent.replace(/import\s+(.*?)\s+from\s+["']\.\.\/\.\.\/\.\.\/\.\.\/assets\/images\/(.*?)["']/g, (_, importName, imagePath) => `import ${importName} from "#formickAssets/images/${imagePath}"`);
    transformedContent = transformedContent.replace(/import\s+(.*?)\s+from\s+["']\.\.\/\.\.\/\.\.\/assets\/images\/(.*?)["']/g, (_, importName, imagePath) => `import ${importName} from "#formickAssets/images/${imagePath}"`);
    transformedContent = transformedContent.replace(/import\s+(.*?)\s+from\s+["']\.\.\/\.\.\/assets\/images\/(.*?)["']/g, (_, importName, imagePath) => `import ${importName} from "#formickAssets/images/${imagePath}"`);
    transformedContent = transformedContent.replace(/import\s+(.*?)\s+from\s+["']\.\.\/assets\/images\/(.*?)["']/g, (_, importName, imagePath) => `import ${importName} from "#formickAssets/images/${imagePath}"`);
    // 12. Remove mocks imports (since mocks folder is not copied)
    transformedContent = transformedContent.replace(/import\s+.*?\s+from\s+["'].*?mocks.*?["'];?\s*\n?/g, '');
    transformedContent = transformedContent.replace(/from\s+["'].*?mocks.*?["']/g, 'from "#formickMocks"');
    // 13. Handle legacy @ imports (keep existing functionality)
    transformedContent = transformedContent.replace(/from ["']@\/lib\/utils["']/g, 'from "#formickUtils"');
    transformedContent = transformedContent.replace(/from ["']@\/components\/(.*?)["']/g, (_, componentPath) => `from "#formickComponents/${componentPath}"`);
    transformedContent = transformedContent.replace(/from ["']@\/utils["']/g, 'from "#formickUtils"');
    transformedContent = transformedContent.replace(/from ["']@\/constants\/(.*?)["']/g, (_, constantPath) => `from "#formickConstants/${constantPath}"`);
    transformedContent = transformedContent.replace(/from ["']@\/types["']/g, 'from "#formickTypes"');
    transformedContent = transformedContent.replace(/from ["']@\/hooks\/(.*?)["']/g, (_, hookPath) => `from "#formickHooks/${hookPath}"`);
    transformedContent = transformedContent.replace(/from ["']@\/assets\/(.*?)["']/g, (_, assetPath) => `from "#formickAssets/${assetPath}"`);
    return transformedContent;
}
function transformCssVariables(content, config) {
    if (!config.tailwind.cssVariables) {
        return content;
    }
    // Transform CSS custom properties if needed
    // This is where you'd implement CSS variable transformations
    return content;
}
//# sourceMappingURL=transformers.js.map