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,
  buildConnectedFindListParams: () => buildConnectedFindListParams,
  buildFindListParams: () => buildFindListParams,
  buildNotConnectedFindListParams: () => buildNotConnectedFindListParams,
  buildPeripheralConnectParams: () => buildPeripheralConnectParams,
  createFindListSearchKey: () => createFindListSearchKey,
  createLocalPeripheralBinding: () => createLocalPeripheralBinding,
  createPeripheralRow: () => createPeripheralRow,
  getConnectionDeviceIdentity: () => getConnectionDeviceIdentity,
  getConnectionFactStatus: () => getConnectionFactStatus,
  getDisplayConnectionStatus: () => getDisplayConnectionStatus,
  getManualConnectionIp: () => getManualConnectionIp,
  getPeripheralIp: () => getPeripheralIp,
  getPeripheralUuid: () => getPeripheralUuid,
  getPhysicalDeviceDisplay: () => getPhysicalDeviceDisplay,
  getPhysicalDeviceFromConnection: () => getPhysicalDeviceFromConnection,
  isLabelPrinterConnection: () => isLabelPrinterConnection,
  isPeripheralSetupIncomplete: () => isPeripheralSetupIncomplete,
  isRecord: () => isRecord,
  matchesPeripheralDiscovery: () => matchesPeripheralDiscovery,
  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";
  if (normalized === "serial") return "SERIAL";
  return void 0;
};
var normalizeComparable = (value) => String(value || "").trim().toUpperCase();
var normalizeDeviceCategory = (value) => {
  const normalized = normalizeComparable(value);
  if (["PRINTER", "RECEIPT_PRINTER", "LABEL_PRINTER"].includes(normalized)) {
    return "PRINTER";
  }
  if (["SCANNER", "BARCODE_SCANNER"].includes(normalized)) {
    return "SCANNER";
  }
  return normalized;
};
var getPeripheralUuid = (connection) => {
  const data = connection.data;
  return typeof (data == null ? void 0 : data.uuid) === "string" ? data.uuid : "";
};
var getPeripheralIp = (connection) => {
  var _a;
  const data = connection.data;
  return (typeof (data == null ? void 0 : data.ip) === "string" ? data.ip : "") || (((_a = connection.config) == null ? void 0 : _a.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 getManualConnectionIp = (connection) => {
  var _a, _b;
  if (((_a = connection.config) == null ? void 0 : _a.connection_method) !== "manual") return "";
  return ((_b = connection.config) == null ? void 0 : _b.connection_ip) ? String(connection.config.connection_ip) : "";
};
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 buildConnectedFindListParams = () => ({
  deviceBrand: "other",
  connectStatus: "CONNECTED"
});
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 buildNotConnectedFindListParams = (connection) => buildFindListParams(connection, "NOT_CONNECT");
var createFindListSearchKey = (params) => [
  normalizeComparable(params.deviceBrand),
  normalizeDeviceCategory(params.deviceType),
  normalizeComparable(params.connectType),
  params.connectStatus,
  params.uuid || ""
].join("|");
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 getDeviceMetadata = (device) => isRecord(device.metadata) ? device.metadata : {};
var matchesPeripheralDiscovery = (connection, device) => {
  const identity = getConnectionDeviceIdentity(connection);
  if (!identity) return false;
  const metadata = getDeviceMetadata(device);
  const expectedBrand = normalizeComparable(identity.deviceBrand);
  const expectedType = normalizeDeviceCategory(identity.deviceType);
  const expectedConnectType = normalizeComparable(identity.connectType);
  const brandCandidates = [
    metadata.brand,
    metadata.device_brand,
    metadata.model_code,
    device.name
  ].map(normalizeComparable);
  const actualType = normalizeDeviceCategory(metadata.device_type);
  const actualConnectType = normalizeComparable(device.connect_type);
  return brandCandidates.some(
    (brand) => expectedBrand === "OTHER" || brand === expectedBrand || brand.includes(expectedBrand)
  ) && (!expectedType || actualType === expectedType) && actualConnectType === expectedConnectType;
};
var getConnectionFactStatus = (row) => {
  var _a, _b;
  if (!((_a = row.physical) == null ? void 0 : _a.uuid) && !((_b = row.physical) == null ? void 0 : _b.ip)) return "empty";
  return row.physical.is_connect ? "connected" : "disconnected";
};
var isLabelPrinterConnection = (connection) => {
  var _a;
  return ((_a = connection.target_device) == null ? void 0 : _a.type_code) === "label_printer";
};
var isPeripheralSetupIncomplete = (row) => {
  var _a;
  return getConnectionFactStatus(row) === "empty" || isLabelPrinterConnection(row.connection) && Boolean((_a = row.physical) == null ? void 0 : _a.is_connect) && !row.usage;
};
var getDisplayConnectionStatus = (factStatus, flowState) => {
  if ((flowState == null ? void 0 : flowState.step) === "connecting") return "connecting";
  if ((flowState == null ? void 0 : flowState.step) === "searching") return "searching";
  return factStatus === "connected" ? "connected" : "disconnected";
};
var getPhysicalDeviceFromConnection = (connection) => {
  var _a;
  const data = connection.data;
  if (!isRecord(data)) return void 0;
  const uuid = typeof data.uuid === "string" ? data.uuid : "";
  const ip = typeof data.ip === "string" ? data.ip : "";
  if (!uuid && !ip) return void 0;
  return {
    ...data,
    uuid,
    ip: 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((_a = connection.config) == null ? void 0 : _a.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) || void 0,
    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,
  buildConnectedFindListParams,
  buildFindListParams,
  buildNotConnectedFindListParams,
  buildPeripheralConnectParams,
  createFindListSearchKey,
  createLocalPeripheralBinding,
  createPeripheralRow,
  getConnectionDeviceIdentity,
  getConnectionFactStatus,
  getDisplayConnectionStatus,
  getManualConnectionIp,
  getPeripheralIp,
  getPeripheralUuid,
  getPhysicalDeviceDisplay,
  getPhysicalDeviceFromConnection,
  isLabelPrinterConnection,
  isPeripheralSetupIncomplete,
  isRecord,
  matchesPeripheralDiscovery,
  mergeConnectionRows,
  normalizeConnectType,
  uniquePeripheralDevices
});
