require("../../../../../../_virtual/_rolldown/runtime.js");
const require_serve = require("../../../serve.js");
const require_utils = require("../../../utils.js");
let react = require("react");
//#region src/plus/saasDevice/devicePlanning/components/DeviceDetailDrawer/hooks/useDeviceDetail.ts
/** 管理详情抽屉的当前 tab、设备详情补拉取和 integrated 设备 tab 保护。 */
const useDeviceDetail = ({ open, record, initialTab, translation }) => {
	const [activeTab, setActiveTab] = (0, react.useState)("info");
	const [detailRecord, setDetailRecord] = (0, react.useState)();
	const detailFetchedIdRef = (0, react.useRef)();
	const displayRecord = detailRecord || record;
	const activeKind = (displayRecord === null || displayRecord === void 0 ? void 0 : displayRecord._raw.type_kind) || "smart";
	(0, react.useEffect)(() => {
		if (open) {
			setActiveTab(initialTab);
			setDetailRecord(record);
			detailFetchedIdRef.current = void 0;
		}
	}, [
		initialTab,
		open,
		record === null || record === void 0 ? void 0 : record.id
	]);
	(0, react.useEffect)(() => {
		if (!open || !(record === null || record === void 0 ? void 0 : record.id)) return;
		if (!(activeTab === "info" || detailFetchedIdRef.current !== record.id)) return;
		detailFetchedIdRef.current = record.id;
		require_serve.getDeviceDetail(record.id).then((detail) => {
			if (detail) setDetailRecord(require_utils.createDeviceRow(detail, translation));
		}).catch(() => {
			setDetailRecord(record);
		});
	}, [
		activeTab,
		open,
		record,
		record === null || record === void 0 ? void 0 : record.id,
		translation
	]);
	(0, react.useEffect)(() => {
		if (activeKind === "integrated" && activeTab === "connections") setActiveTab("info");
	}, [activeKind, activeTab]);
	return {
		activeTab,
		activeKind,
		displayRecord,
		setActiveTab
	};
};
//#endregion
exports.useDeviceDetail = useDeviceDetail;
