/**
 * @license
 * Copyright 2025-2026 Open Home Foundation
 * SPDX-License-Identifier: Apache-2.0
 */
import {
  mdiAccessPoint,
  mdiAirConditioner,
  mdiAirFilter,
  mdiAirPurifier,
  mdiBell,
  mdiBlindsHorizontal,
  mdiBrightnessPercent,
  mdiCamera,
  mdiCast,
  mdiCctv,
  mdiChip,
  mdiCircleMedium,
  mdiCrown,
  mdiDishwasher,
  mdiDoorbell,
  mdiDoorbellVideo,
  mdiDoorOpen,
  mdiEvStation,
  mdiFan,
  mdiFridge,
  mdiGauge,
  mdiHeatPump,
  mdiHelp,
  mdiHome,
  mdiLeaf,
  mdiLightbulb,
  mdiLock,
  mdiMeterElectric,
  mdiMicrowave,
  mdiMotionSensor,
  mdiPowerPlug,
  mdiPump,
  mdiRemote,
  mdiRobotVacuum,
  mdiRouter,
  mdiRouterWireless,
  mdiSleep,
  mdiSmokeDetector,
  mdiSnowflakeAlert,
  mdiSolarPower,
  mdiSpeaker,
  mdiSprinkler,
  mdiStar,
  mdiStove,
  mdiSwapHorizontal,
  mdiTelevision,
  mdiThermometer,
  mdiToggleSwitch,
  mdiTumbleDryer,
  mdiWashingMachine,
  mdiWater,
  mdiWaterBoiler,
  mdiWaterPercent,
  mdiWeatherRainy,
  mdiWifi
} from "@mdi/js";
import { getCssVar } from "./shared-styles.js";
function getDefaultIconColor() {
  return getCssVar("--node-color-default", "#666666");
}
const DeviceTypes = {
  // Utility (per Matter spec - deprioritized for icon selection)
  ROOT_NODE: 22,
  POWER_SOURCE: 17,
  OTA_REQUESTOR: 18,
  OTA_PROVIDER: 20,
  BRIDGED_NODE: 19,
  ELECTRICAL_SENSOR: 1296,
  DEVICE_ENERGY_MANAGEMENT: 1293,
  SECONDARY_NETWORK_INTERFACE: 25,
  JOINT_FABRIC_ADMINISTRATOR: 304,
  // Lighting
  ON_OFF_LIGHT: 256,
  DIMMABLE_LIGHT: 257,
  COLOR_TEMPERATURE_LIGHT: 268,
  EXTENDED_COLOR_LIGHT: 269,
  // Smart Plugs/Outlets and Other Actuators
  ON_OFF_PLUG: 266,
  DIMMABLE_PLUG: 267,
  MOUNTED_ON_OFF_CONTROL: 271,
  MOUNTED_DIMMABLE_LOAD_CONTROL: 272,
  PUMP: 771,
  WATER_VALVE: 66,
  IRRIGATION_SYSTEM: 64,
  // Switches and Controls
  ON_OFF_SWITCH: 259,
  DIMMER_SWITCH: 260,
  COLOR_DIMMER_SWITCH: 261,
  CONTROL_BRIDGE: 2112,
  PUMP_CONTROLLER: 772,
  GENERIC_SWITCH: 15,
  // Sensors
  CONTACT_SENSOR: 21,
  LIGHT_SENSOR: 262,
  OCCUPANCY_SENSOR: 263,
  TEMPERATURE_SENSOR: 770,
  PRESSURE_SENSOR: 773,
  FLOW_SENSOR: 774,
  HUMIDITY_SENSOR: 775,
  ON_OFF_SENSOR: 2128,
  SMOKE_CO_ALARM: 118,
  AIR_QUALITY_SENSOR: 44,
  WATER_FREEZE_DETECTOR: 65,
  WATER_LEAK_DETECTOR: 67,
  RAIN_SENSOR: 68,
  SOIL_SENSOR: 69,
  // Closures
  DOOR_LOCK: 10,
  DOOR_LOCK_CONTROLLER: 11,
  WINDOW_COVERING: 514,
  WINDOW_COVERING_CONTROLLER: 515,
  CLOSURE: 560,
  CLOSURE_PANEL: 561,
  CLOSURE_CONTROLLER: 574,
  // HVAC
  THERMOSTAT: 769,
  FAN: 43,
  AIR_PURIFIER: 45,
  THERMOSTAT_CONTROLLER: 778,
  HEAT_PUMP: 777,
  ROOM_AIR_CONDITIONER: 114,
  // Media
  SPEAKER: 34,
  CASTING_VIDEO_PLAYER: 35,
  CONTENT_APP: 36,
  BASIC_VIDEO_PLAYER: 40,
  CASTING_VIDEO_CLIENT: 41,
  VIDEO_REMOTE_CONTROL: 42,
  // Generic
  AGGREGATOR: 14,
  // Appliances
  REFRIGERATOR: 112,
  TEMPERATURE_CONTROLLED_CABINET: 113,
  LAUNDRY_WASHER: 115,
  ROBOTIC_VACUUM_CLEANER: 116,
  DISHWASHER: 117,
  COOK_SURFACE: 119,
  COOKTOP: 120,
  MICROWAVE_OVEN: 121,
  EXTRACTOR_HOOD: 122,
  OVEN: 123,
  LAUNDRY_DRYER: 124,
  // Energy
  EVSE: 1292,
  WATER_HEATER: 1295,
  SOLAR_POWER: 23,
  BATTERY_STORAGE: 24,
  // Network Infrastructure
  NETWORK_INFRASTRUCTURE_MANAGER: 144,
  THREAD_BORDER_ROUTER: 145,
  // Cameras
  CAMERA: 322,
  SNAPSHOT_CAMERA: 325,
  VIDEO_DOORBELL: 323,
  AUDIO_DOORBELL: 321,
  FLOODLIGHT_CAMERA: 324,
  DOORBELL: 328,
  CHIME: 326,
  CAMERA_CONTROLLER: 327,
  INTERCOM: 320
};
const deviceTypeToIcon = {
  // Utility
  [DeviceTypes.ROOT_NODE]: mdiHome,
  [DeviceTypes.ELECTRICAL_SENSOR]: mdiMeterElectric,
  [DeviceTypes.DEVICE_ENERGY_MANAGEMENT]: mdiMeterElectric,
  // Lighting
  [DeviceTypes.ON_OFF_LIGHT]: mdiLightbulb,
  [DeviceTypes.DIMMABLE_LIGHT]: mdiLightbulb,
  [DeviceTypes.COLOR_TEMPERATURE_LIGHT]: mdiLightbulb,
  [DeviceTypes.EXTENDED_COLOR_LIGHT]: mdiLightbulb,
  // Smart Plugs/Outlets and Other Actuators
  [DeviceTypes.ON_OFF_PLUG]: mdiPowerPlug,
  [DeviceTypes.DIMMABLE_PLUG]: mdiPowerPlug,
  [DeviceTypes.MOUNTED_ON_OFF_CONTROL]: mdiPowerPlug,
  [DeviceTypes.MOUNTED_DIMMABLE_LOAD_CONTROL]: mdiPowerPlug,
  [DeviceTypes.PUMP]: mdiPump,
  [DeviceTypes.WATER_VALVE]: mdiWater,
  [DeviceTypes.IRRIGATION_SYSTEM]: mdiSprinkler,
  // Switches and Controls
  [DeviceTypes.ON_OFF_SWITCH]: mdiToggleSwitch,
  [DeviceTypes.DIMMER_SWITCH]: mdiToggleSwitch,
  [DeviceTypes.COLOR_DIMMER_SWITCH]: mdiToggleSwitch,
  [DeviceTypes.CONTROL_BRIDGE]: mdiRouter,
  [DeviceTypes.PUMP_CONTROLLER]: mdiPump,
  [DeviceTypes.GENERIC_SWITCH]: mdiToggleSwitch,
  // Sensors
  [DeviceTypes.CONTACT_SENSOR]: mdiDoorOpen,
  [DeviceTypes.LIGHT_SENSOR]: mdiBrightnessPercent,
  [DeviceTypes.OCCUPANCY_SENSOR]: mdiMotionSensor,
  [DeviceTypes.TEMPERATURE_SENSOR]: mdiThermometer,
  [DeviceTypes.PRESSURE_SENSOR]: mdiGauge,
  [DeviceTypes.FLOW_SENSOR]: mdiWater,
  [DeviceTypes.HUMIDITY_SENSOR]: mdiWaterPercent,
  [DeviceTypes.ON_OFF_SENSOR]: mdiMotionSensor,
  [DeviceTypes.SMOKE_CO_ALARM]: mdiSmokeDetector,
  [DeviceTypes.AIR_QUALITY_SENSOR]: mdiAirFilter,
  [DeviceTypes.WATER_FREEZE_DETECTOR]: mdiSnowflakeAlert,
  [DeviceTypes.WATER_LEAK_DETECTOR]: mdiWater,
  [DeviceTypes.RAIN_SENSOR]: mdiWeatherRainy,
  [DeviceTypes.SOIL_SENSOR]: mdiLeaf,
  // Closures
  [DeviceTypes.DOOR_LOCK]: mdiLock,
  [DeviceTypes.DOOR_LOCK_CONTROLLER]: mdiLock,
  [DeviceTypes.WINDOW_COVERING]: mdiBlindsHorizontal,
  [DeviceTypes.WINDOW_COVERING_CONTROLLER]: mdiBlindsHorizontal,
  [DeviceTypes.CLOSURE]: mdiDoorOpen,
  [DeviceTypes.CLOSURE_PANEL]: mdiDoorOpen,
  [DeviceTypes.CLOSURE_CONTROLLER]: mdiDoorOpen,
  // HVAC
  [DeviceTypes.THERMOSTAT]: mdiThermometer,
  [DeviceTypes.FAN]: mdiFan,
  [DeviceTypes.AIR_PURIFIER]: mdiAirPurifier,
  [DeviceTypes.THERMOSTAT_CONTROLLER]: mdiThermometer,
  [DeviceTypes.HEAT_PUMP]: mdiHeatPump,
  [DeviceTypes.ROOM_AIR_CONDITIONER]: mdiAirConditioner,
  // Media
  [DeviceTypes.SPEAKER]: mdiSpeaker,
  [DeviceTypes.CASTING_VIDEO_PLAYER]: mdiTelevision,
  [DeviceTypes.CONTENT_APP]: mdiTelevision,
  [DeviceTypes.BASIC_VIDEO_PLAYER]: mdiTelevision,
  [DeviceTypes.CASTING_VIDEO_CLIENT]: mdiCast,
  [DeviceTypes.VIDEO_REMOTE_CONTROL]: mdiRemote,
  // Generic
  [DeviceTypes.AGGREGATOR]: mdiRouter,
  // Appliances
  [DeviceTypes.REFRIGERATOR]: mdiFridge,
  [DeviceTypes.TEMPERATURE_CONTROLLED_CABINET]: mdiFridge,
  [DeviceTypes.LAUNDRY_WASHER]: mdiWashingMachine,
  [DeviceTypes.ROBOTIC_VACUUM_CLEANER]: mdiRobotVacuum,
  [DeviceTypes.DISHWASHER]: mdiDishwasher,
  [DeviceTypes.COOK_SURFACE]: mdiStove,
  [DeviceTypes.COOKTOP]: mdiStove,
  [DeviceTypes.MICROWAVE_OVEN]: mdiMicrowave,
  [DeviceTypes.EXTRACTOR_HOOD]: mdiFan,
  [DeviceTypes.OVEN]: mdiStove,
  [DeviceTypes.LAUNDRY_DRYER]: mdiTumbleDryer,
  // Energy
  [DeviceTypes.EVSE]: mdiEvStation,
  [DeviceTypes.WATER_HEATER]: mdiWaterBoiler,
  [DeviceTypes.SOLAR_POWER]: mdiSolarPower,
  [DeviceTypes.BATTERY_STORAGE]: mdiMeterElectric,
  // Network Infrastructure
  [DeviceTypes.NETWORK_INFRASTRUCTURE_MANAGER]: mdiRouter,
  [DeviceTypes.THREAD_BORDER_ROUTER]: mdiAccessPoint,
  // Cameras
  [DeviceTypes.CAMERA]: mdiCamera,
  [DeviceTypes.SNAPSHOT_CAMERA]: mdiCamera,
  [DeviceTypes.VIDEO_DOORBELL]: mdiDoorbellVideo,
  [DeviceTypes.AUDIO_DOORBELL]: mdiDoorbell,
  [DeviceTypes.FLOODLIGHT_CAMERA]: mdiCctv,
  [DeviceTypes.DOORBELL]: mdiDoorbell,
  [DeviceTypes.CHIME]: mdiBell,
  [DeviceTypes.CAMERA_CONTROLLER]: mdiCamera,
  [DeviceTypes.INTERCOM]: mdiDoorbell
};
const threadRoleToIcon = {
  5: mdiRouter,
  // Router
  6: mdiAccessPoint
  // Leader
};
const THREAD_ROLE_BADGES = {
  2: { iconPath: mdiSleep, colorVar: "--node-color-thread-enddevice", colorFallback: "#90a4ae" },
  // Sleepy End Device
  3: { iconPath: mdiCircleMedium, colorVar: "--node-color-thread-enddevice", colorFallback: "#90a4ae" },
  // End Device
  4: { iconPath: mdiCircleMedium, colorVar: "--node-color-thread-enddevice", colorFallback: "#90a4ae" },
  // REED
  5: { iconPath: mdiSwapHorizontal, colorVar: "--node-color-thread-router", colorFallback: "#1e88e5" },
  // Router
  6: { iconPath: mdiCrown, colorVar: "--node-color-thread-leader", colorFallback: "#f9a825" }
  // Leader
};
const UTILITY_TYPES = /* @__PURE__ */ new Set([
  DeviceTypes.ROOT_NODE,
  DeviceTypes.POWER_SOURCE,
  DeviceTypes.OTA_REQUESTOR,
  DeviceTypes.OTA_PROVIDER,
  DeviceTypes.BRIDGED_NODE,
  DeviceTypes.ELECTRICAL_SENSOR,
  DeviceTypes.DEVICE_ENERGY_MANAGEMENT,
  DeviceTypes.SECONDARY_NETWORK_INTERFACE,
  DeviceTypes.JOINT_FABRIC_ADMINISTRATOR
]);
function extractDeviceType(entry) {
  return entry?.["0"] ?? entry?.deviceType;
}
function selectBestDeviceType(deviceTypeList) {
  if (!deviceTypeList?.length) return void 0;
  let firstFound;
  for (const entry of deviceTypeList) {
    const deviceType = extractDeviceType(entry);
    if (deviceType === void 0) continue;
    firstFound ??= deviceType;
    if (deviceTypeToIcon[deviceType] && !UTILITY_TYPES.has(deviceType)) {
      return deviceType;
    }
  }
  return firstFound;
}
function getPrimaryDeviceType(node) {
  const endpoints = [];
  for (const key of Object.keys(node.attributes)) {
    const match = key.match(/^(\d+)\/29\/0$/);
    if (match) {
      endpoints.push(Number(match[1]));
    }
  }
  endpoints.sort((a, b) => a === 0 ? 1 : b === 0 ? -1 : a - b);
  let firstFound;
  for (const ep of endpoints) {
    const deviceTypeList = node.attributes[`${ep}/29/0`];
    const deviceType = selectBestDeviceType(deviceTypeList);
    if (deviceType === void 0) continue;
    firstFound ??= deviceType;
    if (deviceTypeToIcon[deviceType] && !UTILITY_TYPES.has(deviceType)) {
      return deviceType;
    }
  }
  return firstFound;
}
function getEndpointIcon(node, endpoint) {
  const deviceTypeList = node.attributes[`${endpoint}/29/0`];
  const deviceType = selectBestDeviceType(deviceTypeList);
  if (deviceType !== void 0 && deviceTypeToIcon[deviceType]) {
    return deviceTypeToIcon[deviceType];
  }
  return mdiChip;
}
function getDeviceIcon(node, threadRole) {
  if (threadRole !== void 0 && threadRoleToIcon[threadRole]) {
    const deviceType2 = getPrimaryDeviceType(node);
    if (deviceType2 === DeviceTypes.ROOT_NODE || deviceType2 === DeviceTypes.AGGREGATOR || node.is_bridge) {
      return threadRoleToIcon[threadRole];
    }
  }
  if (node.is_bridge) {
    return mdiRouter;
  }
  const deviceType = getPrimaryDeviceType(node);
  if (deviceType !== void 0 && deviceTypeToIcon[deviceType]) {
    return deviceTypeToIcon[deviceType];
  }
  return mdiChip;
}
function getNetworkTypeIcon(networkType) {
  switch (networkType) {
    case "thread":
      return mdiAccessPoint;
    case "wifi":
      return mdiWifi;
    case "ethernet":
      return mdiRouter;
    default:
      return mdiChip;
  }
}
function createIconDataUrl(iconPath, color, size = 48, badge) {
  const badgeMarkup = badge !== void 0 ? `<circle cx="18" cy="6" r="4" fill="${badge.color}"/>
            <path d="${badge.iconPath}" fill="white" transform="translate(14.64,2.64) scale(0.28)"/>` : "";
  const svg = `
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="${size}" height="${size}">
            <circle cx="12" cy="12" r="11" fill="white" stroke="${color}" stroke-width="1"/>
            <path d="${iconPath}" fill="${color}" transform="scale(0.6) translate(8,8)"/>
            ${badgeMarkup}
        </svg>
    `.trim();
  return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
}
function createNodeIconDataUrl(node, threadRole, isSelected = false, isOffline = false) {
  const iconPath = getDeviceIcon(node, threadRole);
  let color;
  if (isSelected) {
    color = isOffline ? getCssVar("--node-color-selected-offline", "#b71c1c") : getCssVar("--node-color-selected", "#1976d2");
  } else if (isOffline) {
    color = getCssVar("--node-color-offline", "#d32f2f");
  } else {
    color = getDefaultIconColor();
  }
  const roleBadge = threadRole !== void 0 ? THREAD_ROLE_BADGES[threadRole] : void 0;
  const badge = roleBadge !== void 0 ? { iconPath: roleBadge.iconPath, color: getCssVar(roleBadge.colorVar, roleBadge.colorFallback) } : void 0;
  return createIconDataUrl(iconPath, color, 48, badge);
}
function createUnknownDeviceIconDataUrl(isRouter = false, isSelected = false) {
  const iconPath = isRouter ? mdiAccessPoint : mdiHelp;
  const color = isSelected ? getCssVar("--node-color-selected", "#1976d2") : getCssVar("--node-color-unknown", "#ff9800");
  return createIconDataUrl(iconPath, color);
}
function createBorderRouterIconDataUrl(isSelected = false, isLeader = false, isPrimaryBbr = false) {
  const glyph = isLeader ? mdiCrown : mdiRouterWireless;
  const color = isSelected ? getCssVar("--node-color-selected", "#1976d2") : isLeader ? getCssVar("--node-color-thread-leader", "#f9a825") : getCssVar("--md-sys-color-primary", "#03a9f4");
  const badge = isPrimaryBbr ? { iconPath: mdiStar, color: getCssVar("--node-color-primary-bbr", "#00897b") } : void 0;
  return createIconDataUrl(glyph, color, 48, badge);
}
function createWiFiRouterIconDataUrl(isSelected = false) {
  const color = isSelected ? getCssVar("--node-color-selected", "#1976d2") : getCssVar("--node-color-unknown", "#ff9800");
  return createIconDataUrl(mdiWifi, color);
}
export {
  createBorderRouterIconDataUrl,
  createIconDataUrl,
  createNodeIconDataUrl,
  createUnknownDeviceIconDataUrl,
  createWiFiRouterIconDataUrl,
  getDeviceIcon,
  getEndpointIcon,
  getNetworkTypeIcon,
  getPrimaryDeviceType
};
//# sourceMappingURL=device-icons.js.map
