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/plus/saasDevice/devicePlanning/modules/deviceConnections/components/ConnectedDevicePreviewModal.tsx
var ConnectedDevicePreviewModal_exports = {};
__export(ConnectedDevicePreviewModal_exports, {
  default: () => ConnectedDevicePreviewModal_default
});
module.exports = __toCommonJS(ConnectedDevicePreviewModal_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_icons = require("@ant-design/icons");
var import_api = require("../../../api");
var import_model = require("../../../model");
var import_shared = require("../../../shared");
var import_DeviceInfoPanel = __toESM(require("../../deviceDetail/components/DeviceInfoPanel"));
var import_ConnectionDeviceCard = __toESM(require("./ConnectionDeviceCard"));
var getStatusTone = (value) => {
  if (value === "online" || value === "connected" || value === "linked") {
    return "success";
  }
  if (value === "connecting") return "processing";
  if (value === "unlinked") return "warning";
  if (value === "disconnected") return "error";
  return "default";
};
var getDataValue = (data, key) => {
  if (!data) return void 0;
  const value = data[key];
  if (value == null || typeof value === "object") return void 0;
  return String(value);
};
var getNetworkLocalIp = (data) => {
  const network = data == null ? void 0 : data.network;
  if (!network || typeof network !== "object" || Array.isArray(network)) {
    return void 0;
  }
  return getDataValue(network, "local_ip");
};
var getConnectionLines = (connection) => {
  var _a;
  const connectionType = (0, import_shared.getConnectionTypeLabel)(
    (_a = connection.config) == null ? void 0 : _a.connection_type
  );
  const parts = [connectionType === "-" ? void 0 : connectionType];
  const connectionData = connection.data;
  const ip = getDataValue(connectionData, "ip") || getNetworkLocalIp(connectionData);
  const port = getDataValue(connectionData, "port");
  const macAddress = getDataValue(connectionData, "mac");
  if (ip) parts.push(`IP: ${ip}`);
  if (port) parts.push(`Port: ${port}`);
  if (macAddress) parts.push(`MAC: ${macAddress}`);
  return parts.filter(Boolean).length ? [parts.filter(Boolean).join(" · ")] : [];
};
var ConnectedDevicePreviewModal = ({
  open,
  device,
  connectionType,
  getText,
  translation,
  onClose
}) => {
  var _a;
  const [detailDevice, setDetailDevice] = (0, import_react.useState)(device);
  const [connections, setConnections] = (0, import_react.useState)([]);
  const [loading, setLoading] = (0, import_react.useState)(false);
  const record = (0, import_model.createDeviceRow)(detailDevice, translation);
  const getStatusLabel = (value) => {
    if (!value) return "-";
    const key = `pisell2.saas.device-planning.status.${value}`;
    const text = getText(key);
    return text && text !== key ? text : value;
  };
  (0, import_react.useEffect)(() => {
    if (!open || !device.id) return;
    let cancelled = false;
    setDetailDevice(device);
    setConnections([]);
    setLoading(true);
    Promise.all([
      (0, import_api.getDeviceDetail)(device.id),
      (0, import_api.getDeviceConnections)({
        type: connectionType,
        target_device_id: device.id,
        with: [
          "sourceDevice.type",
          "sourceDevice.model.brand",
          "sourceDevice.workarea",
          "profiles"
        ]
      })
    ]).then(([detail, list]) => {
      if (cancelled) return;
      setDetailDevice(detail || device);
      setConnections(list);
    }).catch(() => {
      if (cancelled) return;
      setDetailDevice(device);
      setConnections([]);
    }).finally(() => {
      if (!cancelled) {
        setLoading(false);
      }
    });
    return () => {
      cancelled = true;
    };
  }, [connectionType, device, device.id, open]);
  const renderConnectionItem = (connection) => {
    var _a2, _b, _c, _d, _e, _f;
    const smartDevice = connection.source_device;
    return /* @__PURE__ */ import_react.default.createElement(
      import_ConnectionDeviceCard.default,
      {
        key: connection.id,
        title: smartDevice ? translation(smartDevice.name) || smartDevice.id : "-",
        deviceInfo: {
          icon: ((_a2 = smartDevice == null ? void 0 : smartDevice.type) == null ? void 0 : _a2.icon) || "DesktopOutlined",
          modelName: translation((_b = smartDevice == null ? void 0 : smartDevice.model) == null ? void 0 : _b.name) || (smartDevice == null ? void 0 : smartDevice.model_code) || "-",
          brandName: translation((_d = (_c = smartDevice == null ? void 0 : smartDevice.model) == null ? void 0 : _c.brand) == null ? void 0 : _d.name) || ((_e = smartDevice == null ? void 0 : smartDevice.model) == null ? void 0 : _e.brand_code) || "-",
          typeName: translation((_f = smartDevice == null ? void 0 : smartDevice.type) == null ? void 0 : _f.name) || (smartDevice == null ? void 0 : smartDevice.type_code) || "-",
          statusLines: []
        },
        connectionInfo: {
          lines: getConnectionLines(connection)
        },
        deviceStatuses: [
          {
            key: "link",
            label: getStatusLabel((smartDevice == null ? void 0 : smartDevice.link) || "unconnected"),
            tone: getStatusTone((smartDevice == null ? void 0 : smartDevice.link) || "unconnected")
          },
          {
            key: "online",
            label: getStatusLabel((smartDevice == null ? void 0 : smartDevice.status) || "unknown"),
            tone: getStatusTone((smartDevice == null ? void 0 : smartDevice.status) || "unknown")
          }
        ],
        connectionStatuses: [
          {
            key: "connection",
            label: getStatusLabel(connection.status || "unconnected"),
            tone: getStatusTone(connection.status || "unconnected")
          }
        ],
        profiles: (connection.profiles || []).map((profile) => ({
          key: String(profile.id),
          label: translation(profile.name) || profile.id
        })),
        lastSeen: connection.last_seen ? `${getText("pisell2.saas.device-planning.detail.last-seen")}${connection.last_seen}` : void 0
      }
    );
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open,
      width: 760,
      title: /* @__PURE__ */ import_react.default.createElement("div", { className: "device-detail-drawer__preview-title" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "device-detail-drawer__preview-icon" }, /* @__PURE__ */ import_react.default.createElement(
        import_materials.Icon,
        {
          type: ((_a = detailDevice.type) == null ? void 0 : _a.icon) || "PrinterOutlined",
          color: import_shared.DEVICE_ICON_COLOR[detailDevice.type_kind]
        }
      )), /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { className: "device-detail-drawer__preview-name" }, record.name), /* @__PURE__ */ import_react.default.createElement("div", { className: "device-detail-drawer__preview-type" }, record.typeName))),
      closeIcon: /* @__PURE__ */ import_react.default.createElement(import_icons.CloseOutlined, null),
      onCancel: onClose,
      okVisible: false,
      cancelText: getText("pisell2.saas.device-planning.preview.close"),
      cancelButtonProps: {
        type: "default",
        onClick: onClose
      },
      destroyOnClose: true,
      className: "device-detail-drawer__preview-modal",
      bodyStyle: { padding: "24px" }
    },
    /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { spinning: loading }, /* @__PURE__ */ import_react.default.createElement(import_DeviceInfoPanel.default, { record, getText }), /* @__PURE__ */ import_react.default.createElement("div", { className: "device-detail-drawer__preview-section" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "device-detail-drawer__preview-section-title" }, getText("pisell2.saas.device-planning.preview.reported-by-smart")), connections.length ? /* @__PURE__ */ import_react.default.createElement("div", { className: "device-detail-drawer__preview-connection-list" }, connections.map(renderConnectionItem)) : /* @__PURE__ */ import_react.default.createElement(
      import_antd.Empty,
      {
        image: import_antd.Empty.PRESENTED_IMAGE_SIMPLE,
        description: getText(
          "pisell2.saas.device-planning.drawer.connections.not-connected-smart"
        )
      }
    )))
  );
};
var ConnectedDevicePreviewModal_default = ConnectedDevicePreviewModal;
