function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useEffect, useMemo } from 'react';
import { Card, Tabs, Segmented, Select, Input, Button, Empty, Spin } from 'antd';
import { SearchOutlined, DownloadOutlined } from '@ant-design/icons';
import Statistics from "./errorTipStatistics";
import ErrorCard from "./errorCard";
import LogCard from "./logCard";
import { getText } from "../../locales";
import "./index.less";
var TabPane = Tabs.TabPane;
var Option = Select.Option;
var HardwareErrorTip = function HardwareErrorTip(_ref) {
  var statistics = _ref.statistics,
    _ref$errors = _ref.errors,
    errors = _ref$errors === void 0 ? [] : _ref$errors,
    _ref$logs = _ref.logs,
    logs = _ref$logs === void 0 ? [] : _ref$logs,
    onExportLogs = _ref.onExportLogs,
    onCloseError = _ref.onCloseError,
    onTabChange = _ref.onTabChange,
    externalActiveTab = _ref.activeTab,
    _ref$isLoadingErrors = _ref.isLoadingErrors,
    isLoadingErrors = _ref$isLoadingErrors === void 0 ? false : _ref$isLoadingErrors,
    _ref$isLoadingLogs = _ref.isLoadingLogs,
    isLoadingLogs = _ref$isLoadingLogs === void 0 ? false : _ref$isLoadingLogs,
    style = _ref.style;
  useEffect(function () {
    document.body.id = 'body';
  }, []);
  var _useState = useState('errors'),
    _useState2 = _slicedToArray(_useState, 2),
    internalActiveTab = _useState2[0],
    setInternalActiveTab = _useState2[1];
  var activeTab = externalActiveTab !== undefined ? externalActiveTab : internalActiveTab;
  var _useState3 = useState('all'),
    _useState4 = _slicedToArray(_useState3, 2),
    deviceFilter = _useState4[0],
    setDeviceFilter = _useState4[1];
  var _useState5 = useState('all'),
    _useState6 = _slicedToArray(_useState5, 2),
    errorLevelFilter = _useState6[0],
    setErrorLevelFilter = _useState6[1];
  var _useState7 = useState('all'),
    _useState8 = _slicedToArray(_useState7, 2),
    logLevelFilter = _useState8[0],
    setLogLevelFilter = _useState8[1];
  var _useState9 = useState(''),
    _useState10 = _slicedToArray(_useState9, 2),
    searchText = _useState10[0],
    setSearchText = _useState10[1];

  // 获取设备列表
  var getDeviceOptions = useMemo(function () {
    var devices = new Set();
    errors.forEach(function (error) {
      var _error$device;
      if ((_error$device = error.device) !== null && _error$device !== void 0 && _error$device.device_name) {
        devices.add(error.device.device_name);
      }
    });
    logs.forEach(function (log) {
      var _log$device;
      if ((_log$device = log.device) !== null && _log$device !== void 0 && _log$device.device_name) {
        devices.add(log.device.device_name);
      }
    });
    return Array.from(devices);
  }, [errors, logs]);

  // 过滤错误数据
  var filteredErrors = useMemo(function () {
    var filtered = errors;

    // 设备筛选
    if (deviceFilter !== 'all') {
      filtered = filtered.filter(function (error) {
        var _error$device2;
        return ((_error$device2 = error.device) === null || _error$device2 === void 0 ? void 0 : _error$device2.device_name) === deviceFilter;
      });
    }

    // 等级筛选
    if (errorLevelFilter !== 'all') {
      var levelMap = {
        fatal: 0,
        critical: 1,
        normal: 2,
        info: 3
      };
      var targetLevel = levelMap[errorLevelFilter];
      filtered = filtered.filter(function (error) {
        return error.error_level === targetLevel;
      });
    }
    return filtered;
  }, [errors, deviceFilter, errorLevelFilter]);

  // 过滤日志数据
  var filteredLogs = useMemo(function () {
    var filtered = logs;

    // 设备筛选
    if (deviceFilter !== 'all') {
      filtered = filtered.filter(function (log) {
        var _log$device2;
        return ((_log$device2 = log.device) === null || _log$device2 === void 0 ? void 0 : _log$device2.device_name) === deviceFilter;
      });
    }

    // 等级筛选
    if (logLevelFilter !== 'all') {
      var levelMap = {
        DEBUG: 0,
        INFO: 1,
        ERROR: 2
      };
      var targetLevel = levelMap[logLevelFilter];
      filtered = filtered.filter(function (log) {
        return log.log_level === targetLevel;
      });
    }

    // 搜索筛选
    if (searchText) {
      filtered = filtered.filter(function (log) {
        return log.message.toLowerCase().includes(searchText.toLowerCase());
      });
    }
    return filtered;
  }, [logs, deviceFilter, logLevelFilter, searchText]);
  return /*#__PURE__*/React.createElement("div", {
    className: "hardware-error-tip",
    style: 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(function (device) {
    return /*#__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: function onChange(value) {
      if (externalActiveTab !== undefined) {
        // 受控模式，通知外部
        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: function onChange(e) {
      return 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: function onClick() {
      return onExportLogs === null || onExportLogs === void 0 ? void 0 : onExportLogs(filteredLogs);
    }
  }, getText('pisell2.text.hardware-error.export-logs'))) : null), /*#__PURE__*/React.createElement(Tabs, {
    activeKey: activeTab,
    onChange: function onChange(key) {
      if (externalActiveTab !== undefined) {
        // 受控模式，通知外部
        onTabChange === null || onTabChange === void 0 || onTabChange(key);
      } else {
        // 非受控模式，内部管理状态
        setInternalActiveTab(key);
      }
    },
    renderTabBar: function renderTabBar() {
      return 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(function (error, index) {
    return /*#__PURE__*/React.createElement(ErrorCard, {
      key: index,
      data: error,
      onClose: onCloseError ? function () {
        return onCloseError(error, index);
      } : undefined
    });
  })))), /*#__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(function (log, index) {
    return /*#__PURE__*/React.createElement(LogCard, {
      key: index,
      data: log
    });
  })))))));
};
export default HardwareErrorTip;