import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Loader2, Check } from "lucide-react";
import { useEditContext, useFieldsEditContext } from "../client/editContext";
import { SimpleTable } from "../ui/SimpleTable";
import { stripGuid } from "../utils";
export function Validation() {
    const editContext = useEditContext();
    const fieldsContext = useFieldsEditContext();
    const validationResults = editContext?.validationResult?.flatMap((x) => x.results.map((y) => ({
        ...y,
        language: x.item.language,
        version: x.item.version,
    })));
    if (!validationResults)
        return null;
    const nameBodyTemplate = (validationResult) => {
        const id = validationResult.itemId +
            validationResult.language +
            validationResult.version;
        return (_jsx("div", { className: "flex items-center gap-2", children: _jsxs("span", { className: `name-tooltip-${id} break-all`, style: { position: "relative" }, children: [validationResult.itemName, " ", _jsxs("span", { className: "text-neutral-grey-50", children: ["(", validationResult.language, ", ", validationResult.itemName, ")"] })] }) }));
    };
    const validationTemplate = (validationResult) => {
        return _jsx("div", { children: validationResult.message });
    };
    const columns = [
        { body: nameBodyTemplate, header: "Item" },
        {
            body: validationTemplate,
            align: "center",
            header: _jsx(Check, { className: "h-4 w-4" }),
        },
    ];
    if (validationResults.length === 0) {
        return (_jsx("div", { className: "text-neutral-grey-50 p-4 text-center text-xs", children: "No validation results" }));
    }
    return (_jsxs("div", { children: [editContext?.validating && (_jsx("div", { className: "p-3 text-center", children: _jsx(Loader2, { className: "h-5 w-5 animate-spin" }) })), !editContext?.validating && (_jsx(SimpleTable, { "data-testid": "validation-results-table", items: validationResults, columns: columns, onRowClick: async ({ item: result }) => {
                    if (!editContext)
                        return;
                    const itemDescriptor = {
                        id: result.itemId,
                        language: result.language,
                        version: result.version,
                    };
                    const currentItem = editContext.currentItemDescriptor;
                    const isCurrentItem = !!currentItem &&
                        stripGuid(currentItem?.id ?? "") ===
                            stripGuid(itemDescriptor.id) &&
                        currentItem.language === itemDescriptor.language &&
                        currentItem.version === itemDescriptor.version;
                    if (!isCurrentItem) {
                        const loadedItem = await editContext.loadItem(itemDescriptor);
                        if (!loadedItem)
                            return;
                    }
                    if (result.fieldId) {
                        await fieldsContext?.setFocusedField({
                            item: itemDescriptor,
                            fieldId: result.fieldId,
                        }, false);
                    }
                    else {
                        editContext.select?.([result.itemId]);
                    }
                } }))] }));
}
//# sourceMappingURL=Validation.js.map