function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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); }
export var TESTABLE_PRINTER_TYPES = ['receipt_printer', 'label_printer'];
export var PERIPHERAL_TABLE_NAME = 'peripheral_bindings';
export var isRecord = function isRecord(value) {
  return Boolean(value && _typeof(value) === 'object' && !Array.isArray(value));
};
export var normalizeConnectType = function normalizeConnectType(value) {
  var normalized = String(value || '').toLowerCase();
  if (normalized === 'usb') return 'USB';
  if (normalized === 'ethernet') return 'ETHERNET';
  if (normalized === 'wifi') return 'WIFI';
  if (normalized === 'bluetooth') return 'BLUETOOTH';
  return undefined;
};
export var getPeripheralUuid = function getPeripheralUuid(connection) {
  var _connection$target_de;
  var data = (_connection$target_de = connection.target_device) === null || _connection$target_de === void 0 ? void 0 : _connection$target_de.data;
  return typeof (data === null || data === void 0 ? void 0 : data.uuid) === 'string' ? data.uuid : '';
};
export var getPeripheralIp = function getPeripheralIp(connection) {
  var _connection$target_de2, _connection$config;
  var data = (_connection$target_de2 = connection.target_device) === null || _connection$target_de2 === void 0 ? void 0 : _connection$target_de2.data;
  return (typeof (data === null || data === void 0 ? void 0 : data.ip) === 'string' ? data.ip : '') || ((_connection$config = connection.config) !== null && _connection$config !== void 0 && _connection$config.connection_ip ? String(connection.config.connection_ip) : '');
};
export var getPhysicalDeviceDisplay = function getPhysicalDeviceDisplay(device) {
  return (device === null || device === void 0 ? void 0 : device.ip) || (device === null || device === void 0 ? void 0 : device.port) || (device === null || device === void 0 ? void 0 : device.mac) || '';
};
export var getConnectionDeviceIdentity = function getConnectionDeviceIdentity(connection) {
  var _connection$config2, _peripheral$model, _peripheral$model2;
  var peripheral = connection.target_device;
  var connectType = normalizeConnectType((_connection$config2 = connection.config) === null || _connection$config2 === void 0 ? void 0 : _connection$config2.connection_type);
  if (!peripheral || !connectType) return undefined;
  return {
    deviceBrand: ((_peripheral$model = peripheral.model) === null || _peripheral$model === void 0 || (_peripheral$model = _peripheral$model.brand) === null || _peripheral$model === void 0 ? void 0 : _peripheral$model.code) || ((_peripheral$model2 = peripheral.model) === null || _peripheral$model2 === void 0 ? void 0 : _peripheral$model2.brand_code) || peripheral.model_code || '',
    deviceType: peripheral.type_code || '',
    connectType: connectType
  };
};
export var buildPeripheralConnectParams = function buildPeripheralConnectParams(connection, target) {
  var identity = getConnectionDeviceIdentity(connection);
  if (!identity) return undefined;
  return _objectSpread(_objectSpread(_objectSpread({}, identity), target.uuid ? {
    uuid: target.uuid
  } : {}), target.ip ? {
    ip: target.ip
  } : {});
};
export var buildFindListParams = function buildFindListParams(connection, connectStatus, uuid) {
  var identity = getConnectionDeviceIdentity(connection);
  if (!identity) return undefined;
  return _objectSpread(_objectSpread({}, identity), {}, {
    connectStatus: connectStatus,
    uuid: uuid
  });
};
export var uniquePeripheralDevices = function uniquePeripheralDevices(devices) {
  var deviceMap = new Map();
  devices.forEach(function (device) {
    if (device !== null && device !== void 0 && device.uuid) {
      deviceMap.set(device.uuid, device);
    }
  });
  return Array.from(deviceMap.values());
};
export var buildTargetDeviceData = function buildTargetDeviceData(connection, physicalDevice) {
  var _connection$target_de3, _connection$target_de4;
  var sourceData = isRecord((_connection$target_de3 = connection.target_device) === null || _connection$target_de3 === void 0 ? void 0 : _connection$target_de3.data) ? ((_connection$target_de4 = connection.target_device) === null || _connection$target_de4 === void 0 ? void 0 : _connection$target_de4.data) || {} : {};
  var sourceMetadata = isRecord(sourceData.metadata) ? sourceData.metadata : {};
  var physicalMetadata = isRecord(physicalDevice === null || physicalDevice === void 0 ? void 0 : physicalDevice.metadata) ? (physicalDevice === null || physicalDevice === void 0 ? void 0 : physicalDevice.metadata) || {} : {};
  if (!physicalDevice) return sourceData;
  return _objectSpread(_objectSpread(_objectSpread({}, sourceData), physicalDevice), {}, {
    metadata: _objectSpread(_objectSpread({}, sourceMetadata), physicalMetadata)
  });
};
export var getPhysicalDeviceFromConnection = function getPhysicalDeviceFromConnection(connection) {
  var _connection$target_de5, _connection$config3;
  var data = (_connection$target_de5 = connection.target_device) === null || _connection$target_de5 === void 0 ? void 0 : _connection$target_de5.data;
  if (!isRecord(data) || typeof data.uuid !== 'string') return undefined;
  return _objectSpread(_objectSpread({}, data), {}, {
    uuid: data.uuid,
    ip: typeof data.ip === 'string' ? data.ip : undefined,
    port: typeof data.port === 'string' ? data.port : undefined,
    mac: typeof data.mac === 'string' ? data.mac : undefined,
    is_connect: Boolean(data.is_connect),
    name: typeof data.name === 'string' ? data.name : String(connection.target_device_id),
    connect_type: normalizeConnectType(String(data.connect_type || '')) || normalizeConnectType((_connection$config3 = connection.config) === null || _connection$config3 === void 0 ? void 0 : _connection$config3.connection_type) || 'USB',
    status_code: typeof data.status_code === 'string' ? data.status_code : '',
    metadata: isRecord(data.metadata) ? data.metadata : undefined
  });
};
export var createLocalPeripheralBinding = function createLocalPeripheralBinding(connection, physicalDevice, peripheralUsage) {
  var _connection$target_de6, _connection$target_de7;
  return {
    id: "connection_".concat(connection.id),
    connection_id: connection.id,
    source_device_id: connection.source_device_id,
    target_device_id: connection.target_device_id,
    type_code: ((_connection$target_de6 = connection.target_device) === null || _connection$target_de6 === void 0 ? void 0 : _connection$target_de6.type_code) || '',
    model_code: (_connection$target_de7 = connection.target_device) === null || _connection$target_de7 === void 0 ? void 0 : _connection$target_de7.model_code,
    physical_uuid: physicalDevice === null || physicalDevice === void 0 ? void 0 : physicalDevice.uuid,
    physical_device: physicalDevice,
    peripheral_usage: peripheralUsage,
    is_connect: Boolean(physicalDevice === null || physicalDevice === void 0 ? void 0 : physicalDevice.is_connect),
    updated_at: Date.now(),
    payload: connection
  };
};

