import { getConnectionTypeLabel } from "../utils";
import { getPhysicalDeviceDisplay, TESTABLE_PRINTER_TYPES } from "./peripheralRuntime";
/** 基于 row 生成卡片展示模型，只做展示派生，不改变接口字段结构。 */
export var createPeripheralConnectionCardModel = function createPeripheralConnectionCardModel(_ref) {
  var _connection$config, _peripheral$type, _peripheral$workarea, _peripheral$model, _connection$config2, _peripheral$type2;
  var row = _ref.row,
    runtimeOverlay = _ref.runtimeOverlay,
    getText = _ref.getText,
    translation = _ref.translation;
  var connection = row.connection;
  var peripheral = row.peripheral || undefined;
  /** runtimeOverlay 是短暂交互态，row.physical 是稳定物理态。 */
  var physicalDevice = (runtimeOverlay === null || runtimeOverlay === void 0 ? void 0 : runtimeOverlay.physicalDevice) || row.physical;
  var status = function () {
    if ((runtimeOverlay === null || runtimeOverlay === void 0 ? void 0 : runtimeOverlay.status) === 'select') return 'connecting';
    if (runtimeOverlay !== null && runtimeOverlay !== void 0 && runtimeOverlay.status) return runtimeOverlay.status;
    if (physicalDevice) {
      return physicalDevice.is_connect ? 'connected' : 'disconnected';
    }
    return 'connecting';
  }();
  var isSelectAction = (runtimeOverlay === null || runtimeOverlay === void 0 ? void 0 : runtimeOverlay.status) === 'select';
  var isUsageAction = status === 'connected' && (peripheral === null || peripheral === void 0 ? void 0 : peripheral.type_code) === 'label_printer' && !row.usage;
  var connectionDetail = getPhysicalDeviceDisplay(physicalDevice) || ((_connection$config = connection.config) !== null && _connection$config !== void 0 && _connection$config.connection_ip ? String(connection.config.connection_ip) : '');
  var typeName = peripheral ? translation((_peripheral$type = peripheral.type) === null || _peripheral$type === void 0 ? void 0 : _peripheral$type.name) || peripheral.type_code : '-';
  var workAreaName = translation(peripheral === null || peripheral === void 0 || (_peripheral$workarea = peripheral.workarea) === null || _peripheral$workarea === void 0 ? void 0 : _peripheral$workarea.name);
  var profileIdSet = new Set((connection.profile_ids || []).map(Number));
  var selectedProfiles = (connection.profiles || []).filter(function (profile) {
    return profileIdSet.has(Number(profile.id));
  });
  return {
    connection: connection,
    peripheral: peripheral,
    physicalDevice: physicalDevice,
    status: status,
    title: peripheral ? translation(peripheral.name) || String(peripheral.id) : '-',
    subtitle: peripheral && workAreaName ? "".concat(typeName, " @ ").concat(workAreaName) : typeName,
    modelName: translation(peripheral === null || peripheral === void 0 || (_peripheral$model = peripheral.model) === null || _peripheral$model === void 0 ? void 0 : _peripheral$model.name) || (peripheral === null || peripheral === void 0 ? void 0 : peripheral.model_code) || '-',
    connectionTypeLabel: getConnectionTypeLabel((_connection$config2 = connection.config) === null || _connection$config2 === void 0 ? void 0 : _connection$config2.connection_type),
    connectionDetail: connectionDetail,
    profileLabels: selectedProfiles.length ? selectedProfiles.map(function (profile) {
      return translation(profile.name) || profile.id;
    }) : [],
    icon: (peripheral === null || peripheral === void 0 || (_peripheral$type2 = peripheral.type) === null || _peripheral$type2 === void 0 ? void 0 : _peripheral$type2.icon) || 'PrinterOutlined',
    canTest: status === 'connected' && !isUsageAction && TESTABLE_PRINTER_TYPES.includes((peripheral === null || peripheral === void 0 ? void 0 : peripheral.type_code) || ''),
    canConnect: status !== 'connected' || isUsageAction,
    isSelectAction: isSelectAction,
    isUsageAction: isUsageAction,
    actionText: isSelectAction ? getText('pisell2.saas.app-peripheral.select') : isUsageAction ? getText('pisell2.saas.app-peripheral.print-usage-title') : status === 'connecting' ? getText('pisell2.saas.app-peripheral.connecting') : getText('pisell2.saas.app-peripheral.connect'),
    actionDisabled: status === 'connecting' && !isSelectAction,
    canDisconnect: status === 'connected'
  };
};