const SETTINGS_TABLE = 'workspace_settings';
const MONDAY_CONFIG_KEYS = {
    apiToken: 'monday.api_token',
    boardId: 'monday.board_id',
    boardName: 'monday.board_name',
    accountName: 'monday.account_name',
};
function getSetting(db, key) {
    const row = db
        .prepare(`SELECT value FROM ${SETTINGS_TABLE} WHERE key = ?`)
        .get(key);
    return row?.value ?? null;
}
function setSetting(db, key, value) {
    db.prepare(`
    INSERT INTO ${SETTINGS_TABLE} (key, value)
    VALUES (?, ?)
    ON CONFLICT(key) DO UPDATE SET value = excluded.value
  `).run(key, value);
}
function deleteSetting(db, key) {
    db.prepare(`DELETE FROM ${SETTINGS_TABLE} WHERE key = ?`).run(key);
}
export function isMondayConfigured(db) {
    return getSetting(db, MONDAY_CONFIG_KEYS.apiToken) !== null;
}
export function loadMondayConfig(db) {
    const apiToken = getSetting(db, MONDAY_CONFIG_KEYS.apiToken);
    if (!apiToken)
        return null;
    return {
        apiToken,
        boardId: getSetting(db, MONDAY_CONFIG_KEYS.boardId) ?? undefined,
        boardName: getSetting(db, MONDAY_CONFIG_KEYS.boardName) ?? undefined,
        accountName: getSetting(db, MONDAY_CONFIG_KEYS.accountName) ?? undefined,
    };
}
export function saveMondayApiToken(db, apiToken) {
    setSetting(db, MONDAY_CONFIG_KEYS.apiToken, apiToken);
}
export function saveMondayBoard(db, boardId, boardName) {
    setSetting(db, MONDAY_CONFIG_KEYS.boardId, boardId);
    setSetting(db, MONDAY_CONFIG_KEYS.boardName, boardName);
}
export function saveMondayAccountName(db, accountName) {
    setSetting(db, MONDAY_CONFIG_KEYS.accountName, accountName);
}
export function clearMondayConfig(db) {
    for (const key of Object.values(MONDAY_CONFIG_KEYS)) {
        deleteSetting(db, key);
    }
}
export function getMondayApiToken(db) {
    const envToken = process.env.PRLT_MONDAY_API_TOKEN || process.env.MONDAY_API_TOKEN;
    if (envToken)
        return envToken;
    return getSetting(db, MONDAY_CONFIG_KEYS.apiToken);
}
export function getMondayBoardId(db) {
    const envBoardId = process.env.PRLT_MONDAY_BOARD_ID || process.env.MONDAY_BOARD_ID;
    if (envBoardId)
        return envBoardId;
    return getSetting(db, MONDAY_CONFIG_KEYS.boardId);
}
//# sourceMappingURL=config.js.map