import { getText } from "../../locales/index.js";
import Statistics from "./errorTipStatistics/index.js";
import ErrorCard from "./errorCard/index.js";
import LogCard from "./logCard/index.js";
import React, { useEffect, useMemo, useState } from "react";
import { Button, Card, Empty, Input, Segmented, Select, Spin, Tabs } from "antd";
import { DownloadOutlined, SearchOutlined } from "@ant-design/icons";
import "./index.less";
//#region src/components/hardwareErrorTip/index.tsx
const { TabPane } = Tabs;
const { Option } = Select;
const HardwareErrorTip = ({ statistics, errors = [], logs = [], onExportLogs, onCloseError, onTabChange, activeTab: externalActiveTab, isLoadingErrors = false, isLoadingLogs = false, style }) => {
	useEffect(() => {
		document.body.id = "body";
	}, []);
	const [internalActiveTab, setInternalActiveTab] = useState("errors");
	const activeTab = externalActiveTab !== void 0 ? externalActiveTab : internalActiveTab;
	const [deviceFilter, setDeviceFilter] = useState("all");
	const [errorLevelFilter, setErrorLevelFilter] = useState("all");
	const [logLevelFilter, setLogLevelFilter] = useState("all");
	const [searchText, setSearchText] = useState("");
	const getDeviceOptions = useMemo(() => {
		const devices = /* @__PURE__ */ new Set();
		errors.forEach((error) => {
			var _error$device;
			if ((_error$device = error.device) === null || _error$device === void 0 ? void 0 : _error$device.device_name) devices.add(error.device.device_name);
		});
		logs.forEach((log) => {
			var _log$device;
			if ((_log$device = log.device) === null || _log$device === void 0 ? void 0 : _log$device.device_name) devices.add(log.device.device_name);
		});
		return Array.from(devices);
	}, [errors, logs]);
	const filteredErrors = useMemo(() => {
		let filtered = errors;
		if (deviceFilter !== "all") filtered = filtered.filter((error) => {
			var _error$device2;
			return ((_error$device2 = error.device) === null || _error$device2 === void 0 ? void 0 : _error$device2.device_name) === deviceFilter;
		});
		if (errorLevelFilter !== "all") {
			const targetLevel = {
				fatal: 0,
				critical: 1,
				normal: 2,
				info: 3
			}[errorLevelFilter];
			filtered = filtered.filter((error) => error.error_level === targetLevel);
		}
		return filtered;
	}, [
		errors,
		deviceFilter,
		errorLevelFilter
	]);
	const filteredLogs = useMemo(() => {
		let filtered = logs;
		if (deviceFilter !== "all") filtered = filtered.filter((log) => {
			var _log$device2;
			return ((_log$device2 = log.device) === null || _log$device2 === void 0 ? void 0 : _log$device2.device_name) === deviceFilter;
		});
		if (logLevelFilter !== "all") {
			const targetLevel = {
				DEBUG: 0,
				INFO: 1,
				ERROR: 2
			}[logLevelFilter];
			filtered = filtered.filter((log) => log.log_level === targetLevel);
		}
		if (searchText) filtered = filtered.filter((log) => log.message.toLowerCase().includes(searchText.toLowerCase()));
		return filtered;
	}, [
		logs,
		deviceFilter,
		logLevelFilter,
		searchText
	]);
	return /* @__PURE__ */ React.createElement("div", {
		className: "hardware-error-tip",
		style
	}, /* @__PURE__ */ React.createElement("div", { className: "statistics-section" }, /* @__PURE__ */ React.createElement(Statistics, { data: statistics })), /* @__PURE__ */ React.createElement(Card, { className: "main-content" }, /* @__PURE__ */ React.createElement("div", { className: "header" }, /* @__PURE__ */ React.createElement("h3", null, getText("pisell2.text.hardware-error.error-center")), /* @__PURE__ */ React.createElement("div", { className: "controls" }, /* @__PURE__ */ React.createElement(Select, {
		value: deviceFilter,
		onChange: setDeviceFilter,
		style: { marginRight: 8 },
		placeholder: getText("pisell2.text.hardware-error.all-devices"),
		popupMatchSelectWidth: false
	}, /* @__PURE__ */ React.createElement(Option, { value: "all" }, getText("pisell2.text.hardware-error.all-devices")), getDeviceOptions.map((device) => /* @__PURE__ */ React.createElement(Option, {
		key: device,
		value: device
	}, device))), activeTab === "errors" ? /* @__PURE__ */ React.createElement(Select, {
		value: errorLevelFilter,
		onChange: setErrorLevelFilter,
		placeholder: getText("pisell2.text.hardware-error.all-levels"),
		popupMatchSelectWidth: false
	}, /* @__PURE__ */ React.createElement(Option, { value: "all" }, getText("pisell2.text.hardware-error.all-levels")), /* @__PURE__ */ React.createElement(Option, { value: "fatal" }, getText("pisell2.text.hardware-error.fatal")), /* @__PURE__ */ React.createElement(Option, { value: "critical" }, getText("pisell2.text.hardware-error.critical")), /* @__PURE__ */ React.createElement(Option, { value: "normal" }, getText("pisell2.text.hardware-error.normal")), /* @__PURE__ */ React.createElement(Option, { value: "info" }, getText("pisell2.text.hardware-error.info"))) : /* @__PURE__ */ React.createElement(Select, {
		value: logLevelFilter,
		onChange: setLogLevelFilter,
		placeholder: getText("pisell2.text.hardware-error.all-levels"),
		popupMatchSelectWidth: false
	}, /* @__PURE__ */ React.createElement(Option, { value: "all" }, getText("pisell2.text.hardware-error.all-levels")), /* @__PURE__ */ React.createElement(Option, { value: "ERROR" }, getText("pisell2.text.hardware-error.error-info")), /* @__PURE__ */ React.createElement(Option, { value: "INFO" }, getText("pisell2.text.hardware-error.important-info")), /* @__PURE__ */ React.createElement(Option, { value: "DEBUG" }, getText("pisell2.text.hardware-error.debug-info"))))), /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(Segmented, {
		value: activeTab,
		onChange: (value) => {
			if (externalActiveTab !== void 0) onTabChange === null || onTabChange === void 0 || onTabChange(value);
			else setInternalActiveTab(value);
		},
		options: [{
			label: getText("pisell2.text.hardware-error.error-list"),
			value: "errors"
		}, {
			label: getText("pisell2.text.hardware-error.error-logs"),
			value: "logs"
		}],
		style: { fontWeight: "bold" }
	}), activeTab === "logs" ? /* @__PURE__ */ React.createElement("div", { className: "logs-search-export-bar" }, /* @__PURE__ */ React.createElement(Input, {
		placeholder: getText("pisell2.text.hardware-error.search-logs"),
		value: searchText,
		onChange: (e) => setSearchText(e.target.value),
		style: {
			width: 300,
			marginRight: 8
		},
		allowClear: true,
		prefix: /* @__PURE__ */ React.createElement(SearchOutlined, { style: { color: "#7e8490" } })
	}), /* @__PURE__ */ React.createElement(Button, {
		type: "text",
		icon: /* @__PURE__ */ React.createElement(DownloadOutlined, null),
		onClick: () => onExportLogs === null || onExportLogs === void 0 ? void 0 : onExportLogs(filteredLogs)
	}, getText("pisell2.text.hardware-error.export-logs"))) : null), /* @__PURE__ */ React.createElement(Tabs, {
		activeKey: activeTab,
		onChange: (key) => {
			if (externalActiveTab !== void 0) onTabChange === null || onTabChange === void 0 || onTabChange(key);
			else setInternalActiveTab(key);
		},
		renderTabBar: () => null,
		style: { marginTop: 16 }
	}, /* @__PURE__ */ React.createElement(TabPane, {
		tab: getText("pisell2.text.hardware-error.error-list"),
		key: "errors"
	}, /* @__PURE__ */ React.createElement("div", { className: "error-list" }, /* @__PURE__ */ React.createElement(Spin, { spinning: isLoadingErrors }, !isLoadingErrors && filteredErrors.length === 0 ? /* @__PURE__ */ React.createElement(Empty, { description: getText("pisell2.text.hardware-error.no-errors") }) : filteredErrors.map((error, index) => /* @__PURE__ */ React.createElement(ErrorCard, {
		key: index,
		data: error,
		onClose: onCloseError ? () => onCloseError(error, index) : void 0
	}))))), /* @__PURE__ */ React.createElement(TabPane, {
		tab: getText("pisell2.text.hardware-error.error-logs"),
		key: "logs"
	}, /* @__PURE__ */ React.createElement("div", { className: "log-list" }, /* @__PURE__ */ React.createElement(Spin, { spinning: isLoadingLogs }, !isLoadingLogs && filteredLogs.length === 0 ? /* @__PURE__ */ React.createElement(Empty, { description: getText("pisell2.text.hardware-error.no-logs") }) : filteredLogs.map((log, index) => /* @__PURE__ */ React.createElement(LogCard, {
		key: index,
		data: log
	}))))))));
};
//#endregion
export { HardwareErrorTip as default };
