"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const BaseCommand_1 = require("../../BaseCommand");
const core_1 = require("@oclif/core");
const chrono = tslib_1.__importStar(require("chrono-node"));
class Update extends BaseCommand_1.BaseCommand {
    async run() {
        const updates = {
            id: this.lookups.card,
        };
        if (this.flags.name) {
            updates.name = this.flags.name;
        }
        if (this.flags.description !== undefined) {
            updates.desc = this.flags.description;
        }
        if (this.flags["clear-due"]) {
            updates.due = "";
        }
        else if (this.flags.due) {
            const parsed = chrono.parseDate(this.flags.due);
            if (parsed) {
                updates.due = parsed.toString();
            }
            else {
                this.warn(`Could not parse due date: "${this.flags.due}"`);
                this.exit(1);
            }
        }
        // Only id is set — nothing to update
        if (Object.keys(updates).length <= 1) {
            this.warn("No update flags provided. Use --name, --description, --due, or --clear-due.");
            this.exit(1);
        }
        const card = await this.client.cards.updateCard(updates);
        this.output(card);
    }
    toData(data) {
        return {
            id: data.id,
            name: data.name,
            due: data.due,
            description: data.desc,
            labels: data.labels,
            url: data.url,
        };
    }
    async format(data) {
        var _a;
        const labels = ((_a = data.labels) === null || _a === void 0 ? void 0 : _a.map((l) => l.name).filter(Boolean).join(", ")) ||
            "None";
        return [
            `Name: ${data.name}`,
            `ID: ${data.id}`,
            `URL: ${data.url}`,
            `Due: ${data.due || "None"}`,
            `Labels: ${labels}`,
            `Description: ${data.description || "None"}`,
        ].join("\n");
    }
}
Update.description = "Update a card";
Update.flags = {
    card: core_1.Flags.string({ required: true }),
    board: core_1.Flags.string({ required: true }),
    list: core_1.Flags.string({ required: true }),
    name: core_1.Flags.string({ char: "n", description: "New name for the card" }),
    description: core_1.Flags.string(),
    due: core_1.Flags.string(),
    "clear-due": core_1.Flags.boolean({
        description: "Remove the due date",
        default: false,
    }),
};
exports.default = Update;
