var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/saasDevice/devicePlanning/components/DeviceDetailDrawer/hooks/useDeviceConnections.ts
var useDeviceConnections_exports = {};
__export(useDeviceConnections_exports, {
  useDeviceConnections: () => useDeviceConnections
});
module.exports = __toCommonJS(useDeviceConnections_exports);
var import_react = require("react");
var import_ahooks = require("ahooks");
var import_serve = require("../../../serve");
var useDeviceConnections = ({
  open,
  activeTab,
  activeKind,
  displayRecord,
  onConnectionsChanged
}) => {
  const connectionRequest = (0, import_ahooks.useRequest)(import_serve.getDeviceConnections, { manual: true });
  const createConnectionRequest = (0, import_ahooks.useRequest)(import_serve.createDeviceConnection, {
    manual: true
  });
  const updateConnectionRequest = (0, import_ahooks.useRequest)(
    (params) => (0, import_serve.updateDeviceConnection)(params.id, params.payload),
    { manual: true }
  );
  const deleteConnectionRequest = (0, import_ahooks.useRequest)(import_serve.deleteDeviceConnection, {
    manual: true
  });
  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
    });
  };
  (0, import_react.useEffect)(() => {
    if (!open || !displayRecord) return;
    if (activeTab !== "connections") return;
    if (activeKind === "integrated") return;
    reloadConnections();
  }, [activeKind, activeTab, displayRecord == null ? void 0 : displayRecord.id, open]);
  const handleCreateConnection = async (payload) => {
    await createConnectionRequest.runAsync(payload);
    reloadConnections();
    onConnectionsChanged == null ? void 0 : onConnectionsChanged();
  };
  const handleUpdateConnection = async (id, payload) => {
    await updateConnectionRequest.runAsync({ id, payload });
    reloadConnections();
    onConnectionsChanged == null ? void 0 : onConnectionsChanged();
  };
  const handleDeleteConnection = async (id) => {
    await deleteConnectionRequest.runAsync(id);
    reloadConnections();
    onConnectionsChanged == null ? void 0 : onConnectionsChanged();
  };
  return {
    connections: connectionRequest.data || [],
    loading: connectionRequest.loading,
    submitting: createConnectionRequest.loading || updateConnectionRequest.loading,
    deleting: deleteConnectionRequest.loading,
    handleCreateConnection,
    handleUpdateConnection,
    handleDeleteConnection
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useDeviceConnections
});
