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); }
export var DEVICE_ICON_COLOR = {
  smart: '#1677ff',
  dumb: '#faad14',
  integrated: '#389e0d'
};
export var CONNECTION_TYPE_OPTIONS = [{
  label: 'USB',
  value: 'usb'
}, {
  label: 'Ethernet',
  value: 'ethernet'
}, {
  label: 'WiFi',
  value: 'wifi'
}, {
  label: 'Bluetooth',
  value: 'bluetooth'
}, {
  label: 'Serial',
  value: 'serial'
}];
export var BRAND_OPTION_SEPARATOR = '__';

/** 把品牌 id 和 code 编码成 Select value，避免 code 中特殊字符与 id 冲突。 */
export var getBrandOptionValue = function getBrandOptionValue(brand) {
  return "".concat(brand.id).concat(BRAND_OPTION_SEPARATOR).concat(brand.code);
};

/** 从品牌 Select value 中还原 brand_code。 */
export var getBrandCodeFromOptionValue = function getBrandCodeFromOptionValue(value) {
  return value === null || value === void 0 ? void 0 : value.split(BRAND_OPTION_SEPARATOR).slice(1).join(BRAND_OPTION_SEPARATOR);
};

/** 读取型号支持的连接方式，用于外设型号筛选和连接配置展示。 */
export var getModelConnectionTypes = function getModelConnectionTypes(model) {
  var _model$data;
  var connections = (_model$data = model.data) === null || _model$data === void 0 ? void 0 : _model$data.connection_types;
  return Array.isArray(connections) ? connections.map(function (item) {
    return String(item || '').trim();
  }).filter(Boolean) : [];
};

/** 把连接方式 code 转成 UI 展示文案。 */
export var getConnectionTypeLabel = function getConnectionTypeLabel(value) {
  var _CONNECTION_TYPE_OPTI;
  if (!value) return '-';
  var normalizedValue = value.toLowerCase().trim();
  return ((_CONNECTION_TYPE_OPTI = CONNECTION_TYPE_OPTIONS.find(function (option) {
    return option.value === normalizedValue;
  })) === null || _CONNECTION_TYPE_OPTI === void 0 ? void 0 : _CONNECTION_TYPE_OPTI.label) || value;
};

/** 判断当前型号是否必须选择 profile。 */
export var isProfileRequired = function isProfileRequired(model) {
  var _model$data2;
  return Boolean(model === null || model === void 0 || (_model$data2 = model.data) === null || _model$data2 === void 0 || (_model$data2 = _model$data2.profile_structure) === null || _model$data2 === void 0 ? void 0 : _model$data2.required);
};

/** 判断多语言表单值是否至少有一个非空语言值。 */
export var hasI18nValue = function hasI18nValue(value) {
  if (!value) return false;
  return Object.values(value).some(function (item) {
    return String(item || '').trim();
  });
};

/** 将后端按类型返回的 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: link === 'linked' ? item.status || 'offline' : 'unknown',
    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
  };
};