"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViolationsDisplay = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@blueprintjs/core");
const ui_kit_1 = require("@stoplight/ui-kit");
const lodash_1 = require("lodash");
const react_1 = require("react");
const React = tslib_1.__importStar(require("react"));
exports.ViolationsDisplay = ({ violations }) => {
    const errorCount = violations.filter(v => v.severity === 0).length;
    const warningCount = violations.length - errorCount;
    const [tree, setTree] = react_1.useState([]);
    react_1.useEffect(() => {
        const headerElement = (React.createElement("strong", { className: "mb-1" },
            React.createElement("span", { className: "inline mr-3" }, "The returned response does not match the JSON Schema associated with the current operation."),
            errorCount > 0 && (React.createElement("span", { className: "pr-3" },
                React.createElement(ui_kit_1.Icon, { icon: "error", className: "mr-2" }),
                React.createElement("span", null, errorCount))),
            warningCount > 0 && (React.createElement("span", { className: "pr-3" },
                React.createElement(ui_kit_1.Icon, { icon: "warning-sign", className: "mr-1" }),
                React.createElement("span", null, warningCount)))));
        setTree([
            {
                id: 'root',
                isExpanded: false,
                label: headerElement,
                childNodes: buildTreeStructure(violations),
            },
        ]);
    }, [violations, errorCount, warningCount]);
    const refresh = () => setTree([...tree]);
    const handleNodeCollapse = (nodeData) => {
        nodeData.isExpanded = false;
        refresh();
    };
    const handleNodeExpand = (nodeData) => {
        nodeData.isExpanded = true;
        refresh();
    };
    const handleNodeClick = (nodeData) => {
        nodeData.isExpanded = !nodeData.isExpanded;
        refresh();
    };
    return (React.createElement("section", { className: "p-3 px-4 RequestMaker__ViolationsDisplay" },
        React.createElement(core_1.Tree, { contents: tree, onNodeCollapse: handleNodeCollapse, onNodeExpand: handleNodeExpand, onNodeClick: handleNodeClick })));
};
exports.ViolationsDisplay.displayName = 'RequestMaker.ViolationsDisplay';
const buildTreeStructure = (violationsFlat, parentPaths = []) => {
    const rootPaths = lodash_1.uniq(violationsFlat.map(v => (v.path && v.path[parentPaths.length]) || '').filter(v => !!v));
    return rootPaths.map(path => {
        const currentPathArray = [...parentPaths, path];
        const violationsOnPath = violationsFlat.filter(v => lodash_1.isEqual(v.path, currentPathArray));
        const violationsOnChildren = violationsFlat.filter(v => { var _a; return lodash_1.isEqual((_a = v.path) === null || _a === void 0 ? void 0 : _a.slice(0, currentPathArray.length), currentPathArray); });
        return {
            id: currentPathArray.join('#'),
            label: React.createElement(ui_kit_1.Code, null, path),
            childNodes: [
                ...violationsOnPath.map(createViolationTreeNode),
                ...buildTreeStructure(violationsOnChildren, currentPathArray),
            ],
            isExpanded: true,
        };
    });
    function createViolationTreeNode(violation) {
        var _a;
        return {
            id: ((_a = violation.path) === null || _a === void 0 ? void 0 : _a.join('#')) + violation.message,
            label: React.createElement("em", null, violation.message),
            nodeData: violation,
            isExpanded: true,
            icon: violation.severity === 0 ? 'error' : 'warning-sign',
        };
    }
};
//# sourceMappingURL=ViolationsDisplay.js.map