"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const BaseCommand_1 = require("../../BaseCommand");
const core_1 = require("@oclif/core");
class Search extends BaseCommand_1.BaseCommand {
    constructor() {
        super(...arguments);
        this.defaultOutput = "fancy";
    }
    async run() {
        const params = {
            query: this.flags.query,
        };
        if (this.flags.board) {
            params.idBoards = this.lookups.board;
        }
        if (this.flags.type) {
            params.modelTypes = this.flags.type;
        }
        const results = await this.client.search.getSearch(params);
        this.output(results);
    }
    toData(data) {
        return {
            cards: (data.cards || []).map((c) => ({
                id: c.id,
                name: c.name,
                url: c.url,
                board: c.idBoard,
            })),
            boards: (data.boards || []).map((b) => ({
                id: b.id,
                name: b.name,
                url: b.url,
            })),
        };
    }
    async format(data) {
        const sections = [];
        if (data.boards.length > 0) {
            const boardLines = data.boards
                .map((b) => `  ${b.name} (ID: ${b.id})`)
                .join("\n");
            sections.push(`Boards:\n${boardLines}`);
        }
        if (data.cards.length > 0) {
            const cardLines = data.cards
                .map((c) => `  ${c.name} (ID: ${c.id})`)
                .join("\n");
            sections.push(`Cards:\n${cardLines}`);
        }
        if (sections.length === 0) {
            return "No results found";
        }
        return sections.join("\n\n");
    }
}
Search.description = "Search Trello";
Search.flags = {
    query: core_1.Flags.string({ required: true }),
    board: core_1.Flags.string(),
    type: core_1.Flags.option({
        options: ["cards", "boards", "organizations"],
    })(),
};
exports.default = Search;
