export type BuiltMessageState = "ready" | "needs_review" | "blocked" | "excluded"; export type BuiltMessageStatePresentation = { state: BuiltMessageState; explanationLabel: string; }; function record(value: unknown): Record { return value && typeof value === "object" && !Array.isArray(value) ? value as Record : {}; } /** One display/filter model; it never authorizes an exception or a delivery. */ export function builtMessageState( row: Record, index: number, reviewedKeys: ReadonlySet, inspectionComplete = false ): BuiltMessageStatePresentation { const validation = String(row.validation_status ?? "").toLowerCase(); if (validation === "excluded" || validation === "inactive") { return { state: "excluded", explanationLabel: validation === "inactive" ? "i18n:govoplan-campaign.message_state_inactive" : "i18n:govoplan-campaign.message_state_excluded" }; } const build = String(row.build_status ?? "built").toLowerCase(); if (build !== "built") { return { state: "blocked", explanationLabel: ["failed", "error"].includes(build) ? "i18n:govoplan-campaign.message_state_build_failed" : "i18n:govoplan-campaign.message_state_not_built" }; } const issues = Array.isArray(row.issues) ? row.issues.map(record) : []; if (validation === "blocked" || issues.some((issue) => ["block", "blocked"].includes(String(issue.behavior ?? "").toLowerCase()) || ["error", "critical", "fatal"].includes(String(issue.severity ?? "").toLowerCase()))) { return { state: "blocked", explanationLabel: "i18n:govoplan-campaign.message_state_blocked" }; } if (validation === "warning") { // An earlier view/open marker does not replace the final warning gate. return inspectionComplete ? { state: "ready", explanationLabel: "i18n:govoplan-campaign.message_state_warning_accepted" } : { state: "needs_review", explanationLabel: "i18n:govoplan-campaign.message_state_warning_pending" }; } if (validation === "needs_review") { const key = String(row.review_key ?? row.entry_id ?? row.entry_index ?? index); const accepted = row.reviewed === true || reviewedKeys.has(key); return accepted ? { state: "ready", explanationLabel: "i18n:govoplan-campaign.message_state_individually_accepted" } : { state: "needs_review", explanationLabel: "i18n:govoplan-campaign.message_state_individual_pending" }; } if (validation === "ready") { return { state: "ready", explanationLabel: "i18n:govoplan-campaign.message_state_automatic" }; } return { state: "blocked", explanationLabel: "i18n:govoplan-campaign.message_state_unknown" }; }