import React, { useMemo } from "react";
import FacetSection from "./FacetSection";
import ComponentLoader from "../../ComponentLoader/ComponentLoader";
import { getSeverityColor } from "./severityColors";
import LogSeverity from "../../../../Types/Log/LogSeverity";
const RESOURCE_FACET_KEYS = new Set([
    "primaryEntityId",
    "hostId",
    "dockerHostId",
    "kubernetesClusterId",
]);
const SEVERITY_ORDER = [
    LogSeverity.Fatal,
    LogSeverity.Error,
    LogSeverity.Warning,
    LogSeverity.Information,
    LogSeverity.Debug,
    LogSeverity.Trace,
    LogSeverity.Unspecified,
];
function buildSeverityColorMap() {
    const map = {};
    for (const severity of SEVERITY_ORDER) {
        map[severity] = getSeverityColor(severity).fill;
    }
    return map;
}
function buildServiceDisplayMap(serviceMap) {
    const map = {};
    for (const [id, service] of Object.entries(serviceMap)) {
        if (service === null || service === void 0 ? void 0 : service.name) {
            map[id] = service.name;
        }
    }
    return map;
}
function buildServiceColorMap(serviceMap) {
    const map = {};
    for (const [id, service] of Object.entries(serviceMap)) {
        if (service === null || service === void 0 ? void 0 : service.serviceColor) {
            map[id] = service.serviceColor.toString();
        }
    }
    return map;
}
function buildHostDisplayMap(hostMap) {
    const map = {};
    if (!hostMap) {
        return map;
    }
    for (const [id, host] of Object.entries(hostMap)) {
        const label = (host === null || host === void 0 ? void 0 : host.name) || (host === null || host === void 0 ? void 0 : host.hostIdentifier);
        if (label) {
            map[id] = label;
        }
    }
    return map;
}
function buildDockerHostDisplayMap(dockerHostMap) {
    const map = {};
    if (!dockerHostMap) {
        return map;
    }
    for (const [id, dockerHost] of Object.entries(dockerHostMap)) {
        const label = (dockerHost === null || dockerHost === void 0 ? void 0 : dockerHost.name) || (dockerHost === null || dockerHost === void 0 ? void 0 : dockerHost.hostIdentifier);
        if (label) {
            map[id] = label;
        }
    }
    return map;
}
function buildClusterDisplayMap(clusterMap) {
    const map = {};
    if (!clusterMap) {
        return map;
    }
    for (const [id, cluster] of Object.entries(clusterMap)) {
        const label = (cluster === null || cluster === void 0 ? void 0 : cluster.name) || (cluster === null || cluster === void 0 ? void 0 : cluster.clusterIdentifier);
        if (label) {
            map[id] = label;
        }
    }
    return map;
}
function getFacetTitle(key) {
    const titleMap = {
        severityText: "Severity",
        primaryEntityId: "Service",
        hostId: "Host",
        dockerHostId: "Docker Host",
        kubernetesClusterId: "Kubernetes Cluster",
        traceId: "Trace ID",
        spanId: "Span ID",
    };
    return titleMap[key] || key;
}
const LogsFacetSidebar = (props) => {
    const severityColorMap = useMemo(() => {
        return buildSeverityColorMap();
    }, []);
    const serviceDisplayMap = useMemo(() => {
        return buildServiceDisplayMap(props.serviceMap);
    }, [props.serviceMap]);
    const serviceColorMap = useMemo(() => {
        return buildServiceColorMap(props.serviceMap);
    }, [props.serviceMap]);
    const hostDisplayMap = useMemo(() => {
        return buildHostDisplayMap(props.hostMap);
    }, [props.hostMap]);
    const dockerHostDisplayMap = useMemo(() => {
        return buildDockerHostDisplayMap(props.dockerHostMap);
    }, [props.dockerHostMap]);
    const clusterDisplayMap = useMemo(() => {
        return buildClusterDisplayMap(props.kubernetesClusterMap);
    }, [props.kubernetesClusterMap]);
    const facetKeys = useMemo(() => {
        const priorityKeys = [
            "severityText",
            "primaryEntityId",
            "hostId",
            "dockerHostId",
            "kubernetesClusterId",
        ];
        const otherKeys = Object.keys(props.facetData).filter((key) => {
            return !priorityKeys.includes(key);
        });
        return [
            ...priorityKeys.filter((key) => {
                return props.facetData[key] !== undefined;
            }),
            ...otherKeys.sort(),
        ];
    }, [props.facetData]);
    // Build a map of facetKey -> Set<activeValue>
    const activeValuesByKey = useMemo(() => {
        const map = {};
        if (props.activeFilters) {
            for (const filter of props.activeFilters) {
                if (!map[filter.facetKey]) {
                    map[filter.facetKey] = new Set();
                }
                map[filter.facetKey].add(filter.value);
            }
        }
        return map;
    }, [props.activeFilters]);
    return (React.createElement("div", { className: "flex h-full w-56 flex-none flex-col overflow-y-auto rounded-lg border border-gray-200 bg-white" },
        React.createElement("div", { className: "border-b border-gray-100 px-3 py-2.5" },
            React.createElement("h3", { className: "text-[11px] font-semibold uppercase tracking-widest text-gray-400" }, "Filters")),
        props.isLoading && Object.keys(props.facetData).length === 0 && (React.createElement("div", { className: "flex flex-1 items-center justify-center py-8" },
            React.createElement(ComponentLoader, null))),
        React.createElement("div", { className: "flex-1 overflow-y-auto" },
            props.savedViews && props.savedViews.length > 0 && (React.createElement("div", { className: "border-b border-gray-100 px-3 py-3" },
                React.createElement("p", { className: "mb-2 text-[11px] font-semibold uppercase tracking-wider text-gray-400" }, "Saved Views"),
                React.createElement("div", { className: "space-y-1.5" }, props.savedViews.map((view) => {
                    const isSelected = view.id === props.selectedSavedViewId;
                    return (React.createElement("button", { key: view.id, type: "button", className: `flex w-full items-center justify-between rounded-md px-2.5 py-2 text-left text-sm transition-colors ${isSelected
                            ? "bg-indigo-50 text-indigo-700"
                            : "text-gray-600 hover:bg-gray-50 hover:text-gray-800"}`, onClick: () => {
                            var _a;
                            (_a = props.onSavedViewSelect) === null || _a === void 0 ? void 0 : _a.call(props, view.id);
                        } },
                        React.createElement("span", { className: "truncate" }, view.name),
                        view.isDefault && (React.createElement("span", { className: "ml-2 rounded-full bg-amber-100 px-2 py-0.5 text-[10px] font-semibold uppercase tracking-wide text-amber-700" }, "Default"))));
                })))),
            facetKeys.map((key) => {
                const values = props.facetData[key] || [];
                let valueDisplayMap;
                let valueColorMap;
                if (key === "primaryEntityId") {
                    valueDisplayMap = serviceDisplayMap;
                    valueColorMap = serviceColorMap;
                }
                else if (key === "hostId") {
                    valueDisplayMap = hostDisplayMap;
                }
                else if (key === "dockerHostId") {
                    valueDisplayMap = dockerHostDisplayMap;
                }
                else if (key === "kubernetesClusterId") {
                    valueDisplayMap = clusterDisplayMap;
                }
                else if (key === "severityText") {
                    valueColorMap = severityColorMap;
                }
                const onSearchChange = RESOURCE_FACET_KEYS.has(key) && props.onFacetSearchChange
                    ? (text) => {
                        props.onFacetSearchChange(key, text);
                    }
                    : undefined;
                return (React.createElement(FacetSection, { key: key, facetKey: key, title: getFacetTitle(key), values: values, onIncludeValue: props.onIncludeFilter, onExcludeValue: props.onExcludeFilter, valueDisplayMap: valueDisplayMap, valueColorMap: valueColorMap, activeValues: activeValuesByKey[key], onSearchChange: onSearchChange }));
            }))));
};
export default LogsFacetSidebar;
//# sourceMappingURL=LogsFacetSidebar.js.map