import { TESTABLE_PRINTER_TYPES, getPhysicalDeviceDisplay } from "./peripheralRuntime.js";
import { getConnectionTypeLabel } from "../utils.js";
//#region src/plus/saasDevice/appPeripheral/utils/peripheralCardModel.ts
/** 基于 row 生成卡片展示模型，只做展示派生，不改变接口字段结构。 */
const createPeripheralConnectionCardModel = ({ row, runtimeOverlay, getText, translation }) => {
	var _connection$config, _peripheral$type, _peripheral$workarea, _peripheral$model, _connection$config2, _peripheral$type2;
	const { connection } = row;
	const peripheral = row.peripheral || void 0;
	/** runtimeOverlay 是短暂交互态，row.physical 是稳定物理态。 */
	const physicalDevice = (runtimeOverlay === null || runtimeOverlay === void 0 ? void 0 : runtimeOverlay.physicalDevice) || row.physical;
	const status = (() => {
		if ((runtimeOverlay === null || runtimeOverlay === void 0 ? void 0 : runtimeOverlay.status) === "select") return "connecting";
		if (runtimeOverlay === null || runtimeOverlay === void 0 ? void 0 : runtimeOverlay.status) return runtimeOverlay.status;
		if (physicalDevice) return physicalDevice.is_connect ? "connected" : "disconnected";
		return "connecting";
	})();
	const isSelectAction = (runtimeOverlay === null || runtimeOverlay === void 0 ? void 0 : runtimeOverlay.status) === "select";
	const isUsageAction = status === "connected" && (peripheral === null || peripheral === void 0 ? void 0 : peripheral.type_code) === "label_printer" && !row.usage;
	const connectionDetail = getPhysicalDeviceDisplay(physicalDevice) || (((_connection$config = connection.config) === null || _connection$config === void 0 ? void 0 : _connection$config.connection_ip) ? String(connection.config.connection_ip) : "");
	const typeName = peripheral ? translation((_peripheral$type = peripheral.type) === null || _peripheral$type === void 0 ? void 0 : _peripheral$type.name) || peripheral.type_code : "-";
	const workAreaName = translation(peripheral === null || peripheral === void 0 || (_peripheral$workarea = peripheral.workarea) === null || _peripheral$workarea === void 0 ? void 0 : _peripheral$workarea.name);
	const profileIdSet = new Set((connection.profile_ids || []).map(Number));
	const selectedProfiles = (connection.profiles || []).filter((profile) => profileIdSet.has(Number(profile.id)));
	return {
		connection,
		peripheral,
		physicalDevice,
		status,
		title: peripheral ? translation(peripheral.name) || String(peripheral.id) : "-",
		subtitle: peripheral && workAreaName ? `${typeName} @ ${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,
		profileLabels: selectedProfiles.length ? selectedProfiles.map((profile) => 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,
		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"
	};
};
//#endregion
export { createPeripheralConnectionCardModel };
