"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.ListCommand = void 0;
const spinner_1 = require("../utils/spinner");
const chalk_1 = __importDefault(require("chalk"));
class ListCommand {
    async execute(target, options) {
        const spinner = new spinner_1.EnhancedSpinner("Loading components...");
        try {
            // Local mode: list from filesystem without auth
            if (options.local) {
                await this.listLocal(spinner, options);
                return;
            }
            // Check authentication for remote listing
            const { AuthCommand } = await Promise.resolve().then(() => __importStar(require("./auth")));
            const authCommand = new AuthCommand();
            // if (!(await authCommand.requireAuth())) {
            //   console.log(chalk.yellow("🔐 Authentication required"));
            //   console.log(
            //     chalk.gray("Please run 'mycontext auth' to authenticate first")
            //   );
            //   return;
            // }
            // Get user info
            // const userInfo = await authCommand.getUserInfo();
            const userInfo = { userId: "local" };
            if (!userInfo) {
                console.log(chalk_1.default.red("❌ Authentication failed"));
                return;
            }
            console.log(chalk_1.default.green(`✅ Authenticated as ${userInfo.userId}`));
            // Determine what to list
            const listType = target || options.type || "components";
            switch (listType) {
                case "components":
                case "my-components":
                    await this.listComponents(userInfo.userId, options, spinner);
                    break;
                case "projects":
                    await this.listProjects(spinner);
                    break;
                case "files":
                    await this.listFiles(spinner);
                    break;
                case "all":
                    await this.listAll(userInfo.userId, options, spinner);
                    break;
                default:
                    throw new Error(`Unknown list type: ${listType}`);
            }
        }
        catch (error) {
            spinner.error({ text: "Listing failed" });
            throw error;
        }
    }
    async listLocal(spinner, options) {
        try {
            spinner.updateText("Scanning local components...");
            const fs = await Promise.resolve().then(() => __importStar(require("fs-extra")));
            const path = await Promise.resolve().then(() => __importStar(require("path")));
            const componentsDir = path.join(process.cwd(), "components", ".mycontext");
            const items = (await fs.readdir(componentsDir)).filter(async (d) => (await fs.stat(path.join(componentsDir, d))).isDirectory());
            const list = [];
            for (const dir of items) {
                const files = (await fs.readdir(path.join(componentsDir, dir))).filter((f) => f.endsWith(".tsx") && f !== "page.tsx");
                for (const f of files) {
                    list.push({
                        name: f.replace(/\.tsx$/, ""),
                        group: dir,
                        qualityScore: "N/A",
                    });
                }
            }
            spinner.success({ text: `Found ${list.length} components locally` });
            if ((options.format || "table") === "json") {
                console.log(JSON.stringify(list, null, 2));
            }
            else if (options.format === "simple") {
                list.forEach((c) => console.log(`${c.name} (${c.group})`));
            }
            else {
                this.displayComponentsTable(list);
            }
        }
        catch (e) {
            spinner.error({ text: "Local listing failed" });
            console.log(chalk_1.default.yellow(`⚠️  ${e}`));
        }
    }
    printNextCommands() {
        try {
            console.log("");
            console.log(chalk_1.default.blue("➡️ Next commands:"));
            console.log(chalk_1.default.gray("   Preview at: https://studio.mycontext.app"));
            console.log(chalk_1.default.gray("   mycontext generate-components all --with-tests"));
            console.log("");
        }
        catch { }
    }
    async listComponents(userId, options, spinner) {
        const API_BASE = process.env.MYCONTEXT_API_URL || "https://mycontext.fbien.com";
        spinner.updateText("Loading your components...");
        try {
            // Build query parameters
            const params = new URLSearchParams({
                userId,
                limit: (options.limit || 50).toString(),
                offset: "0",
            });
            if (options.group) {
                params.append("group", options.group);
            }
            const response = await fetch(`${API_BASE}/api/components/list?${params}`, {
                method: "GET",
                headers: {
                    "Content-Type": "application/json",
                },
            });
            const result = (await response.json());
            if (!result.success) {
                throw new Error(result.error || "Failed to load components");
            }
            const components = result.components || [];
            const pagination = result.pagination;
            spinner.success({ text: `Found ${components.length} components` });
            if (components.length === 0) {
                console.log(chalk_1.default.gray("No components found. Generate some components first!"));
                return;
            }
            // Display components based on format
            const format = options.format || "table";
            switch (format) {
                case "json":
                    console.log(JSON.stringify(components, null, 2));
                    break;
                case "simple":
                    components.forEach((comp) => {
                        console.log(`${comp.name} (${comp.group}) - Score: ${comp.qualityScore || "N/A"}`);
                    });
                    break;
                case "table":
                default:
                    this.displayComponentsTable(components);
                    break;
            }
            // Show pagination info
            if (pagination && pagination.total > components.length) {
                console.log(chalk_1.default.gray(`\nShowing ${components.length} of ${pagination.total} components`));
                if (pagination.hasMore) {
                    console.log(chalk_1.default.gray("Use --limit and --offset for pagination"));
                }
            }
            // Next commands
            this.printNextCommands();
        }
        catch (error) {
            console.log(chalk_1.default.yellow(`⚠️  Failed to load components: ${error}`));
        }
    }
    displayComponentsTable(components) {
        console.log(chalk_1.default.blue("\n📋 Your Components"));
        console.log(chalk_1.default.gray("─".repeat(80)));
        // Header
        console.log(chalk_1.default.bold(`${chalk_1.default.blue("Name")}${" ".repeat(20)}${chalk_1.default.blue("Group")}${" ".repeat(15)}${chalk_1.default.blue("Score")}${" ".repeat(8)}${chalk_1.default.blue("Created")}${" ".repeat(15)}${chalk_1.default.blue("Public")}`));
        console.log(chalk_1.default.gray("─".repeat(80)));
        // Components
        components.forEach((comp) => {
            const name = comp.name.padEnd(20);
            const group = (comp.group || "general").padEnd(15);
            const score = (comp.qualityScore || "N/A").toString().padEnd(8);
            const created = new Date(comp.createdAt).toLocaleDateString().padEnd(15);
            const isPublic = comp.isPublic ? "Yes" : "No";
            console.log(`${name}${group}${score}${created}${isPublic}`);
        });
        console.log(chalk_1.default.gray("─".repeat(80)));
    }
    async listProjects(spinner) {
        spinner.updateText("Loading projects...");
        const fs = await Promise.resolve().then(() => __importStar(require("fs-extra")));
        const path = await Promise.resolve().then(() => __importStar(require("path")));
        const cwd = process.cwd();
        const candidates = [
            path.join(cwd, "components", ".mycontext"),
            path.join(cwd, "components", "generated"),
            path.join(cwd, "components"),
        ];
        const projects = [];
        for (const dir of candidates) {
            if (!(await fs.pathExists(dir)))
                continue;
            const items = await fs.readdir(dir);
            for (const item of items) {
                const full = path.join(dir, item);
                const stat = await fs.stat(full);
                if (!stat.isDirectory())
                    continue;
                const files = (await fs.readdir(full)).filter((f) => f.endsWith(".tsx"));
                projects.push({
                    name: item,
                    description: `Local group at ${path.relative(cwd, full)}`,
                    components: files.length,
                });
            }
        }
        spinner.success({ text: `Found ${projects.length} projects` });
        console.log(chalk_1.default.blue("\n📁 Your Projects"));
        console.log(chalk_1.default.gray("─".repeat(60)));
        projects.forEach((project) => {
            console.log(`${chalk_1.default.bold(project.name)} - ${project.description}`);
            console.log(chalk_1.default.gray(`  Components: ${project.components}`));
        });
    }
    async listFiles(spinner) {
        spinner.updateText("Loading context files...");
        // Standardized context files
        const files = [
            { name: ".mycontext/01-prd.md", type: "Product Requirements", size: "" },
            { name: ".mycontext/02-types.ts", type: "TypeScript Types", size: "" },
            { name: ".mycontext/03-branding.md", type: "Brand Guidelines", size: "" },
            {
                name: ".mycontext/04-component-list.json",
                type: "Component List",
                size: "",
            },
        ];
        spinner.success({ text: `Found ${files.length} context files` });
        console.log(chalk_1.default.blue("\n📄 Context Files"));
        console.log(chalk_1.default.gray("─".repeat(60)));
        files.forEach((file) => {
            console.log(`${chalk_1.default.bold(file.name)} - ${file.type} (${file.size})`);
        });
    }
    async listAll(userId, options, spinner) {
        spinner.updateText("Loading all data...");
        // List everything
        console.log(chalk_1.default.blue("\n🎯 Complete Overview"));
        console.log(chalk_1.default.gray("=".repeat(60)));
        await this.listComponents(userId, options, spinner);
        console.log();
        await this.listProjects(spinner);
        console.log();
        await this.listFiles(spinner);
    }
}
exports.ListCommand = ListCommand;
//# sourceMappingURL=list.js.map