import { useState } from "react"; import type { WidgetProps } from "./types"; import type { SchemaTable } from "../api/types"; import { SchemaColumnsDialog } from "./SchemaColumnsDialog"; import { ReservedControls } from "./ReservedControls"; export function SchemaLinkingGateWidget({ descriptor, onRespond }: WidgetProps) { const tables: SchemaTable[] = descriptor.tables ?? []; const [enacted, setEnacted] = useState>( () => new Set(tables.filter((t) => t.recommended).map((t) => t.id)) ); const [selected, setSelected] = useState>>(() => { const m = new Map>(); for (const t of tables) { m.set( t.id, new Set(t.kind === "promote" ? t.columns.filter((c) => c.suggested).map((c) => c.name) : []) ); } return m; }); const [openId, setOpenId] = useState(null); function toggleEnact(id: string) { setEnacted((prev) => { const next = new Set(prev); if (next.has(id)) next.delete(id); else next.add(id); return next; }); } function toggleColumn(tableId: string, column: string) { setSelected((prev) => { const next = new Map(prev); const set = new Set(next.get(tableId) ?? []); if (set.has(column)) set.delete(column); else set.add(column); next.set(tableId, set); return next; }); } function confirm() { const payload = tables.map((t) => { const on = enacted.has(t.id); if (on && t.kind === "promote") { const sel = selected.get(t.id) ?? new Set(); return { id: t.id, enacted: true, columns: t.columns.filter((c) => sel.has(c.name)).map((c) => c.name) }; } return { id: t.id, enacted: on }; }); onRespond({ id: descriptor.id, kind: "schema-linking", tables: payload }); } const openTable = tables.find((t) => t.id === openId) ?? null; return (
{descriptor.title && (

{descriptor.title}

)}
{tables.map((t) => { const on = enacted.has(t.id); const count = selected.get(t.id)?.size ?? 0; return (
toggleEnact(t.id)} aria-label={t.name} />
{t.name} {t.kind === "promote" ? "Promuovi" : "Escludi"}
{t.description &&

{t.description}

} {t.rationale &&

{t.rationale}

}
); })}
{openTable && ( toggleColumn(openTable.id, c)} onClose={() => setOpenId(null)} /> )}
onRespond({ id: descriptor.id, control: c, ...(t !== undefined ? { text: t } : {}) })} />
); }