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/modules/devices/hooks/useDeviceModels.ts
var useDeviceModels_exports = {};
__export(useDeviceModels_exports, {
  useDeviceModels: () => useDeviceModels
});
module.exports = __toCommonJS(useDeviceModels_exports);
var import_react = require("react");
var import_ahooks = require("ahooks");
var import_api = require("../../../api");
var import_model = require("../../../model");
var EMPTY_BRANDS = [];
var useDeviceModels = ({
  activeKind,
  selectedWorkAreaId,
  devices,
  translation
}) => {
  const [modelCacheByKind, setModelCacheByKind] = (0, import_react.useState)({});
  const modelCacheByKindRef = (0, import_react.useRef)({});
  const requestedModelKindsRef = (0, import_react.useRef)(/* @__PURE__ */ new Set());
  const deviceBrandRequest = (0, import_ahooks.useRequest)(import_api.getDeviceBrands, { manual: true });
  (0, import_react.useEffect)(() => {
    if (!selectedWorkAreaId) return;
    if (modelCacheByKindRef.current[activeKind]) return;
    if (requestedModelKindsRef.current.has(activeKind)) return;
    requestedModelKindsRef.current.add(activeKind);
    (0, import_api.getDeviceTypes)({
      kind: activeKind,
      with: ["activeModels", "activeModels.activeProfiles", "activeModels.brand"]
    }).then((types) => {
      const models = (0, import_model.flattenDeviceModels)(types);
      modelCacheByKindRef.current = {
        ...modelCacheByKindRef.current,
        [activeKind]: models
      };
      setModelCacheByKind(modelCacheByKindRef.current);
    }).catch(() => {
      requestedModelKindsRef.current.delete(activeKind);
      modelCacheByKindRef.current = {
        ...modelCacheByKindRef.current,
        [activeKind]: []
      };
      setModelCacheByKind(modelCacheByKindRef.current);
    });
  }, [activeKind, selectedWorkAreaId]);
  const activeModels = modelCacheByKind[activeKind] || [];
  const typeOptions = (0, import_react.useMemo)(
    () => activeModels.map((model) => ({
      label: translation(model.name) || model.code,
      value: model.code
    })),
    [activeModels, translation]
  );
  const profileNameMap = (0, import_react.useMemo)(() => {
    const map = /* @__PURE__ */ new Map();
    Object.values(modelCacheByKind).forEach((models) => {
      (models || []).forEach((model) => {
        (model.active_profiles || []).forEach((profile) => {
          map.set(profile.id, translation(profile.name) || String(profile.id));
        });
      });
    });
    devices.forEach((device) => {
      var _a;
      if ((_a = device.profile) == null ? void 0 : _a.id) {
        map.set(
          device.profile.id,
          translation(device.profile.name) || String(device.profile.id)
        );
      }
    });
    return map;
  }, [devices, modelCacheByKind, translation]);
  const loadBrandsIfNeeded = () => {
    if (activeKind === "dumb" && !deviceBrandRequest.data && !deviceBrandRequest.loading) {
      deviceBrandRequest.run();
    }
  };
  return {
    modelCacheByKind,
    activeModels,
    typeOptions,
    profileNameMap,
    brands: deviceBrandRequest.data || EMPTY_BRANDS,
    loadBrandsIfNeeded
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useDeviceModels
});
