/**
 * Linear Configuration Storage
 *
 * Stores Linear credentials and preferences in the workspace_settings table.
 */
const SETTINGS_TABLE = 'workspace_settings';
// Config keys stored in workspace_settings table
const LINEAR_CONFIG_KEYS = {
    apiKey: 'linear.api_key',
    defaultTeamId: 'linear.default_team_id',
    defaultTeamKey: 'linear.default_team_key',
    organizationName: 'linear.organization_name',
};
/**
 * Get a setting value from the database.
 */
function getSetting(db, key) {
    const row = db
        .prepare(`SELECT value FROM ${SETTINGS_TABLE} WHERE key = ?`)
        .get(key);
    return row?.value ?? null;
}
/**
 * Set a setting value in the database.
 */
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);
}
/**
 * Delete a setting from the database.
 */
function deleteSetting(db, key) {
    db.prepare(`DELETE FROM ${SETTINGS_TABLE} WHERE key = ?`).run(key);
}
// =============================================================================
// Public API
// =============================================================================
/**
 * Check if Linear is configured (API key is stored).
 */
export function isLinearConfigured(db) {
    return getSetting(db, LINEAR_CONFIG_KEYS.apiKey) !== null;
}
/**
 * Load Linear configuration from the database.
 * Returns null if not configured.
 */
export function loadLinearConfig(db) {
    const apiKey = getSetting(db, LINEAR_CONFIG_KEYS.apiKey);
    if (!apiKey)
        return null;
    return {
        apiKey,
        defaultTeamId: getSetting(db, LINEAR_CONFIG_KEYS.defaultTeamId) ?? undefined,
        defaultTeamKey: getSetting(db, LINEAR_CONFIG_KEYS.defaultTeamKey) ?? undefined,
        organizationName: getSetting(db, LINEAR_CONFIG_KEYS.organizationName) ?? undefined,
    };
}
/**
 * Save Linear API key to the database.
 */
export function saveLinearApiKey(db, apiKey) {
    setSetting(db, LINEAR_CONFIG_KEYS.apiKey, apiKey);
}
/**
 * Save the default team for Linear operations.
 */
export function saveLinearDefaultTeam(db, teamId, teamKey) {
    setSetting(db, LINEAR_CONFIG_KEYS.defaultTeamId, teamId);
    setSetting(db, LINEAR_CONFIG_KEYS.defaultTeamKey, teamKey);
}
/**
 * Save the organization name.
 */
export function saveLinearOrganization(db, name) {
    setSetting(db, LINEAR_CONFIG_KEYS.organizationName, name);
}
/**
 * Clear all Linear configuration from the database.
 */
export function clearLinearConfig(db) {
    for (const key of Object.values(LINEAR_CONFIG_KEYS)) {
        deleteSetting(db, key);
    }
}
/**
 * Get the stored Linear API key.
 * Also checks PRLT_LINEAR_API_KEY and LINEAR_API_KEY environment variables.
 */
export function getLinearApiKey(db) {
    // Environment variables take precedence
    const envKey = process.env.PRLT_LINEAR_API_KEY || process.env.LINEAR_API_KEY;
    if (envKey)
        return envKey;
    return getSetting(db, LINEAR_CONFIG_KEYS.apiKey);
}
//# sourceMappingURL=config.js.map