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/appDevicePlanning/model/adapters.ts
var adapters_exports = {};
__export(adapters_exports, {
  createDeviceRows: () => createDeviceRows,
  createDeviceTypeGroups: () => createDeviceTypeGroups,
  createProfileNameMap: () => createProfileNameMap
});
module.exports = __toCommonJS(adapters_exports);
var import_constants = require("./constants");
var createDeviceTypeGroups = (types = [], iotEndpoint) => types.map((type) => ({
  type,
  models: [...type.active_models || []].filter(
    (model) => {
      var _a;
      return iotEndpoint ? ((_a = model.data) == null ? void 0 : _a.endpoint) === iotEndpoint : true;
    }
  ).sort((a, b) => (a.sort || 0) - (b.sort || 0))
})).filter((group) => group.models.length > 0);
var createProfileNameMap = (modelDetail, devices, translation) => {
  const map = /* @__PURE__ */ new Map();
  ((modelDetail == null ? void 0 : modelDetail.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;
};
var createDeviceRows = ({
  devices,
  search,
  storedIotDevice,
  deviceTypeName,
  modelName,
  profileNameMap,
  translation
}) => {
  const keyword = (0, import_constants.normalizeText)(search);
  const hasCurrentDevice = devices.some(
    (item) => storedIotDevice.number && item.number && storedIotDevice.number === String(item.number)
  );
  return devices.map((item) => {
    var _a, _b, _c;
    const name = translation(item.name);
    const typeName = translation((_a = item.type) == null ? void 0 : _a.name) || deviceTypeName;
    const modelDisplayName = translation((_b = item.model) == null ? void 0 : _b.name) || modelName;
    const linkStatus = item.link || "unlinked";
    const isCurrent = Boolean(
      storedIotDevice.number && item.number && storedIotDevice.number === String(item.number)
    );
    const profileName = translation((_c = item.profile) == null ? void 0 : _c.name) || (item.profile_id ? profileNameMap.get(item.profile_id) : "") || (item.profile_id ? String(item.profile_id) : "-");
    return {
      id: item.id,
      name,
      typeName,
      modelName: modelDisplayName,
      profileName,
      link: linkStatus,
      status: linkStatus === "linked" ? item.status || "offline" : "unknown",
      typeCode: item.type_code,
      modelCode: item.model_code,
      isCurrent,
      // 没有当前设备命中时，才按上次使用 id 标记 Last Used。
      isLastUsed: Boolean(
        !hasCurrentDevice && storedIotDevice.id !== void 0 && String(storedIotDevice.id) === String(item.id)
      ),
      _raw: item
    };
  }).filter(
    (item) => keyword ? [
      item.name,
      item.typeName,
      item.modelName,
      item.profileName,
      item._raw.workarea ? translation(item._raw.workarea.name) : ""
    ].some((value) => (0, import_constants.normalizeText)(value || "").includes(keyword)) : true
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createDeviceRows,
  createDeviceTypeGroups,
  createProfileNameMap
});
