function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useCallback, useMemo, useState } from 'react';
import { createDeviceRow } from "../saasDevice/devicePlanning/model";
/**
 * 用拓扑已有的原始设备 + recordId 拼一个最小 {@link DeviceRow}。
 * 抽屉内部 `useDeviceDetail` 会用 `id` 再调 `getDeviceDetail` 拉完整详情覆盖，
 * 所以这里只要保证 `id` 正确、`_raw` 字段够渲染首屏即可。
 */
function buildInitialDeviceRow(recordId, raw, translation) {
  var _ref, _raw$type_code, _raw$model_code, _ref2, _raw$number, _raw$data;
  var _raw = {
    id: Number(recordId),
    type_kind: (_ref = raw === null || raw === void 0 ? void 0 : raw.type_kind) !== null && _ref !== void 0 ? _ref : 'smart',
    type_code: (_raw$type_code = raw === null || raw === void 0 ? void 0 : raw.type_code) !== null && _raw$type_code !== void 0 ? _raw$type_code : 'unknown',
    model_code: (_raw$model_code = raw === null || raw === void 0 ? void 0 : raw.model_code) !== null && _raw$model_code !== void 0 ? _raw$model_code : '',
    // ghost 节点（recordId 有但设备列表无匹配）raw 缺失时，用 #id 兜底，避免详情 404 回退后头部空白
    name: (_ref2 = raw === null || raw === void 0 ? void 0 : raw.name) !== null && _ref2 !== void 0 ? _ref2 : {
      original: "#".concat(recordId)
    },
    number: (_raw$number = raw === null || raw === void 0 ? void 0 : raw.number) !== null && _raw$number !== void 0 ? _raw$number : undefined,
    status: raw === null || raw === void 0 ? void 0 : raw.status,
    link: raw === null || raw === void 0 ? void 0 : raw.link,
    data: (_raw$data = raw === null || raw === void 0 ? void 0 : raw.data) !== null && _raw$data !== void 0 ? _raw$data : null,
    type: raw === null || raw === void 0 ? void 0 : raw.type,
    model: raw === null || raw === void 0 ? void 0 : raw.model
  };
  return createDeviceRow(_raw, translation);
}

/** 拓扑阅读态点击设备图元 → 打开只读详情抽屉的状态管理 */
export function useTopologyDeviceDetail(_ref3) {
  var rawDevices = _ref3.rawDevices,
    translation = _ref3.translation;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    record = _useState4[0],
    setRecord = _useState4[1];
  var rawDevicesById = useMemo(function () {
    return new Map(rawDevices.map(function (d) {
      return [String(d.id), d];
    }));
  }, [rawDevices]);
  var openByItem = useCallback(function (item) {
    var _item$_sceneElement;
    var recordId = (_item$_sceneElement = item._sceneElement) === null || _item$_sceneElement === void 0 || (_item$_sceneElement = _item$_sceneElement.dataBinding) === null || _item$_sceneElement === void 0 ? void 0 : _item$_sceneElement.recordId;
    // 选区框 / 无数据绑定的图元不弹抽屉
    if (recordId == null) return;
    // 拓扑设备 id 均为后端数字 id；非数字（如低代码 mock 的 prop-N）无详情接口，跳过，
    // 同时保证 record.id 必为有限数字，避免 NaN 作为抽屉 key 造成串台
    if (!Number.isFinite(Number(recordId))) return;
    var raw = rawDevicesById.get(String(recordId));
    setRecord(buildInitialDeviceRow(String(recordId), raw, translation));
    setOpen(true);
  }, [rawDevicesById, translation]);
  var close = useCallback(function () {
    return setOpen(false);
  }, []);
  return {
    open: open,
    record: record,
    openByItem: openByItem,
    close: close
  };
}