"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const db_1 = __importDefault(require("./db"));
const setup_1 = __importDefault(require("./setup"));
const sync_1 = __importDefault(require("./sync"));
class default_1 {
    constructor(configDir, appKey, token) {
        this.db = new db_1.default(configDir, "trello");
        this.appKey = appKey;
        this.token = token;
    }
    async bootstrap() {
        const r = new setup_1.default(this.db);
        return r.bootstrap();
    }
    async reset() {
        const r = new setup_1.default(this.db);
        return r.teardown();
    }
    async sync() {
        this.reset();
        const sync = new sync_1.default(this.db, this.appKey, this.token);
        return sync.run();
    }
    async getBoard(id) {
        const r = this.db.get("SELECT name FROM boards WHERE id=?", [id]);
        if (!r) {
            throw new Error(`Board [${id}] not found`);
        }
        return r.name;
    }
    async getList(id) {
        const r = this.db.get("SELECT name FROM lists WHERE id=?", [id]);
        if (!r) {
            throw new Error(`List [${id}] not found`);
        }
        return r.name;
    }
    async getBoardIdByName(name) {
        const r = this.db.get("SELECT id FROM boards WHERE name=?", [name]);
        if (!r) {
            throw new Error(`Board [${name}] not found`);
        }
        return r.id;
    }
    async getListIdByBoardAndName(board, name) {
        const r = this.db.get("SELECT id FROM lists WHERE boardId=? AND (name=? OR id=?)", [board, name, name]);
        if (!r) {
            throw new Error(`List [${name}] not found on board [${board}]`);
        }
        return r.id;
    }
    async getLabelIdByName(name) {
        const r = this.db.get("SELECT id FROM labels WHERE name=? OR id=?", [
            name,
            name,
        ]);
        if (!r) {
            throw new Error(`Label [${name}] not found`);
        }
        return r.id;
    }
    async getLabelsByBoardAndColor(boardId, color, name) {
        return name
            ? this.db.all("SELECT id, name FROM labels WHERE boardId=? AND color=? AND name=?", [boardId, color, name])
            : this.db.all("SELECT id, name FROM labels WHERE boardId=? AND color=?", [boardId, color]);
    }
    async getLabelIdByBoardAndColor(boardId, color, name) {
        const rows = name
            ? this.db.all("SELECT id FROM labels WHERE boardId=? AND color=? AND name=?", [boardId, color, name])
            : this.db.all("SELECT id FROM labels WHERE boardId=? AND color=?", [boardId, color]);
        if (!rows || rows.length === 0) {
            throw new Error(`Label with color [${color}] not found on board`);
        }
        if (rows.length > 1) {
            throw new Error(`Multiple labels with color [${color}] found on board. Use --text to disambiguate.`);
        }
        return rows[0].id;
    }
    async getUserIdByName(name) {
        const r = this.db.get("SELECT id FROM members WHERE username=?", [name]);
        if (!r) {
            return "";
        }
        return r.id;
    }
    async convertMemberIdsToEntity(ids) {
        const placeholder = ids.map((i) => "?").join(",");
        return this.db.all(`SELECT * FROM members WHERE id IN (${placeholder})`, ids);
    }
    async getListsOnBoard(id) {
        const r = this.db.all("SELECT id FROM lists WHERE boardId=?", [id]);
        return r.map((n) => n.id);
    }
}
exports.default = default_1;
//# sourceMappingURL=index.js.map