var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saasDevice/appPeripheral/utils/peripheralRuntime.ts
var peripheralRuntime_exports = {};
__export(peripheralRuntime_exports, {
  PERIPHERAL_TABLE_NAME: () => PERIPHERAL_TABLE_NAME,
  TESTABLE_PRINTER_TYPES: () => TESTABLE_PRINTER_TYPES,
  buildFindListParams: () => buildFindListParams,
  buildPeripheralConnectParams: () => buildPeripheralConnectParams,
  buildTargetDeviceData: () => buildTargetDeviceData,
  createLocalPeripheralBinding: () => createLocalPeripheralBinding,
  createPeripheralRow: () => createPeripheralRow,
  getConnectionDeviceIdentity: () => getConnectionDeviceIdentity,
  getPeripheralIp: () => getPeripheralIp,
  getPeripheralUuid: () => getPeripheralUuid,
  getPhysicalDeviceDisplay: () => getPhysicalDeviceDisplay,
  getPhysicalDeviceFromConnection: () => getPhysicalDeviceFromConnection,
  isRecord: () => isRecord,
  mergeConnectionRows: () => mergeConnectionRows,
  normalizeConnectType: () => normalizeConnectType,
  uniquePeripheralDevices: () => uniquePeripheralDevices
});
module.exports = __toCommonJS(peripheralRuntime_exports);
var TESTABLE_PRINTER_TYPES = ["receipt_printer", "label_printer"];
var PERIPHERAL_TABLE_NAME = "peripheral_bindings";
var isRecord = (value) => Boolean(value && typeof value === "object" && !Array.isArray(value));
var normalizeConnectType = (value) => {
  const 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 void 0;
};
var getPeripheralUuid = (connection) => {
  var _a;
  const data = (_a = connection.target_device) == null ? void 0 : _a.data;
  return typeof (data == null ? void 0 : data.uuid) === "string" ? data.uuid : "";
};
var getPeripheralIp = (connection) => {
  var _a, _b;
  const data = (_a = connection.target_device) == null ? void 0 : _a.data;
  return (typeof (data == null ? void 0 : data.ip) === "string" ? data.ip : "") || (((_b = connection.config) == null ? void 0 : _b.connection_ip) ? String(connection.config.connection_ip) : "");
};
var getPhysicalDeviceDisplay = (device) => (device == null ? void 0 : device.ip) || (device == null ? void 0 : device.port) || (device == null ? void 0 : device.mac) || "";
var getConnectionDeviceIdentity = (connection) => {
  var _a, _b, _c, _d;
  const peripheral = connection.target_device;
  const connectType = normalizeConnectType((_a = connection.config) == null ? void 0 : _a.connection_type);
  if (!peripheral || !connectType) return void 0;
  return {
    deviceBrand: ((_c = (_b = peripheral.model) == null ? void 0 : _b.brand) == null ? void 0 : _c.code) || ((_d = peripheral.model) == null ? void 0 : _d.brand_code) || peripheral.model_code || "",
    deviceType: peripheral.type_code || "",
    connectType
  };
};
var buildPeripheralConnectParams = (connection, target) => {
  const identity = getConnectionDeviceIdentity(connection);
  if (!identity) return void 0;
  return {
    ...identity,
    ...target.uuid ? { uuid: target.uuid } : {},
    ...target.ip ? { ip: target.ip } : {}
  };
};
var buildFindListParams = (connection, connectStatus, uuid) => {
  const identity = getConnectionDeviceIdentity(connection);
  if (!identity) return void 0;
  return {
    ...identity,
    connectStatus,
    uuid
  };
};
var uniquePeripheralDevices = (devices) => {
  const deviceMap = /* @__PURE__ */ new Map();
  devices.forEach((device) => {
    if (device == null ? void 0 : device.uuid) {
      deviceMap.set(device.uuid, device);
    }
  });
  return Array.from(deviceMap.values());
};
var buildTargetDeviceData = (connection, physicalDevice) => {
  var _a, _b;
  const sourceData = isRecord((_a = connection.target_device) == null ? void 0 : _a.data) ? ((_b = connection.target_device) == null ? void 0 : _b.data) || {} : {};
  const sourceMetadata = isRecord(sourceData.metadata) ? sourceData.metadata : {};
  const physicalMetadata = isRecord(physicalDevice == null ? void 0 : physicalDevice.metadata) ? (physicalDevice == null ? void 0 : physicalDevice.metadata) || {} : {};
  if (!physicalDevice) return sourceData;
  return {
    ...sourceData,
    ...physicalDevice,
    metadata: {
      ...sourceMetadata,
      ...physicalMetadata
    }
  };
};
var getPhysicalDeviceFromConnection = (connection) => {
  var _a, _b;
  const data = (_a = connection.target_device) == null ? void 0 : _a.data;
  if (!isRecord(data) || typeof data.uuid !== "string") return void 0;
  return {
    ...data,
    uuid: data.uuid,
    ip: typeof data.ip === "string" ? data.ip : void 0,
    port: typeof data.port === "string" ? data.port : void 0,
    mac: typeof data.mac === "string" ? data.mac : void 0,
    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((_b = connection.config) == null ? void 0 : _b.connection_type) || "USB",
    status_code: typeof data.status_code === "string" ? data.status_code : "",
    metadata: isRecord(data.metadata) ? data.metadata : void 0
  };
};
var createLocalPeripheralBinding = (connection, physicalDevice, peripheralUsage) => {
  var _a, _b;
  return {
    id: `connection_${connection.id}`,
    connection_id: connection.id,
    source_device_id: connection.source_device_id,
    target_device_id: connection.target_device_id,
    type_code: ((_a = connection.target_device) == null ? void 0 : _a.type_code) || "",
    model_code: (_b = connection.target_device) == null ? void 0 : _b.model_code,
    physical_uuid: physicalDevice == null ? void 0 : physicalDevice.uuid,
    physical_device: physicalDevice,
    peripheral_usage: peripheralUsage,
    is_connect: Boolean(physicalDevice == null ? void 0 : physicalDevice.is_connect),
    updated_at: Date.now(),
    payload: connection
  };
};
var createPeripheralRow = (connection, localBinding) => ({
  id: connection.id,
  peripheralId: connection.target_device_id,
  connection,
  peripheral: connection.target_device,
  physical: getPhysicalDeviceFromConnection(connection) || (localBinding == null ? void 0 : localBinding.physical_device),
  usage: localBinding == null ? void 0 : localBinding.peripheral_usage
});
var mergeConnectionRows = (prevRows, nextConnections, localBindingMap = {}) => {
  const prevRowMap = new Map(prevRows.map((row) => [row.id, row]));
  return nextConnections.map((connection) => {
    const prevRow = prevRowMap.get(connection.id);
    if (!prevRow) {
      return createPeripheralRow(connection, localBindingMap[connection.id]);
    }
    return {
      ...prevRow,
      id: connection.id,
      peripheralId: connection.target_device_id,
      connection,
      peripheral: connection.target_device
    };
  });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PERIPHERAL_TABLE_NAME,
  TESTABLE_PRINTER_TYPES,
  buildFindListParams,
  buildPeripheralConnectParams,
  buildTargetDeviceData,
  createLocalPeripheralBinding,
  createPeripheralRow,
  getConnectionDeviceIdentity,
  getPeripheralIp,
  getPeripheralUuid,
  getPhysicalDeviceDisplay,
  getPhysicalDeviceFromConnection,
  isRecord,
  mergeConnectionRows,
  normalizeConnectType,
  uniquePeripheralDevices
});
