const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_adapters = require("../../../model/adapters.js");
const require_utils = require("../../../shared/utils.js");
const require_DeviceInfoPanel = require("../../deviceDetail/components/DeviceInfoPanel.js");
const require_devices = require("../../../api/devices.js");
const require_deviceConnections = require("../../../api/deviceConnections.js");
const require_ConnectionDeviceCard = require("./ConnectionDeviceCard.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
let _ant_design_icons = require("@ant-design/icons");
//#region src/plus/saasDevice/devicePlanning/modules/deviceConnections/components/ConnectedDevicePreviewModal.tsx
const getStatusTone = (value) => {
	if (value === "online" || value === "connected" || value === "linked") return "success";
	if (value === "connecting") return "processing";
	if (value === "unlinked") return "warning";
	if (value === "disconnected") return "error";
	return "default";
};
const getDataValue = (data, key) => {
	if (!data) return void 0;
	const value = data[key];
	if (value == null || typeof value === "object") return void 0;
	return String(value);
};
const getNetworkLocalIp = (data) => {
	const network = data === null || data === void 0 ? void 0 : data.network;
	if (!network || typeof network !== "object" || Array.isArray(network)) return;
	return getDataValue(network, "local_ip");
};
const getConnectionLines = (connection) => {
	var _connection$config;
	const connectionType = require_utils.getConnectionTypeLabel((_connection$config = connection.config) === null || _connection$config === void 0 ? void 0 : _connection$config.connection_type);
	const parts = [connectionType === "-" ? void 0 : connectionType];
	const connectionData = connection.data;
	const ip = getDataValue(connectionData, "ip") || getNetworkLocalIp(connectionData);
	const port = getDataValue(connectionData, "port");
	const macAddress = getDataValue(connectionData, "mac");
	if (ip) parts.push(`IP: ${ip}`);
	if (port) parts.push(`Port: ${port}`);
	if (macAddress) parts.push(`MAC: ${macAddress}`);
	return parts.filter(Boolean).length ? [parts.filter(Boolean).join(" · ")] : [];
};
const ConnectedDevicePreviewModal = ({ open, device, connectionType, getText, translation, onClose }) => {
	var _detailDevice$type;
	const [detailDevice, setDetailDevice] = (0, react.useState)(device);
	const [connections, setConnections] = (0, react.useState)([]);
	const [loading, setLoading] = (0, react.useState)(false);
	const record = require_adapters.createDeviceRow(detailDevice, translation);
	const getStatusLabel = (value) => {
		if (!value) return "-";
		const key = `pisell2.saas.device-planning.status.${value}`;
		const text = getText(key);
		return text && text !== key ? text : value;
	};
	(0, react.useEffect)(() => {
		if (!open || !device.id) return;
		let cancelled = false;
		setDetailDevice(device);
		setConnections([]);
		setLoading(true);
		Promise.all([require_devices.getDeviceDetail(device.id), require_deviceConnections.getDeviceConnections({
			type: connectionType,
			target_device_id: device.id,
			with: [
				"sourceDevice.type",
				"sourceDevice.model.brand",
				"sourceDevice.workarea",
				"profiles"
			]
		})]).then(([detail, list]) => {
			if (cancelled) return;
			setDetailDevice(detail || device);
			setConnections(list);
		}).catch(() => {
			if (cancelled) return;
			setDetailDevice(device);
			setConnections([]);
		}).finally(() => {
			if (!cancelled) setLoading(false);
		});
		return () => {
			cancelled = true;
		};
	}, [
		connectionType,
		device,
		device.id,
		open
	]);
	const renderConnectionItem = (connection) => {
		var _smartDevice$type, _smartDevice$model, _smartDevice$model2, _smartDevice$model3, _smartDevice$type2;
		const smartDevice = connection.source_device;
		return /* @__PURE__ */ react.default.createElement(require_ConnectionDeviceCard.default, {
			key: connection.id,
			title: smartDevice ? translation(smartDevice.name) || smartDevice.id : "-",
			deviceInfo: {
				icon: (smartDevice === null || smartDevice === void 0 || (_smartDevice$type = smartDevice.type) === null || _smartDevice$type === void 0 ? void 0 : _smartDevice$type.icon) || "DesktopOutlined",
				modelName: translation(smartDevice === null || smartDevice === void 0 || (_smartDevice$model = smartDevice.model) === null || _smartDevice$model === void 0 ? void 0 : _smartDevice$model.name) || (smartDevice === null || smartDevice === void 0 ? void 0 : smartDevice.model_code) || "-",
				brandName: translation(smartDevice === null || smartDevice === void 0 || (_smartDevice$model2 = smartDevice.model) === null || _smartDevice$model2 === void 0 || (_smartDevice$model2 = _smartDevice$model2.brand) === null || _smartDevice$model2 === void 0 ? void 0 : _smartDevice$model2.name) || (smartDevice === null || smartDevice === void 0 || (_smartDevice$model3 = smartDevice.model) === null || _smartDevice$model3 === void 0 ? void 0 : _smartDevice$model3.brand_code) || "-",
				typeName: translation(smartDevice === null || smartDevice === void 0 || (_smartDevice$type2 = smartDevice.type) === null || _smartDevice$type2 === void 0 ? void 0 : _smartDevice$type2.name) || (smartDevice === null || smartDevice === void 0 ? void 0 : smartDevice.type_code) || "-",
				statusLines: []
			},
			connectionInfo: { lines: getConnectionLines(connection) },
			deviceStatuses: [{
				key: "link",
				label: getStatusLabel((smartDevice === null || smartDevice === void 0 ? void 0 : smartDevice.link) || "unconnected"),
				tone: getStatusTone((smartDevice === null || smartDevice === void 0 ? void 0 : smartDevice.link) || "unconnected")
			}, {
				key: "online",
				label: getStatusLabel((smartDevice === null || smartDevice === void 0 ? void 0 : smartDevice.status) || "unknown"),
				tone: getStatusTone((smartDevice === null || smartDevice === void 0 ? void 0 : smartDevice.status) || "unknown")
			}],
			connectionStatuses: [{
				key: "connection",
				label: getStatusLabel(connection.status || "unconnected"),
				tone: getStatusTone(connection.status || "unconnected")
			}],
			profiles: (connection.profiles || []).map((profile) => ({
				key: String(profile.id),
				label: translation(profile.name) || profile.id
			})),
			lastSeen: connection.last_seen ? `${getText("pisell2.saas.device-planning.detail.last-seen")}${connection.last_seen}` : void 0
		});
	};
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open,
		width: 760,
		title: /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__preview-title" }, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__preview-icon" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
			type: ((_detailDevice$type = detailDevice.type) === null || _detailDevice$type === void 0 ? void 0 : _detailDevice$type.icon) || "PrinterOutlined",
			color: require_utils.DEVICE_ICON_COLOR[detailDevice.type_kind]
		})), /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__preview-name" }, record.name), /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__preview-type" }, record.typeName))),
		closeIcon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.CloseOutlined, null),
		onCancel: onClose,
		okVisible: false,
		cancelText: getText("pisell2.saas.device-planning.preview.close"),
		cancelButtonProps: {
			type: "default",
			onClick: onClose
		},
		destroyOnClose: true,
		className: "device-detail-drawer__preview-modal",
		bodyStyle: { padding: "24px" }
	}, /* @__PURE__ */ react.default.createElement(antd.Spin, { spinning: loading }, /* @__PURE__ */ react.default.createElement(require_DeviceInfoPanel.default, {
		record,
		getText
	}), /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__preview-section" }, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__preview-section-title" }, getText("pisell2.saas.device-planning.preview.reported-by-smart")), connections.length ? /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__preview-connection-list" }, connections.map(renderConnectionItem)) : /* @__PURE__ */ react.default.createElement(antd.Empty, {
		image: antd.Empty.PRESENTED_IMAGE_SIMPLE,
		description: getText("pisell2.saas.device-planning.drawer.connections.not-connected-smart")
	}))));
};
//#endregion
exports.default = ConnectedDevicePreviewModal;
