"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const BaseCommand_1 = require("../../BaseCommand");
const core_1 = require("@oclif/core");
class LabelUpdate extends BaseCommand_1.BaseCommand {
    async run() {
        const labels = await this.cache.getLabelsByBoardAndColor(this.lookups.board, this.flags.color);
        let result;
        if (labels.length === 0) {
            // No label with this color exists — create one
            result = await this.client.boards.createBoardLabel({
                id: this.lookups.board,
                name: this.flags.name,
                color: this.flags.color,
            });
        }
        else if (labels.length === 1) {
            // Exactly one match — update it
            result = await this.client.labels.updateLabel({
                id: labels[0].id,
                name: this.flags.name,
            });
        }
        else {
            // Multiple labels with the same color — need --old-name to disambiguate
            if (!this.flags["old-name"]) {
                throw new Error(`Multiple labels with color [${this.flags.color}] found on board. Use --old-name to disambiguate.`);
            }
            const match = labels.filter((l) => l.name === this.flags["old-name"]);
            if (match.length === 0) {
                throw new Error(`No label with color [${this.flags.color}] and name [${this.flags["old-name"]}] found on board.`);
            }
            if (match.length > 1) {
                throw new Error(`Multiple labels with color [${this.flags.color}] and name [${this.flags["old-name"]}] found on board.`);
            }
            result = await this.client.labels.updateLabel({
                id: match[0].id,
                name: this.flags.name,
            });
        }
        await this.cache.sync();
        this.output(result);
    }
    toData(data) {
        return {
            id: data.id,
            name: data.name,
            color: data.color,
        };
    }
}
LabelUpdate.description = "Update a label's text. Creates the label if no matching color exists on the board.";
LabelUpdate.flags = {
    board: core_1.Flags.string({ required: true }),
    color: core_1.Flags.option({
        required: true,
        options: [
            "green",
            "yellow",
            "orange",
            "red",
            "purple",
            "blue",
            "sky",
            "lime",
            "pink",
            "black",
        ],
    })(),
    name: core_1.Flags.string({
        char: "n",
        required: true,
        description: "New label name",
    }),
    "old-name": core_1.Flags.string({
        description: "Existing label name to match when multiple labels share the same color",
    }),
};
exports.default = LabelUpdate;
