import { db } from "../../db.js";
import { logger } from "../../utils/logger.js";
import { SUBMISSION_CONFIG_ENTITY_TYPE, } from "./types.js";
const VALID_MODES = new Set([
    "closed",
    "read_only",
    "submit_only",
    "submitter_scoped",
    "open",
]);
function parseMirrors(raw) {
    if (!Array.isArray(raw))
        return [];
    const out = [];
    for (const row of raw) {
        if (!row || typeof row !== "object")
            continue;
        const o = row;
        const provider = o.provider;
        const config = o.config;
        if ((provider === "github" || provider === "linear" || provider === "custom_webhook") &&
            config &&
            typeof config === "object") {
            out.push({ provider, config: config });
        }
    }
    return out;
}
function parseRecord(entityId, snapshot) {
    const config_key = snapshot.config_key;
    const target_entity_type = snapshot.target_entity_type;
    if (typeof config_key !== "string" || typeof target_entity_type !== "string")
        return null;
    const ap = snapshot.access_policy;
    const mode = typeof ap === "string" && VALID_MODES.has(ap)
        ? ap
        : "closed";
    const active = snapshot.active !== false;
    return {
        entity_id: entityId,
        config_key,
        target_entity_type,
        access_policy: mode,
        active,
        enable_conversation_threading: Boolean(snapshot.enable_conversation_threading),
        enable_guest_read_back: Boolean(snapshot.enable_guest_read_back),
        external_mirrors: parseMirrors(snapshot.external_mirrors),
    };
}
/**
 * Load the active submission_config row for a target entity type (first match).
 */
export async function getSubmissionConfigForTargetType(targetEntityType) {
    const { data: rows, error } = await db
        .from("entity_snapshots")
        .select("entity_id, snapshot")
        .eq("entity_type", SUBMISSION_CONFIG_ENTITY_TYPE);
    if (error) {
        logger.warn("[submission_config] load failed", { message: error.message });
        return null;
    }
    for (const row of rows ?? []) {
        const snap = row.snapshot;
        const parsed = parseRecord(row.entity_id, snap);
        if (parsed?.target_entity_type === targetEntityType && parsed.active) {
            return parsed;
        }
    }
    return null;
}
//# sourceMappingURL=submission_config_loader.js.map