const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
//#region src/plus/saasDevice/appPeripheral/utils/peripheralRuntime.ts
const TESTABLE_PRINTER_TYPES = ["receipt_printer", "label_printer"];
const PERIPHERAL_TABLE_NAME = "peripheral_bindings";
const isRecord = (value) => Boolean(value && typeof value === "object" && !Array.isArray(value));
const 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";
};
const getPeripheralUuid = (connection) => {
	var _connection$target_de;
	const 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 : "";
};
const getPeripheralIp = (connection) => {
	var _connection$target_de2, _connection$config;
	const 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 ? void 0 : _connection$config.connection_ip) ? String(connection.config.connection_ip) : "");
};
const getPhysicalDeviceDisplay = (device) => (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) || "";
const getConnectionDeviceIdentity = (connection) => {
	var _connection$config2, _peripheral$model, _peripheral$model2;
	const peripheral = connection.target_device;
	const connectType = normalizeConnectType((_connection$config2 = connection.config) === null || _connection$config2 === void 0 ? void 0 : _connection$config2.connection_type);
	if (!peripheral || !connectType) return void 0;
	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
	};
};
const buildPeripheralConnectParams = (connection, target) => {
	const identity = getConnectionDeviceIdentity(connection);
	if (!identity) return void 0;
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, identity), target.uuid ? { uuid: target.uuid } : {}), target.ip ? { ip: target.ip } : {});
};
const buildFindListParams = (connection, connectStatus, uuid) => {
	const identity = getConnectionDeviceIdentity(connection);
	if (!identity) return void 0;
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, identity), {}, {
		connectStatus,
		uuid
	});
};
const uniquePeripheralDevices = (devices) => {
	const deviceMap = /* @__PURE__ */ new Map();
	devices.forEach((device) => {
		if (device === null || device === void 0 ? void 0 : device.uuid) deviceMap.set(device.uuid, device);
	});
	return Array.from(deviceMap.values());
};
const buildTargetDeviceData = (connection, physicalDevice) => {
	var _connection$target_de3, _connection$target_de4;
	const 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) || {} : {};
	const sourceMetadata = isRecord(sourceData.metadata) ? sourceData.metadata : {};
	const 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 require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, sourceData), physicalDevice), {}, { metadata: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, sourceMetadata), physicalMetadata) });
};
const getPhysicalDeviceFromConnection = (connection) => {
	var _connection$target_de5, _connection$config3;
	const 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 void 0;
	return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, 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((_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 : void 0
	});
};
const createLocalPeripheralBinding = (connection, physicalDevice, peripheralUsage) => {
	var _connection$target_de6, _connection$target_de7;
	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: ((_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，不转换后端字段命名。 */
const createPeripheralRow = (connection, localBinding) => ({
	id: connection.id,
	peripheralId: connection.target_device_id,
	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 实现数据隔离。 */
const 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 require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prevRow), {}, {
			id: connection.id,
			peripheralId: connection.target_device_id,
			connection,
			peripheral: connection.target_device
		});
	});
};
//#endregion
exports.PERIPHERAL_TABLE_NAME = PERIPHERAL_TABLE_NAME;
exports.TESTABLE_PRINTER_TYPES = TESTABLE_PRINTER_TYPES;
exports.buildFindListParams = buildFindListParams;
exports.buildPeripheralConnectParams = buildPeripheralConnectParams;
exports.buildTargetDeviceData = buildTargetDeviceData;
exports.createLocalPeripheralBinding = createLocalPeripheralBinding;
exports.getPeripheralIp = getPeripheralIp;
exports.getPeripheralUuid = getPeripheralUuid;
exports.getPhysicalDeviceDisplay = getPhysicalDeviceDisplay;
exports.getPhysicalDeviceFromConnection = getPhysicalDeviceFromConnection;
exports.mergeConnectionRows = mergeConnectionRows;
exports.uniquePeripheralDevices = uniquePeripheralDevices;