/** 用接口 item 和本地缓存轻量组装渲染 row，不转换后端字段命名。 */
export var createPeripheralRow = function createPeripheralRow(connection, localBinding) {
  return {
    id: connection.id,
    peripheralId: connection.target_device_id,
    connection: connection,
    peripheral: connection.target_device,
    physical: getPhysicalDeviceFromConnection(connection) || (localBinding === null || localBinding === void 0 ? void 0 : localBinding.physical_device),
    usage: localBinding === null || localBinding === void 0 ? void 0 : localBinding.peripheral_usage
  };
};

/** 刷新连接列表时只替换云端字段，保留已有 physical/usage 实现数据隔离。 */
export var mergeConnectionRows = function mergeConnectionRows(prevRows, nextConnections) {
  var localBindingMap = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var prevRowMap = new Map(prevRows.map(function (row) {
    return [row.id, row];
  }));
  return nextConnections.map(function (connection) {
    var prevRow = prevRowMap.get(connection.id);
    if (!prevRow) {
      return createPeripheralRow(connection, localBindingMap[connection.id]);
    }
    return _objectSpread(_objectSpread({}, prevRow), {}, {
      id: connection.id,
      peripheralId: connection.target_device_id,
      connection: connection,
      peripheral: connection.target_device
    });
  });
};