"use strict";
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);
var NodePhysicalProperties_exports = {};
__export(NodePhysicalProperties_exports, {
  NodePhysicalProperties: () => NodePhysicalProperties
});
module.exports = __toCommonJS(NodePhysicalProperties_exports);
var import_IcdClient = require("#behavior/system/icd/IcdClient.js");
var import_basic_information = require("#behaviors/basic-information");
var import_descriptor = require("#behaviors/descriptor");
var import_general_diagnostics = require("#behaviors/general-diagnostics");
var import_icd_management = require("#behaviors/icd-management");
var import_network_commissioning = require("#behaviors/network-commissioning");
var import_power_source = require("#behaviors/power-source");
var import_thread_network_diagnostics = require("#behaviors/thread-network-diagnostics");
var import_aggregator = require("#endpoints/aggregator");
var import_general = require("@matter/general");
var import_general_diagnostics2 = require("@matter/types/clusters/general-diagnostics");
var import_icd_management2 = require("@matter/types/clusters/icd-management");
var import_power_source2 = require("@matter/types/clusters/power-source");
var import_thread_network_diagnostics2 = require("@matter/types/clusters/thread-network-diagnostics");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function NodePhysicalProperties(node) {
  const rootEndpointServerList = [...node.maybeStateOf(import_descriptor.DescriptorClient)?.serverList ?? []];
  const supportsLit = import_IcdClient.IcdClient.litSupported(node);
  const operatingMode = node.maybeStateOf(import_icd_management.IcdManagementClient)?.operatingMode;
  const idleModeDuration = node.maybeStateOf(import_icd_management.IcdManagementClient)?.idleModeDuration;
  const properties = {
    supportsThread: false,
    supportsWifi: false,
    supportsEthernet: false,
    rootEndpointServerList,
    isMainsPowered: false,
    isBatteryPowered: false,
    isIntermittentlyConnected: rootEndpointServerList.includes(import_icd_management2.IcdManagement.id),
    isLongIdleTimeOperating: supportsLit && operatingMode === import_icd_management2.IcdManagement.OperatingMode.Lit,
    idleModeDuration: idleModeDuration === void 0 ? void 0 : (0, import_general.Seconds)(idleModeDuration),
    isThreadSleepyEndDevice: false,
    specificationVersion: node.maybeStateOf(import_basic_information.BasicInformationClient)?.specificationVersion,
    deviceTypes: /* @__PURE__ */ new Set()
  };
  inspectEndpoint(node, properties);
  return properties;
}
function inspectEndpoint(endpoint, properties, behindAggregator = false) {
  const deviceTypes = properties.deviceTypes ??= /* @__PURE__ */ new Set();
  for (const { deviceType } of endpoint.maybeStateOf(import_descriptor.DescriptorClient)?.deviceTypeList ?? []) {
    deviceTypes.add(deviceType);
  }
  if (!behindAggregator) {
    const networkFeatures = endpoint.maybeFeaturesOf(import_network_commissioning.NetworkCommissioningClient);
    if (networkFeatures) {
      if (networkFeatures.wiFiNetworkInterface) {
        properties.supportsWifi = true;
      }
      if (networkFeatures.threadNetworkInterface) {
        properties.supportsThread = true;
      }
      if (networkFeatures.ethernetNetworkInterface) {
        properties.supportsEthernet = true;
      }
    }
    const networkInterfaces = endpoint.maybeStateOf(import_general_diagnostics.GeneralDiagnosticsClient)?.networkInterfaces;
    if (networkInterfaces !== void 0) {
      for (const { type, isOperational } of networkInterfaces) {
        if (type === import_general_diagnostics2.GeneralDiagnostics.InterfaceType.WiFi) {
          properties.wifiActive = (properties.wifiActive ?? false) || isOperational;
        } else if (type === import_general_diagnostics2.GeneralDiagnostics.InterfaceType.Ethernet) {
          properties.ethernetActive = (properties.ethernetActive ?? false) || isOperational;
        }
      }
    }
    const powerSourceFeatures = endpoint.maybeFeaturesOf(import_power_source.PowerSourceClient);
    const powerSourceState = powerSourceFeatures ? endpoint.maybeStateOf(import_power_source.PowerSourceClient) : void 0;
    if (powerSourceFeatures && powerSourceState) {
      const { status } = powerSourceState;
      if (powerSourceFeatures.wired) {
        if (status === import_power_source2.PowerSource.PowerSourceStatus.Active) {
          properties.isMainsPowered = true;
        }
      } else if (powerSourceFeatures.battery || // Perform additional checks because we've encountered devices with incorrect features
      !powerSourceFeatures.wired || endpoint.behaviors.elementsOf(import_power_source.PowerSourceClient).attributes.has("batChargeLevel")) {
        if (status === import_power_source2.PowerSource.PowerSourceStatus.Active || // Some devices do not properly specify state as active
        status === import_power_source2.PowerSource.PowerSourceStatus.Unspecified) {
          properties.isBatteryPowered = true;
        }
      }
    }
    const threadNetworkDiagnostics = endpoint.behaviors.typeFor(import_thread_network_diagnostics.ThreadNetworkDiagnosticsClient);
    const tnd = threadNetworkDiagnostics ? endpoint.maybeStateOf(threadNetworkDiagnostics) : void 0;
    if (tnd) {
      if (tnd.routingRole === import_thread_network_diagnostics2.ThreadNetworkDiagnostics.RoutingRole.SleepyEndDevice) {
        properties.isThreadSleepyEndDevice = true;
      }
      if (tnd.extendedPanId !== void 0 && tnd.extendedPanId !== null) {
        properties.threadActive = true;
        properties.threadPan = BigInt(tnd.extendedPanId);
        properties.threadChannel = tnd.channel ?? void 0;
      } else {
        properties.threadActive = false;
      }
    }
  }
  for (const part of endpoint.parts) {
    const partBehindAggregator = behindAggregator || part.number !== 0 && part.type.deviceType === import_aggregator.AggregatorEndpoint.deviceType;
    inspectEndpoint(part, properties, partBehindAggregator);
  }
}
//# sourceMappingURL=NodePhysicalProperties.js.map
