const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_index$1 = require("./errorTipStatistics/index.js");
const require_index$2 = require("./errorCard/index.js");
const require_index$3 = require("./logCard/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _ant_design_icons = require("@ant-design/icons");
require("./index.less");
//#region src/components/hardwareErrorTip/index.tsx
const { TabPane } = antd.Tabs;
const { Option } = antd.Select;
const HardwareErrorTip = ({ statistics, errors = [], logs = [], onExportLogs, onCloseError, onTabChange, activeTab: externalActiveTab, isLoadingErrors = false, isLoadingLogs = false, style }) => {
	(0, react.useEffect)(() => {
		document.body.id = "body";
	}, []);
	const [internalActiveTab, setInternalActiveTab] = (0, react.useState)("errors");
	const activeTab = externalActiveTab !== void 0 ? externalActiveTab : internalActiveTab;
	const [deviceFilter, setDeviceFilter] = (0, react.useState)("all");
	const [errorLevelFilter, setErrorLevelFilter] = (0, react.useState)("all");
	const [logLevelFilter, setLogLevelFilter] = (0, react.useState)("all");
	const [searchText, setSearchText] = (0, react.useState)("");
	const getDeviceOptions = (0, react.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 = (0, react.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 = (0, react.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.default.createElement("div", {
		className: "hardware-error-tip",
		style
	}, /* @__PURE__ */ react.default.createElement("div", { className: "statistics-section" }, /* @__PURE__ */ react.default.createElement(require_index$1.default, { data: statistics })), /* @__PURE__ */ react.default.createElement(antd.Card, { className: "main-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "header" }, /* @__PURE__ */ react.default.createElement("h3", null, require_index.getText("pisell2.text.hardware-error.error-center")), /* @__PURE__ */ react.default.createElement("div", { className: "controls" }, /* @__PURE__ */ react.default.createElement(antd.Select, {
		value: deviceFilter,
		onChange: setDeviceFilter,
		style: { marginRight: 8 },
		placeholder: require_index.getText("pisell2.text.hardware-error.all-devices"),
		popupMatchSelectWidth: false
	}, /* @__PURE__ */ react.default.createElement(Option, { value: "all" }, require_index.getText("pisell2.text.hardware-error.all-devices")), getDeviceOptions.map((device) => /* @__PURE__ */ react.default.createElement(Option, {
		key: device,
		value: device
	}, device))), activeTab === "errors" ? /* @__PURE__ */ react.default.createElement(antd.Select, {
		value: errorLevelFilter,
		onChange: setErrorLevelFilter,
		placeholder: require_index.getText("pisell2.text.hardware-error.all-levels"),
		popupMatchSelectWidth: false
	}, /* @__PURE__ */ react.default.createElement(Option, { value: "all" }, require_index.getText("pisell2.text.hardware-error.all-levels")), /* @__PURE__ */ react.default.createElement(Option, { value: "fatal" }, require_index.getText("pisell2.text.hardware-error.fatal")), /* @__PURE__ */ react.default.createElement(Option, { value: "critical" }, require_index.getText("pisell2.text.hardware-error.critical")), /* @__PURE__ */ react.default.createElement(Option, { value: "normal" }, require_index.getText("pisell2.text.hardware-error.normal")), /* @__PURE__ */ react.default.createElement(Option, { value: "info" }, require_index.getText("pisell2.text.hardware-error.info"))) : /* @__PURE__ */ react.default.createElement(antd.Select, {
		value: logLevelFilter,
		onChange: setLogLevelFilter,
		placeholder: require_index.getText("pisell2.text.hardware-error.all-levels"),
		popupMatchSelectWidth: false
	}, /* @__PURE__ */ react.default.createElement(Option, { value: "all" }, require_index.getText("pisell2.text.hardware-error.all-levels")), /* @__PURE__ */ react.default.createElement(Option, { value: "ERROR" }, require_index.getText("pisell2.text.hardware-error.error-info")), /* @__PURE__ */ react.default.createElement(Option, { value: "INFO" }, require_index.getText("pisell2.text.hardware-error.important-info")), /* @__PURE__ */ react.default.createElement(Option, { value: "DEBUG" }, require_index.getText("pisell2.text.hardware-error.debug-info"))))), /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement(antd.Segmented, {
		value: activeTab,
		onChange: (value) => {
			if (externalActiveTab !== void 0) onTabChange === null || onTabChange === void 0 || onTabChange(value);
			else setInternalActiveTab(value);
		},
		options: [{
			label: require_index.getText("pisell2.text.hardware-error.error-list"),
			value: "errors"
		}, {
			label: require_index.getText("pisell2.text.hardware-error.error-logs"),
			value: "logs"
		}],
		style: { fontWeight: "bold" }
	}), activeTab === "logs" ? /* @__PURE__ */ react.default.createElement("div", { className: "logs-search-export-bar" }, /* @__PURE__ */ react.default.createElement(antd.Input, {
		placeholder: require_index.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.default.createElement(_ant_design_icons.SearchOutlined, { style: { color: "#7e8490" } })
	}), /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "text",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.DownloadOutlined, null),
		onClick: () => onExportLogs === null || onExportLogs === void 0 ? void 0 : onExportLogs(filteredLogs)
	}, require_index.getText("pisell2.text.hardware-error.export-logs"))) : null), /* @__PURE__ */ react.default.createElement(antd.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.default.createElement(TabPane, {
		tab: require_index.getText("pisell2.text.hardware-error.error-list"),
		key: "errors"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "error-list" }, /* @__PURE__ */ react.default.createElement(antd.Spin, { spinning: isLoadingErrors }, !isLoadingErrors && filteredErrors.length === 0 ? /* @__PURE__ */ react.default.createElement(antd.Empty, { description: require_index.getText("pisell2.text.hardware-error.no-errors") }) : filteredErrors.map((error, index) => /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		key: index,
		data: error,
		onClose: onCloseError ? () => onCloseError(error, index) : void 0
	}))))), /* @__PURE__ */ react.default.createElement(TabPane, {
		tab: require_index.getText("pisell2.text.hardware-error.error-logs"),
		key: "logs"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "log-list" }, /* @__PURE__ */ react.default.createElement(antd.Spin, { spinning: isLoadingLogs }, !isLoadingLogs && filteredLogs.length === 0 ? /* @__PURE__ */ react.default.createElement(antd.Empty, { description: require_index.getText("pisell2.text.hardware-error.no-logs") }) : filteredLogs.map((log, index) => /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		key: index,
		data: log
	}))))))));
};
//#endregion
exports.default = HardwareErrorTip;
