import { _asyncToGenerator } from "../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { createDeviceConnection, deleteDeviceConnection, getDeviceConnections, updateDeviceConnection } from "../../../serve.js";
import { useEffect } from "react";
import { useRequest } from "ahooks";
//#region src/plus/saasDevice/devicePlanning/components/DeviceDetailDrawer/hooks/useDeviceConnections.ts
/** 管理详情抽屉连接页的连接列表加载和连接增删改请求。 */
const useDeviceConnections = ({ open, activeTab, activeKind, displayRecord, onConnectionsChanged }) => {
	const connectionRequest = useRequest(getDeviceConnections, { manual: true });
	const createConnectionRequest = useRequest(createDeviceConnection, { manual: true });
	const updateConnectionRequest = useRequest((params) => updateDeviceConnection(params.id, params.payload), { manual: true });
	const deleteConnectionRequest = useRequest(deleteDeviceConnection, { manual: true });
	/** 根据当前设备种类生成正确的 smart/dumb 过滤参数并刷新连接列表。 */
	const reloadConnections = () => {
		if (!displayRecord || activeKind === "integrated") return;
		const relationWith = activeKind === "smart" ? [
			"targetDevice.type",
			"targetDevice.model.brand",
			"targetDevice.workarea",
			"profiles"
		] : ["sourceDevice.type", "profiles"];
		connectionRequest.run({
			type: "dumb",
			source_device_id: activeKind === "smart" ? displayRecord.id : void 0,
			target_device_id: activeKind === "dumb" ? displayRecord.id : void 0,
			with: relationWith
		});
	};
	useEffect(() => {
		if (!open || !displayRecord) return;
		if (activeTab !== "connections") return;
		if (activeKind === "integrated") return;
		reloadConnections();
	}, [
		activeKind,
		activeTab,
		displayRecord === null || displayRecord === void 0 ? void 0 : displayRecord.id,
		open
	]);
	/** 创建连接后刷新连接列表，并通知父级刷新设备 link/status。 */
	const handleCreateConnection = function() {
		var _ref = _asyncToGenerator(function* (payload) {
			yield createConnectionRequest.runAsync(payload);
			reloadConnections();
			onConnectionsChanged === null || onConnectionsChanged === void 0 || onConnectionsChanged();
		});
		return function handleCreateConnection(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	/** 更新连接配置后刷新连接列表，并通知父级同步设备状态。 */
	const handleUpdateConnection = function() {
		var _ref2 = _asyncToGenerator(function* (id, payload) {
			yield updateConnectionRequest.runAsync({
				id,
				payload
			});
			reloadConnections();
			onConnectionsChanged === null || onConnectionsChanged === void 0 || onConnectionsChanged();
		});
		return function handleUpdateConnection(_x2, _x3) {
			return _ref2.apply(this, arguments);
		};
	}();
	/** 删除连接后刷新连接列表，并通知父级同步设备状态。 */
	const handleDeleteConnection = function() {
		var _ref3 = _asyncToGenerator(function* (id) {
			yield deleteConnectionRequest.runAsync(id);
			reloadConnections();
			onConnectionsChanged === null || onConnectionsChanged === void 0 || onConnectionsChanged();
		});
		return function handleDeleteConnection(_x4) {
			return _ref3.apply(this, arguments);
		};
	}();
	return {
		connections: connectionRequest.data || [],
		loading: connectionRequest.loading,
		submitting: createConnectionRequest.loading || updateConnectionRequest.loading,
		deleting: deleteConnectionRequest.loading,
		handleCreateConnection,
		handleUpdateConnection,
		handleDeleteConnection
	};
};
//#endregion
export { useDeviceConnections };
