"use client";
import { cn } from "./index3.js";
import { useAIInteraction } from "./index407.js";
import { Checkbox } from "./index424.js";
import { TableBody, TableCell, TableHead, TableHeader, TableNamespace, TableRow } from "./index440.js";
import * as React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/components/organisms/ai/AITable/AITable.tsx
/**
* Get the row ID from a data row
* Uses 'id' field if available, otherwise falls back to first column key
*/
function getRowId(row, columns) {
	if (row.id !== void 0) return String(row.id);
	if (columns.length > 0) {
		const firstKey = columns[0].key;
		return String(row[firstKey] ?? "");
	}
	return "";
}
/**
* AITable - Renders interactive data tables from JSON schema.
*
* Supports row selection with checkboxes and emits interaction events
* through the AIInteractionContext when selections change.
*/
function AITable({ schema, className }) {
	const { onInteraction } = useAIInteraction();
	const [selectedRows, setSelectedRows] = React.useState(/* @__PURE__ */ new Set());
	const { id, title, columns, data, selectable = false } = schema;
	const allRowIds = React.useMemo(() => data.map((row) => getRowId(row, columns)), [data, columns]);
	const emitSelectionChange = React.useCallback((newSelectedRows) => {
		onInteraction?.({
			componentId: id,
			componentType: "table",
			action: "select",
			value: { selectedRows: newSelectedRows },
			timestamp: Date.now()
		});
	}, [id, onInteraction]);
	const handleRowSelect = React.useCallback((rowId, checked) => {
		setSelectedRows((prev) => {
			const next = new Set(prev);
			if (checked) next.add(rowId);
			else next.delete(rowId);
			emitSelectionChange(Array.from(next));
			return next;
		});
	}, [emitSelectionChange]);
	const handleSelectAll = React.useCallback((checked) => {
		if (checked) {
			setSelectedRows(new Set(allRowIds));
			emitSelectionChange(allRowIds);
		} else {
			setSelectedRows(/* @__PURE__ */ new Set());
			emitSelectionChange([]);
		}
	}, [allRowIds, emitSelectionChange]);
	const allSelected = allRowIds.length > 0 && selectedRows.size === allRowIds.length;
	const someSelected = selectedRows.size > 0 && selectedRows.size < allRowIds.length;
	if (!data || data.length === 0) return /* @__PURE__ */ jsx("div", {
		"data-slot": "ai-table",
		role: "status",
		className: cn("flex items-center justify-center rounded-lg border border-border bg-muted p-8 text-muted-foreground", className),
		children: "No data available"
	});
	return /* @__PURE__ */ jsxs("div", {
		"data-slot": "ai-table",
		className: cn("flex flex-col gap-2", className),
		children: [
			title && /* @__PURE__ */ jsx("h3", {
				className: "text-sm font-medium text-foreground",
				children: title
			}),
			selectable && /* @__PURE__ */ jsx("div", {
				className: "sr-only",
				"aria-live": "polite",
				"aria-atomic": "true",
				children: selectedRows.size > 0 ? `${selectedRows.size} of ${allRowIds.length} rows selected` : "No rows selected"
			}),
			/* @__PURE__ */ jsxs(TableNamespace, {
				"aria-label": title ?? "Interactive data table",
				children: [/* @__PURE__ */ jsx(TableHeader, { children: /* @__PURE__ */ jsxs(TableRow, { children: [selectable && /* @__PURE__ */ jsx(TableHead, {
					className: "w-[40px]",
					children: /* @__PURE__ */ jsx(Checkbox, {
						checked: allSelected,
						indeterminate: someSelected,
						onCheckedChange: handleSelectAll,
						"aria-label": "Select all rows"
					})
				}), columns.map((column) => /* @__PURE__ */ jsx(TableHead, {
					style: { width: typeof column.width === "number" ? `${column.width}px` : column.width },
					children: column.label
				}, column.key))] }) }), /* @__PURE__ */ jsx(TableBody, { children: data.map((row) => {
					const rowId = getRowId(row, columns);
					const isSelected = selectedRows.has(rowId);
					return /* @__PURE__ */ jsxs(TableRow, {
						"data-state": isSelected ? "selected" : void 0,
						className: "transition-colors",
						children: [selectable && /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(Checkbox, {
							checked: isSelected,
							onCheckedChange: (checked) => handleRowSelect(rowId, checked === true),
							"aria-label": `Select row ${rowId}`
						}) }), columns.map((column) => /* @__PURE__ */ jsx(TableCell, { children: String(row[column.key] ?? "") }, column.key))]
					}, rowId);
				}) })]
			})
		]
	});
}
AITable.displayName = "AITable";
//#endregion
export { AITable as default };

//# sourceMappingURL=index441.js.map