"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 Create extends BaseCommand_1.BaseCommand {
    async run() {
        this.flags.label = this.flags.label || [];
        // Convert label name to ID
        const idLabels = await Promise.all(this.flags.label.map(async (l) => {
            return this.cache.getLabelIdByName(l);
        }));
        let dueDate = null;
        if (this.flags.due) {
            const parsed = chrono.parseDate(this.flags.due);
            if (parsed) {
                dueDate = parsed.toString();
            }
            else {
                this.warn(`Could not parse due date: "${this.flags.due}"`);
                this.exit(1);
            }
        }
        const card = await this.client.cards.createCard({
            idList: this.lookups.list,
            name: this.flags.name,
            desc: this.flags.description,
            due: dueDate,
            pos: this.flags.position,
            idLabels,
        });
        this.output(card);
    }
    toData(data) {
        return {
            id: data.id,
            name: data.name,
            due: data.due,
            description: data.desc,
            labels: data.labels,
            url: data.url,
        };
    }
}
Create.description = "Create a card";
Create.flags = {
    name: core_1.Flags.string({ char: "n", required: true }),
    board: core_1.Flags.string({ required: true }),
    list: core_1.Flags.string({ required: true }),
    position: core_1.Flags.option({ options: ["top", "bottom"], default: "bottom" })(),
    label: core_1.Flags.string({ multiple: true }),
    due: core_1.Flags.string(),
    description: core_1.Flags.string(),
};
exports.default = Create;
