"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationSummary = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * Validation Summary Component
 * Provides a detailed report of workflow validation results
 */
const react_1 = __importDefault(require("react"));
const react_2 = require("@chakra-ui/react");
const fi_1 = require("react-icons/fi");
const workflowValidation_1 = require("../../utils/workflowValidation");
// Error code to category mapping
const ERROR_CATEGORIES = {
    GRAPH: [
        workflowValidation_1.ValidationErrorCodes.CYCLIC_DEPENDENCY,
        workflowValidation_1.ValidationErrorCodes.DISCONNECTED_NODE,
        workflowValidation_1.ValidationErrorCodes.INVALID_EDGE,
        workflowValidation_1.ValidationErrorCodes.MULTIPLE_START_NODES,
        workflowValidation_1.ValidationErrorCodes.NO_START_NODE,
        workflowValidation_1.ValidationErrorCodes.NO_END_NODE,
    ],
    CONFIGURATION: [
        workflowValidation_1.ValidationErrorCodes.INVALID_NODE_TYPE,
        workflowValidation_1.ValidationErrorCodes.MISSING_REQUIRED_CONFIG,
        workflowValidation_1.ValidationErrorCodes.INVALID_CONFIG_VALUE,
        workflowValidation_1.ValidationErrorCodes.DUPLICATE_NODE_ID,
    ],
    SECURITY: [
        workflowValidation_1.ValidationErrorCodes.INVALID_SECURITY_POLICY,
        workflowValidation_1.ValidationErrorCodes.INVALID_AUTH_CONFIG,
        workflowValidation_1.ValidationErrorCodes.INVALID_IP_WHITELIST,
        workflowValidation_1.ValidationErrorCodes.INSECURE_CONNECTION,
    ],
    ADMIN: [
        workflowValidation_1.ValidationErrorCodes.INVALID_APPROVAL_CONFIG,
        workflowValidation_1.ValidationErrorCodes.INVALID_TIMEOUT,
        workflowValidation_1.ValidationErrorCodes.INVALID_APPROVER,
    ],
    WORKFLOW: [
        workflowValidation_1.ValidationErrorCodes.INVALID_STATUS,
        workflowValidation_1.ValidationErrorCodes.INVALID_VERSION,
    ],
};
// Category to display name mapping
const CATEGORY_NAMES = {
    GRAPH: 'Graph Structure',
    CONFIGURATION: 'Node Configuration',
    SECURITY: 'Security Settings',
    ADMIN: 'Admin Settings',
    WORKFLOW: 'Workflow Metadata',
};
const ValidationSummary = ({ result, onNodeClick, onErrorClick, showDetails = true, }) => {
    const { errors, warnings } = result;
    const bgColor = (0, react_2.useColorModeValue)('white', 'gray.800');
    const borderColor = (0, react_2.useColorModeValue)('gray.200', 'gray.700');
    // Calculate statistics
    const totalIssues = errors.length + warnings.length;
    const errorCount = errors.length;
    const warningCount = warnings.length;
    const passRate = totalIssues === 0 ? 100 : Math.round((1 - errorCount / totalIssues) * 100);
    // Group errors by category
    const errorsByCategory = react_1.default.useMemo(() => {
        const grouped = {
            GRAPH: [],
            CONFIGURATION: [],
            SECURITY: [],
            ADMIN: [],
            WORKFLOW: [],
        };
        [...errors, ...warnings].forEach(error => {
            for (const [category, codes] of Object.entries(ERROR_CATEGORIES)) {
                if (codes.includes(error.code)) {
                    grouped[category].push(error);
                    break;
                }
            }
        });
        return grouped;
    }, [errors, warnings]);
    // Get category statistics
    const categoryStats = react_1.default.useMemo(() => {
        return Object.entries(errorsByCategory).map(([category, issues]) => ({
            category: CATEGORY_NAMES[category],
            total: issues.length,
            errors: issues.filter(i => i.severity === 'error').length,
            warnings: issues.filter(i => i.severity === 'warning').length,
        }));
    }, [errorsByCategory]);
    return ((0, jsx_runtime_1.jsxs)(react_2.Box, { borderWidth: 1, borderRadius: "lg", overflow: "hidden", bg: bgColor, boxShadow: "sm", children: [(0, jsx_runtime_1.jsx)(react_2.Box, { p: 4, borderBottomWidth: 1, borderColor: borderColor, children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(0, jsx_runtime_1.jsxs)(react_2.HStack, { justify: "space-between", children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "lg", fontWeight: "bold", children: "Validation Summary" }), (0, jsx_runtime_1.jsx)(react_2.Badge, { colorScheme: errorCount === 0 ? 'green' : 'red', fontSize: "sm", px: 2, py: 1, children: errorCount === 0 ? 'Valid' : 'Invalid' })] }), (0, jsx_runtime_1.jsx)(react_2.Progress, { value: passRate, colorScheme: errorCount === 0 ? 'green' : 'red', size: "sm", borderRadius: "full" }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { spacing: 4, children: [(0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Icon, { as: fi_1.FiCheckCircle, color: "green.500" }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "sm", children: ["Pass Rate: ", passRate, "%"] })] }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Icon, { as: fi_1.FiAlertCircle, color: "red.500" }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "sm", children: [errorCount, " Error", errorCount === 1 ? '' : 's'] })] }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Icon, { as: fi_1.FiAlertTriangle, color: "orange.500" }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "sm", children: [warningCount, " Warning", warningCount === 1 ? '' : 's'] })] })] })] }) }), (0, jsx_runtime_1.jsx)(react_2.Box, { p: 4, children: (0, jsx_runtime_1.jsxs)(react_2.Table, { size: "sm", variant: "simple", children: [(0, jsx_runtime_1.jsx)(react_2.Thead, { children: (0, jsx_runtime_1.jsxs)(react_2.Tr, { children: [(0, jsx_runtime_1.jsx)(react_2.Th, { children: "Category" }), (0, jsx_runtime_1.jsx)(react_2.Th, { isNumeric: true, children: "Total" }), (0, jsx_runtime_1.jsx)(react_2.Th, { isNumeric: true, children: "Errors" }), (0, jsx_runtime_1.jsx)(react_2.Th, { isNumeric: true, children: "Warnings" })] }) }), (0, jsx_runtime_1.jsx)(react_2.Tbody, { children: categoryStats.map(stat => ((0, jsx_runtime_1.jsxs)(react_2.Tr, { children: [(0, jsx_runtime_1.jsx)(react_2.Td, { children: stat.category }), (0, jsx_runtime_1.jsx)(react_2.Td, { isNumeric: true, children: stat.total }), (0, jsx_runtime_1.jsx)(react_2.Td, { isNumeric: true, color: stat.errors > 0 ? 'red.500' : undefined, children: stat.errors }), (0, jsx_runtime_1.jsx)(react_2.Td, { isNumeric: true, color: stat.warnings > 0 ? 'orange.500' : undefined, children: stat.warnings })] }, stat.category))) })] }) }), showDetails && ((0, jsx_runtime_1.jsx)(react_2.Accordion, { allowMultiple: true, defaultIndex: [0], children: Object.entries(errorsByCategory).map(([category, issues]) => {
                    if (issues.length === 0)
                        return null;
                    return ((0, jsx_runtime_1.jsxs)(react_2.AccordionItem, { children: [(0, jsx_runtime_1.jsxs)(react_2.AccordionButton, { children: [(0, jsx_runtime_1.jsx)(react_2.Box, { flex: "1", textAlign: "left", children: (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontWeight: "medium", children: CATEGORY_NAMES[category] }), (0, jsx_runtime_1.jsx)(react_2.Badge, { colorScheme: issues.some(i => i.severity === 'error') ? 'red' : 'orange', children: issues.length })] }) }), (0, jsx_runtime_1.jsx)(react_2.AccordionIcon, {})] }), (0, jsx_runtime_1.jsx)(react_2.AccordionPanel, { pb: 4, children: (0, jsx_runtime_1.jsx)(react_2.VStack, { spacing: 2, align: "stretch", children: issues.map((error, index) => ((0, jsx_runtime_1.jsxs)(react_2.HStack, { p: 2, bg: error.severity === 'error' ? 'red.50' : 'orange.50', borderRadius: "md", cursor: onErrorClick ? 'pointer' : 'default', onClick: () => onErrorClick?.(error), children: [(0, jsx_runtime_1.jsx)(react_2.Icon, { as: error.severity === 'error' ? fi_1.FiAlertCircle : fi_1.FiAlertTriangle, color: error.severity === 'error' ? 'red.500' : 'orange.500', boxSize: 4 }), (0, jsx_runtime_1.jsxs)(react_2.VStack, { align: "start", spacing: 0, flex: 1, children: [(0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", children: error.message }), error.nodeId && ((0, jsx_runtime_1.jsxs)(react_2.Badge, { colorScheme: error.severity === 'error' ? 'red' : 'orange', variant: "subtle", cursor: onNodeClick ? 'pointer' : 'default', onClick: (e) => {
                                                            e.stopPropagation();
                                                            onNodeClick?.(error.nodeId);
                                                        }, children: ["Node: ", error.nodeId] }))] })] }, `${error.code}-${index}`))) }) })] }, category));
                }) }))] }));
};
exports.ValidationSummary = ValidationSummary;
//# sourceMappingURL=ValidationSummary.js.map