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/appDevicePlanning/hooks/useAppDeviceModels.ts
var useAppDeviceModels_exports = {};
__export(useAppDeviceModels_exports, {
  useAppDeviceModels: () => useAppDeviceModels
});
module.exports = __toCommonJS(useAppDeviceModels_exports);
var import_react = require("react");
var import_ahooks = require("ahooks");
var import_api = require("../api");
var import_model = require("../model");
var useAppDeviceModels = ({
  kind,
  showBack,
  iotEndpoint,
  translation
}) => {
  var _a, _b;
  const [typeCode, setTypeCode] = (0, import_react.useState)(() => (0, import_model.getStoredTypeCode)());
  const [modelCode, setModelCode] = (0, import_react.useState)(() => (0, import_model.getStoredModelCode)());
  const isModelSelecting = !typeCode || !modelCode;
  const deviceTypeRequest = (0, import_ahooks.useRequest)(
    () => (0, import_api.getDeviceTypes)({
      kind,
      with: ["activeModels", "activeModels.activeProfiles"]
    }),
    { manual: true }
  );
  const modelRequest = (0, import_ahooks.useRequest)(import_api.getDeviceModel, { manual: true });
  (0, import_react.useEffect)(() => {
    if (!isModelSelecting) return;
    deviceTypeRequest.run();
  }, [isModelSelecting, kind]);
  (0, import_react.useEffect)(() => {
    if (!modelCode) return;
    modelRequest.run(modelCode);
  }, [modelCode]);
  const modelDetail = ((_a = modelRequest.data) == null ? void 0 : _a.code) === modelCode ? modelRequest.data : void 0;
  const modelName = translation(modelDetail == null ? void 0 : modelDetail.name) || modelCode || "-";
  const modelDescription = translation(modelDetail == null ? void 0 : modelDetail.description);
  const deviceTypeName = translation((_b = modelDetail == null ? void 0 : modelDetail.type) == null ? void 0 : _b.name) || (modelDetail == null ? void 0 : modelDetail.type_code) || typeCode || "-";
  const deviceTypeGroups = (0, import_react.useMemo)(
    () => (0, import_model.createDeviceTypeGroups)(deviceTypeRequest.data || [], iotEndpoint),
    [deviceTypeRequest.data, iotEndpoint]
  );
  const handleSelectDeviceModel = (type, model) => {
    const nextTypeCode = model.type_code || type.code;
    const nextModelCode = model.code;
    if (!nextTypeCode || !nextModelCode) return;
    if (!showBack) {
      (0, import_model.persistDeviceModel)(nextTypeCode, nextModelCode);
    }
    setTypeCode(nextTypeCode);
    setModelCode(nextModelCode);
  };
  const clearDeviceModel = () => {
    setTypeCode("");
    setModelCode("");
  };
  return {
    typeCode,
    modelCode,
    isModelSelecting,
    modelDetail,
    modelName,
    modelDescription,
    deviceTypeName,
    deviceTypeGroups,
    deviceTypeRequest,
    modelRequest,
    handleSelectDeviceModel,
    clearDeviceModel
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useAppDeviceModels
});
