function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import React, { useMemo } from 'react';
import { Tag } from 'antd';
import { RollbackOutlined } from '@ant-design/icons';
import classNames from 'classnames';
import { Button, PisellModal } from '@pisell/materials';
import "./index.less";
/**
 * 从 data 的嵌套路径中读取可展示字符串。
 *
 * @example
 * getStringByPath(device.data, ['hardware', 'model']);
 */
var getStringByPath = function getStringByPath(source, path) {
  var value = path.reduce(function (current, key) {
    if (!current || _typeof(current) !== 'object') return undefined;
    return current[key];
  }, source);
  return typeof value === 'string' || typeof value === 'number' ? String(value) : '';
};

/** 按固定字段生成信息行，缺失值统一展示为占位符。 */
var buildInfoItems = function buildInfoItems(data, fields) {
  return fields.map(function (field) {
    return {
      labelKey: field.labelKey,
      value: getStringByPath(data, field.path) || '-'
    };
  });
};

/** 渲染接管弹窗中的分组信息表。 */
var renderInfoTable = function renderInfoTable(groups, getText) {
  return /*#__PURE__*/React.createElement("div", {
    className: "saas-app-device-planning-init-takeover-modal__info-table"
  }, groups.map(function (group) {
    return /*#__PURE__*/React.createElement("div", {
      key: group.titleKey,
      className: "saas-app-device-planning-init-takeover-modal__group"
    }, /*#__PURE__*/React.createElement("div", {
      className: "saas-app-device-planning-init-takeover-modal__group-title"
    }, getText(group.titleKey)), group.items.map(function (item) {
      return /*#__PURE__*/React.createElement("div", {
        key: item.labelKey,
        className: "saas-app-device-planning-init-takeover-modal__row"
      }, /*#__PURE__*/React.createElement("span", {
        className: "saas-app-device-planning-init-takeover-modal__label"
      }, getText(item.labelKey)), /*#__PURE__*/React.createElement("span", {
        className: "saas-app-device-planning-init-takeover-modal__value"
      }, item.value));
    }));
  }));
};
var getOnlineStatusColor = function getOnlineStatusColor(status) {
  if (status === 'online') return 'green';
  if (status === 'unknown') return 'default';
  return 'red';
};

/**
 * 初始化校验失败后展示的 last used 接管弹窗。
 *
 * @example
 * <InitCheckTakeoverModal open record={device} onConfirm={handleConfirm} />
 */
var InitCheckTakeoverModal = function InitCheckTakeoverModal(_ref) {
  var open = _ref.open,
    record = _ref.record,
    currentVariant = _ref.currentVariant,
    isPhoneVariant = _ref.isPhoneVariant,
    linkingDeviceId = _ref.linkingDeviceId,
    getText = _ref.getText,
    onCancel = _ref.onCancel,
    onConfirm = _ref.onConfirm;
  var data = record === null || record === void 0 ? void 0 : record._raw.data;
  var statusItems = useMemo(function () {
    var status = (record === null || record === void 0 ? void 0 : record.status) || 'unknown';
    return [{
      labelKey: 'pisell2.saas.device-planning.column.online-status',
      value: /*#__PURE__*/React.createElement(Tag, {
        color: getOnlineStatusColor(status)
      }, getText("pisell2.saas.device-planning.status.".concat(status)) || status || '-')
    }, {
      labelKey: 'pisell2.saas.device-planning.init-takeover.label.last-seen',
      value: (record === null || record === void 0 ? void 0 : record._raw.last_seen) || '-'
    }];
  }, [getText, record === null || record === void 0 ? void 0 : record._raw.last_seen, record === null || record === void 0 ? void 0 : record.status]);
  var hardwareItems = useMemo(function () {
    return buildInfoItems(data, [{
      labelKey: 'pisell2.saas.device-planning.init-takeover.label.hardware-model',
      path: ['hardware', 'model']
    }, {
      labelKey: 'pisell2.saas.device-planning.init-takeover.label.os-version',
      path: ['hardware', 'os_version']
    }]);
  }, [data]);
  var networkItems = useMemo(function () {
    return buildInfoItems(data, [{
      labelKey: 'pisell2.saas.device-planning.init-takeover.label.local-ip',
      path: ['network', 'local_ip']
    }, {
      labelKey: 'pisell2.saas.device-planning.init-takeover.label.internet',
      path: ['network', 'internet']
    }]);
  }, [data]);
  var softwareItems = useMemo(function () {
    return buildInfoItems(data, [{
      labelKey: 'pisell2.saas.device-planning.init-takeover.label.software-name',
      path: ['software', 'name']
    }, {
      labelKey: 'pisell2.saas.device-planning.init-takeover.label.software-version',
      path: ['software', 'version']
    }]);
  }, [data]);
  var groups = useMemo(function () {
    return [{
      titleKey: 'pisell2.saas.device-planning.init-takeover.section.status',
      items: statusItems
    }, {
      titleKey: 'pisell2.saas.device-planning.init-takeover.section.hardware',
      items: hardwareItems
    }, {
      titleKey: 'pisell2.saas.device-planning.init-takeover.section.network',
      items: networkItems
    }, {
      titleKey: 'pisell2.saas.device-planning.init-takeover.section.software',
      items: softwareItems
    }];
  }, [hardwareItems, networkItems, softwareItems, statusItems]);
  return /*#__PURE__*/React.createElement(PisellModal, {
    title: getText('pisell2.saas.device-planning.init-takeover.title'),
    headerDivider: false,
    footerDivider: false,
    open: open,
    footer: /*#__PURE__*/React.createElement("div", {
      className: "saas-app-device-planning-takeover-modal__footer"
    }, /*#__PURE__*/React.createElement(Button, {
      type: "default",
      block: true,
      size: "large",
      disabled: Boolean(linkingDeviceId),
      onClick: onCancel
    }, getText('pisell2.saas.device-planning.init-takeover.cancel')), /*#__PURE__*/React.createElement(Button, {
      type: "primary",
      block: true,
      size: "large",
      icon: /*#__PURE__*/React.createElement(RollbackOutlined, null),
      loading: linkingDeviceId === (record === null || record === void 0 ? void 0 : record.id),
      disabled: Boolean(linkingDeviceId),
      onClick: function onClick() {
        return record && onConfirm(record);
      },
      style: {
        backgroundColor: '#faad14',
        borderColor: '#faad14'
      }
    }, getText('pisell2.saas.device-planning.init-takeover.confirm'))),
    width: 780,
    destroyOnClose: true,
    maskClosable: false,
    platform: isPhoneVariant ? 'h5' : undefined,
    mobileModalHeight: isPhoneVariant ? 'auto' : undefined,
    className: classNames('saas-app-device-planning-init-takeover-modal', "saas-app-device-planning-init-takeover-modal--".concat(currentVariant)),
    onCancel: onCancel
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-app-device-planning-init-takeover-modal__content"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-app-device-planning-init-takeover-modal__message"
  }, getText('pisell2.saas.device-planning.init-takeover.message')), renderInfoTable(groups, getText)));
};
export default InitCheckTakeoverModal;