var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

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