function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/** 将后端按类型返回的 active_models 展平成去重后的型号列表。 */
export var flattenDeviceModels = function flattenDeviceModels(types) {
  var modelMap = new Map();
  types.forEach(function (type) {
    (type.active_models || []).forEach(function (model) {
      if (!modelMap.has(model.code)) {
        modelMap.set(model.code, _objectSpread(_objectSpread({}, model), {}, {
          type: model.type || type
        }));
      }
    });
  });
  return Array.from(modelMap.values());
};

/** 把后端设备实体转换成列表和详情抽屉共用的展示模型。 */
export var createDeviceRow = function createDeviceRow(item, translation, profileNameMap) {
  var _item$type, _item$model, _item$profile;
  var name = translation(item.name);
  var typeName = translation((_item$type = item.type) === null || _item$type === void 0 ? void 0 : _item$type.name) || item.type_code;
  var modelName = translation((_item$model = item.model) === null || _item$model === void 0 ? void 0 : _item$model.name) || item.model_code;
  var link = item.link || 'unlinked';
  var profileName = translation((_item$profile = item.profile) === null || _item$profile === void 0 ? void 0 : _item$profile.name) || (item.profile_id ? profileNameMap === null || profileNameMap === void 0 ? void 0 : profileNameMap.get(item.profile_id) : '') || (item.profile_id ? String(item.profile_id) : '-');
  return {
    id: item.id,
    name: name,
    typeName: typeName,
    modelName: modelName,
    profileName: profileName,
    link: link,
    status: item.type_kind === 'dumb' ? item.status || 'unknown' : link === 'linked' ? item.status || 'offline' : 'unknown',
    structure: item.structure,
    typeCode: item.type_code,
    modelCode: item.model_code,
    _raw: item
  };
};

/** 兼容工作区详情接口可能返回 data 数组或单对象的两种结构。 */
export var resolveWorkAreaDetail = function resolveWorkAreaDetail(response) {
  var data = response === null || response === void 0 ? void 0 : response.data;
  if (Array.isArray(data)) return data[0];
  return data || response;
};

/** 从工作区详情响应中提取三类设备数量，用于删除前阻断判断。 */
export var getDeleteWorkAreaCounts = function getDeleteWorkAreaCounts(response) {
  var workArea = resolveWorkAreaDetail(response);
  return {
    smart: (workArea === null || workArea === void 0 ? void 0 : workArea.smart_devices_count) || 0,
    dumb: (workArea === null || workArea === void 0 ? void 0 : workArea.dumb_devices_count) || 0,
    integrated: (workArea === null || workArea === void 0 ? void 0 : workArea.integrated_devices_count) || 0
  };
};