/**
 * Shortcut Configuration Storage
 *
 * Stores Shortcut credentials and preferences in the workspace_settings table.
 * Mirrors the Jira config module pattern.
 */
import { loadProviderSources, resolveApiKey } from '../work-source/provider-sources.js';
const SETTINGS_TABLE = 'workspace_settings';
const SHORTCUT_CONFIG_KEYS = {
    apiToken: 'shortcut.api_token',
    workspaceSlug: 'shortcut.workspace_slug',
};
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);
}
/**
 * Check if Shortcut is configured.
 * Returns true if either:
 * - Database has shortcut.api_token stored, OR
 * - Environment variables PRLT_SHORTCUT_API_TOKEN/SHORTCUT_API_TOKEN are set
 */
export function isShortcutConfigured(db) {
    const hasDbConfig = getSetting(db, SHORTCUT_CONFIG_KEYS.apiToken) !== null;
    if (hasDbConfig)
        return true;
    const hasEnvToken = !!(process.env.PRLT_SHORTCUT_API_TOKEN || process.env.SHORTCUT_API_TOKEN);
    return hasEnvToken;
}
/**
 * Load Shortcut configuration from the database + environment.
 * Returns null if not configured.
 */
export function loadShortcutConfig(db) {
    const apiToken = getSetting(db, SHORTCUT_CONFIG_KEYS.apiToken)
        || process.env.PRLT_SHORTCUT_API_TOKEN
        || process.env.SHORTCUT_API_TOKEN;
    if (!apiToken)
        return null;
    return {
        apiToken,
        workspaceSlug: getSetting(db, SHORTCUT_CONFIG_KEYS.workspaceSlug)
            || process.env.PRLT_SHORTCUT_WORKSPACE
            || process.env.SHORTCUT_WORKSPACE_SLUG
            || undefined,
    };
}
/**
 * Save Shortcut configuration to the database.
 */
export function saveShortcutConfig(db, config) {
    setSetting(db, SHORTCUT_CONFIG_KEYS.apiToken, config.apiToken);
    if (config.workspaceSlug) {
        setSetting(db, SHORTCUT_CONFIG_KEYS.workspaceSlug, config.workspaceSlug);
    }
}
/**
 * Save the Shortcut API token.
 */
export function saveShortcutApiToken(db, apiToken) {
    setSetting(db, SHORTCUT_CONFIG_KEYS.apiToken, apiToken);
}
/**
 * Save the Shortcut workspace slug.
 */
export function saveShortcutWorkspaceSlug(db, slug) {
    setSetting(db, SHORTCUT_CONFIG_KEYS.workspaceSlug, slug);
}
/**
 * Clear all Shortcut configuration from the database.
 */
export function clearShortcutConfig(db) {
    for (const key of Object.values(SHORTCUT_CONFIG_KEYS)) {
        deleteSetting(db, key);
    }
}
/**
 * Get the stored Shortcut API token.
 * Also checks PRLT_SHORTCUT_API_TOKEN and SHORTCUT_API_TOKEN environment variables.
 */
export function getShortcutApiToken(db) {
    // 1. Try provider sources (supports custom apiKeyRef per source)
    try {
        const sources = loadProviderSources(db);
        for (const source of sources) {
            if (source.provider === 'shortcut') {
                const key = resolveApiKey(db, source);
                if (key)
                    return key;
            }
        }
    }
    catch {
        // Provider sources may not exist in older databases
    }
    // 2. Legacy: environment variables
    const envKey = process.env.PRLT_SHORTCUT_API_TOKEN || process.env.SHORTCUT_API_TOKEN;
    if (envKey)
        return envKey;
    // 3. Legacy: stored workspace setting
    return getSetting(db, SHORTCUT_CONFIG_KEYS.apiToken);
}
//# sourceMappingURL=config.js.map