const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_LinkStatusTag = require("../../../components/shared/LinkStatusTag.js");
const require_OnlineStatus = require("../../../components/shared/OnlineStatus.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./DeviceInfoPanel.less");
//#region src/plus/saasDevice/devicePlanning/modules/deviceDetail/components/DeviceInfoPanel.tsx
const EMPTY_VALUE = "-";
const BYTE_PER_GB = 1024 * 1024 * 1024;
const REPORT_GROUPS = [
	{
		key: "software",
		fields: [{ key: "name" }, { key: "version" }]
	},
	{
		key: "battery",
		fields: [{
			key: "level",
			value: (data, getText) => renderBatteryValue(data, getText)
		}]
	},
	{
		key: "network",
		fields: [
			{ key: "type" },
			{ key: "local_ip" },
			{ key: "gateway" },
			{ key: "subnet_mask" },
			{ key: "ssid" }
		]
	},
	{
		key: "hardware",
		fields: [{ key: "model" }, { key: "os_version" }]
	},
	{
		key: "storage",
		fields: [{
			key: "used",
			value: (data) => formatStorageValue(data.used)
		}, {
			key: "total",
			value: (data) => formatStorageValue(data.total)
		}]
	}
];
const isPlainObject = (value) => Object.prototype.toString.call(value) === "[object Object]";
const getReportText = (getText, key) => getText(`pisell2.saas.device-planning.detail.report.${key}`);
const formatReportValue = (value, getText) => {
	if (value === null || value === void 0 || value === "") return EMPTY_VALUE;
	if (typeof value === "boolean") return getText(`pisell2.saas.device-planning.detail.${value ? "yes" : "no"}`);
	if (Array.isArray(value) || isPlainObject(value)) return /* @__PURE__ */ react.default.createElement("span", { className: "device-detail-drawer__json-value" }, JSON.stringify(value));
	return String(value);
};
const formatStorageValue = (value) => {
	const byteValue = typeof value === "number" || typeof value === "string" ? Number(value) : NaN;
	if (!Number.isFinite(byteValue)) return EMPTY_VALUE;
	const gbValue = byteValue / BYTE_PER_GB;
	return `${gbValue >= 10 ? String(Math.round(gbValue)) : gbValue.toFixed(2).replace(/\.?0+$/, "")} GB`;
};
const formatLastSeen = (value) => typeof value === "string" && value.trim() ? value : EMPTY_VALUE;
const renderBatteryValue = (data, getText) => {
	const rawLevel = typeof data.level === "number" || typeof data.level === "string" ? Number(data.level) : NaN;
	const hasLevel = Number.isFinite(rawLevel);
	const levelValue = hasLevel ? Math.max(0, Math.min(rawLevel, 100)) : 0;
	const level = hasLevel ? `${rawLevel}%` : EMPTY_VALUE;
	const charging = (data === null || data === void 0 ? void 0 : data.charging) === true;
	return /* @__PURE__ */ react.default.createElement("span", { className: "device-detail-drawer__battery-value" }, /* @__PURE__ */ react.default.createElement("span", { className: (0, classnames.default)("device-detail-drawer__battery-icon", { "is-charging": charging }) }, /* @__PURE__ */ react.default.createElement("span", {
		className: "device-detail-drawer__battery-fill",
		style: { width: `${levelValue}%` }
	}), charging ? /* @__PURE__ */ react.default.createElement("span", { className: "device-detail-drawer__battery-bolt" }) : null), /* @__PURE__ */ react.default.createElement("span", null, level), /* @__PURE__ */ react.default.createElement("span", { className: "device-detail-drawer__battery-status" }, getReportText(getText, charging ? "charging" : "not-charging")));
};
const DeviceInfoPanel = ({ record, getText }) => {
	const device = record._raw;
	const showAdvancedInfo = device.type_kind === "smart";
	const showStatusInfo = device.type_kind === "smart" || device.type_kind === "dumb";
	const lastSeen = formatLastSeen(device.last_seen);
	const reportGroups = (0, react.useMemo)(() => {
		if (!isPlainObject(device.data)) return [];
		return REPORT_GROUPS.reduce((list, group) => {
			var _device$data;
			const data = (_device$data = device.data) === null || _device$data === void 0 ? void 0 : _device$data[group.key];
			if (isPlainObject(data)) list.push(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, group), {}, { data }));
			return list;
		}, []);
	}, [device.data]);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Card, {
		size: "small",
		title: getText("pisell2.saas.device-planning.detail.basic-info"),
		className: "device-detail-drawer__card"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__info-list" }, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__info-row" }, /* @__PURE__ */ react.default.createElement("span", null, getText("pisell2.saas.device-planning.detail.name")), /* @__PURE__ */ react.default.createElement("strong", null, record.name)), /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__info-row" }, /* @__PURE__ */ react.default.createElement("span", null, getText("pisell2.saas.device-planning.detail.type")), /* @__PURE__ */ react.default.createElement("strong", null, record.typeName)), /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__info-row" }, /* @__PURE__ */ react.default.createElement("span", null, getText("pisell2.saas.device-planning.detail.model")), /* @__PURE__ */ react.default.createElement("strong", null, record.modelName)), showAdvancedInfo ? /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__info-two-row" }, /* @__PURE__ */ react.default.createElement("span", null, getText("pisell2.saas.device-planning.detail.profile")), /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__profile-card" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, { type: "FileTextOutlined" }), record.profileName)) : null)), showStatusInfo ? /* @__PURE__ */ react.default.createElement(antd.Card, {
		size: "small",
		title: getText("pisell2.saas.device-planning.detail.status"),
		className: "device-detail-drawer__card"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__info-list" }, showAdvancedInfo ? /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__info-row" }, /* @__PURE__ */ react.default.createElement("span", null, getText("pisell2.saas.device-planning.detail.link-status")), /* @__PURE__ */ react.default.createElement(require_LinkStatusTag.default, {
		value: record.link,
		getText,
		style: { marginRight: 0 }
	})) : null, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__info-row" }, /* @__PURE__ */ react.default.createElement("span", null, getText("pisell2.saas.device-planning.detail.online-status")), /* @__PURE__ */ react.default.createElement(require_OnlineStatus.default, {
		value: record.status,
		getText,
		className: "device-detail-drawer__online-status",
		dotClassName: "device-detail-drawer__status-dot"
	})))) : null, showAdvancedInfo ? /* @__PURE__ */ react.default.createElement(antd.Card, {
		size: "small",
		title: getText("pisell2.saas.device-planning.detail.report-info"),
		extra: /* @__PURE__ */ react.default.createElement("span", { className: "device-detail-drawer__last-seen" }, getText("pisell2.saas.device-planning.detail.last-seen"), lastSeen),
		className: "device-detail-drawer__card"
	}, reportGroups.length > 0 ? /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__report-list" }, reportGroups.map((group) => /* @__PURE__ */ react.default.createElement(antd.Card, {
		key: group.key,
		size: "small",
		type: "inner",
		title: getReportText(getText, `group.${group.key}`),
		className: "device-detail-drawer__report-card"
	}, /* @__PURE__ */ react.default.createElement("div", { className: "device-detail-drawer__report-rows" }, group.fields.map((field) => /* @__PURE__ */ react.default.createElement("div", {
		key: field.key,
		className: "device-detail-drawer__report-row"
	}, /* @__PURE__ */ react.default.createElement("span", { className: "device-detail-drawer__report-label" }, getReportText(getText, `field.${field.key}`)), /* @__PURE__ */ react.default.createElement("span", { className: "device-detail-drawer__report-value" }, field.value ? field.value(group.data, getText) : formatReportValue(group.data[field.key], getText)))))))) : /* @__PURE__ */ react.default.createElement(antd.Empty, {
		image: antd.Empty.PRESENTED_IMAGE_SIMPLE,
		description: getText("pisell2.saas.device-planning.detail.no-report-info")
	})) : null);
};
//#endregion
exports.default = DeviceInfoPanel;
