import { getSessionGrantRow, listSessionGrantRows, upsertSessionGrantRow } from "./state/kit-session-grants-sqlite.js";
export const SESSION_POLICY_SCHEMA_VERSION = 1;
export function resolveSessionId(env) {
    const raw = env.WORKSPACE_KIT_SESSION_ID?.trim();
    return raw && raw.length > 0 ? raw : "default";
}
export async function loadSessionPolicyDocument(workspacePath, effectiveConfig) {
    const sessionId = resolveSessionId(process.env);
    const rows = listSessionGrantRows(workspacePath, effectiveConfig, sessionId);
    const grants = {};
    for (const row of rows) {
        grants[row.operationId] = { rationale: row.rationale, grantedAt: row.grantedAt };
    }
    return {
        schemaVersion: SESSION_POLICY_SCHEMA_VERSION,
        sessionId,
        grants
    };
}
export async function saveSessionPolicyDocument(_workspacePath, _doc) {
    throw new Error("saveSessionPolicyDocument is deprecated; use recordSessionGrant instead.");
}
export async function getSessionGrant(workspacePath, operationId, sessionId, effectiveConfig) {
    return getSessionGrantRow(workspacePath, operationId, sessionId, effectiveConfig);
}
export async function recordSessionGrant(workspacePath, operationId, sessionId, rationale, effectiveConfig) {
    upsertSessionGrantRow(workspacePath, operationId, sessionId, rationale, effectiveConfig);
}
