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/deviceDetail/hooks/useDeviceDetail.ts
var useDeviceDetail_exports = {};
__export(useDeviceDetail_exports, {
  useDeviceDetail: () => useDeviceDetail
});
module.exports = __toCommonJS(useDeviceDetail_exports);
var import_react = require("react");
var import_api = require("../../../api");
var import_model = require("../../../model");
var useDeviceDetail = ({
  open,
  record,
  initialTab,
  translation
}) => {
  const [activeTab, setActiveTab] = (0, import_react.useState)("info");
  const [detailRecord, setDetailRecord] = (0, import_react.useState)();
  const detailFetchedIdRef = (0, import_react.useRef)();
  const displayRecord = detailRecord || record;
  const activeKind = (displayRecord == null ? void 0 : displayRecord._raw.type_kind) || "smart";
  const isComposite = (displayRecord == null ? void 0 : displayRecord._raw.structure) === "composite";
  (0, import_react.useEffect)(() => {
    if (open) {
      setActiveTab(initialTab);
      setDetailRecord(record);
      detailFetchedIdRef.current = void 0;
    }
  }, [initialTab, open, record == null ? void 0 : record.id]);
  (0, import_react.useEffect)(() => {
    if (!open || !(record == null ? void 0 : record.id)) return;
    const shouldFetchDetail = activeTab === "info" || detailFetchedIdRef.current !== record.id;
    if (!shouldFetchDetail) return;
    detailFetchedIdRef.current = record.id;
    (0, import_api.getDeviceDetail)(record.id).then((detail) => {
      if (detail) {
        setDetailRecord((0, import_model.createDeviceRow)(detail, translation));
      }
    }).catch(() => {
      setDetailRecord(record);
    });
  }, [activeTab, open, record, record == null ? void 0 : record.id, translation]);
  (0, import_react.useEffect)(() => {
    if (activeKind === "integrated" && activeTab !== "info") {
      setActiveTab("info");
    }
    if (activeTab === "buildIns" && !isComposite) {
      setActiveTab("info");
    }
  }, [activeKind, activeTab, isComposite]);
  return {
    activeTab,
    activeKind,
    displayRecord,
    setActiveTab
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useDeviceDetail
});
