"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.MigrateTransformCommand = void 0;
const chalk_1 = __importDefault(require("chalk"));
const fileSystem_1 = require("../utils/fileSystem");
const spinner_1 = require("../utils/spinner");
const AICore_1 = require("../core/ai/AICore");
const path = __importStar(require("path"));
const fs = __importStar(require("fs-extra"));
class MigrateTransformCommand {
    constructor() {
        this.fs = new fileSystem_1.FileSystemManager();
        this.spinner = new spinner_1.EnhancedSpinner("Refactoring...");
    }
    async migrateRtl(options) {
        await this.runTransform("rtl", "Convert styles to RTL-friendly logical properties (e.g., ml-4 -> ms-4).", options);
    }
    async migrateRadix(options) {
        await this.runTransform("radix", "Refactor primitive UI elements to use Radix UI / Shadcn primitives.", options);
    }
    async runTransform(type, goal, options) {
        const { path: targetPath, all = false } = options;
        if (!targetPath && !all) {
            console.log(chalk_1.default.red("❌ Please specify a --path or use --all"));
            return;
        }
        console.log(chalk_1.default.blue.bold(`🔄 Running ${type} migration...\n`));
        try {
            this.spinner.start();
            const files = await this.getTargetFiles(targetPath, all);
            const enhancementAgent = new (await Promise.resolve().then(() => __importStar(require("../agents/implementations/EnhancementAgent")))).EnhancementAgent();
            const aiCore = AICore_1.AICore.getInstance();
            const livingContext = await aiCore.getLivingContext();
            for (const file of files) {
                this.spinner.updateText(`Transforming ${path.basename(file)}...`);
                const content = await fs.readFile(file, "utf8");
                const result = await enhancementAgent.run({
                    currentComponent: content,
                    userPrompt: goal,
                    designContext: {
                        colors: [],
                        spacing: "relaxed",
                        typography: "modern",
                        interactions: ["hover", "focus"]
                    },
                    constraints: {
                        maxDependencies: 10,
                        targetFramework: "next",
                        uiLibrary: "shadcn"
                    }
                });
                await fs.writeFile(file, result.enhancedComponent);
            }
            this.spinner.succeed(`Migration ${type} completed for ${files.length} files!`);
        }
        catch (error) {
            this.spinner.fail(`Migration ${type} failed`);
            throw error;
        }
    }
    async getTargetFiles(targetPath, all) {
        if (targetPath) {
            const fullPath = path.resolve(process.cwd(), targetPath);
            if (await fs.pathExists(fullPath)) {
                const stats = await fs.stat(fullPath);
                if (stats.isFile())
                    return [fullPath];
                if (stats.isDirectory()) {
                    const files = await fs.readdir(fullPath);
                    return files.filter(f => /\.(tsx|ts|jsx|js)$/.test(f)).map(f => path.join(fullPath, f));
                }
            }
        }
        if (all) {
            // Logic to find all components in src/components
            const componentsDir = path.join(process.cwd(), "src", "components");
            if (await fs.pathExists(componentsDir)) {
                // Simple recursive find (placeholder for more robust logic)
                return this.walkSync(componentsDir).filter(f => /\.(tsx|ts|jsx|js)$/.test(f));
            }
        }
        return [];
    }
    walkSync(dir, filelist = []) {
        const files = fs.readdirSync(dir);
        files.forEach(file => {
            const filepath = path.join(dir, file);
            if (fs.statSync(filepath).isDirectory()) {
                filelist = this.walkSync(filepath, filelist);
            }
            else {
                filelist.push(filepath);
            }
        });
        return filelist;
    }
}
exports.MigrateTransformCommand = MigrateTransformCommand;
//# sourceMappingURL=migrate-transform.js.map