const SETTINGS_TABLE = 'workspace_settings';
const ASANA_CONFIG_KEYS = {
    accessToken: 'asana.access_token',
    workspaceGid: 'asana.workspace_gid',
    workspaceName: 'asana.workspace_name',
    projectGid: 'asana.project_gid',
    projectName: 'asana.project_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 isAsanaConfigured(db) {
    return getSetting(db, ASANA_CONFIG_KEYS.accessToken) !== null;
}
export function loadAsanaConfig(db) {
    const accessToken = getSetting(db, ASANA_CONFIG_KEYS.accessToken);
    if (!accessToken)
        return null;
    return {
        accessToken,
        workspaceGid: getSetting(db, ASANA_CONFIG_KEYS.workspaceGid) ?? undefined,
        workspaceName: getSetting(db, ASANA_CONFIG_KEYS.workspaceName) ?? undefined,
        projectGid: getSetting(db, ASANA_CONFIG_KEYS.projectGid) ?? undefined,
        projectName: getSetting(db, ASANA_CONFIG_KEYS.projectName) ?? undefined,
    };
}
export function saveAsanaAccessToken(db, accessToken) {
    setSetting(db, ASANA_CONFIG_KEYS.accessToken, accessToken);
}
export function saveAsanaWorkspace(db, workspaceGid, workspaceName) {
    setSetting(db, ASANA_CONFIG_KEYS.workspaceGid, workspaceGid);
    setSetting(db, ASANA_CONFIG_KEYS.workspaceName, workspaceName);
}
export function saveAsanaProject(db, projectGid, projectName) {
    setSetting(db, ASANA_CONFIG_KEYS.projectGid, projectGid);
    setSetting(db, ASANA_CONFIG_KEYS.projectName, projectName);
}
export function clearAsanaConfig(db) {
    for (const key of Object.values(ASANA_CONFIG_KEYS)) {
        deleteSetting(db, key);
    }
}
export function getAsanaAccessToken(db) {
    const envToken = process.env.PRLT_ASANA_ACCESS_TOKEN || process.env.ASANA_ACCESS_TOKEN;
    if (envToken)
        return envToken;
    return getSetting(db, ASANA_CONFIG_KEYS.accessToken);
}
//# sourceMappingURL=config.js.map