/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { EndpointType } from "#endpoint/type/EndpointType.js";
import { AggregatorDt, BridgedNodeDt, DeviceClassification, RootNodeDt } from "#model";
import { VendorId } from "#types";
import { BasicInformationBehavior } from "../../../behaviors/basic-information/BasicInformationBehavior.js";
import { DescriptorBehavior } from "../../../behaviors/descriptor/DescriptorBehavior.js";
import { Behavior } from "../../Behavior.js";
class ProductDescriptionServer extends Behavior {
  static id = "productDescription";
  initialize() {
    const pd = this.state;
    const bi = this.agent.get(BasicInformationBehavior).state;
    if (pd.name === "") {
      pd.name = bi.productName;
    }
    if (pd.vendorId === -1) {
      pd.vendorId = bi.vendorId;
    }
    if (pd.productId === -1) {
      pd.productId = bi.productId;
    }
    this.#setDeviceType();
  }
  #setDeviceType() {
    if (this.state.deviceType !== EndpointType.UNKNOWN_DEVICE_TYPE) {
      return;
    }
    const deviceType = inferDeviceType(this.agent);
    if (deviceType !== void 0) {
      this.state.deviceType = deviceType;
      return;
    }
    this.reactTo(
      this.endpoint.lifecycle.changed,
      this.#setDeviceType,
      { once: true }
    );
  }
}
((ProductDescriptionServer2) => {
  ProductDescriptionServer2.UNKNOWN_VENDOR_ID = VendorId(-1, false);
  ProductDescriptionServer2.UNKNOWN_PRODUCT_ID = -1;
  class State {
    /**
     * The device name for commissioning announcements.
     */
    name = "";
    /**
     * The device type for commissioning announcements.
     */
    deviceType = EndpointType.UNKNOWN_DEVICE_TYPE;
    /**
     * The vendor ID for commissioning announcements.
     */
    vendorId = ProductDescriptionServer2.UNKNOWN_VENDOR_ID;
    /**
     * The product ID for commissioning announcements.
     */
    productId = ProductDescriptionServer2.UNKNOWN_PRODUCT_ID;
  }
  ProductDescriptionServer2.State = State;
})(ProductDescriptionServer || (ProductDescriptionServer = {}));
function inferDeviceType(agent) {
  if (!agent.endpoint.behaviors.isActive(DescriptorBehavior)) {
    return;
  }
  let recurse = false;
  for (const dt of agent.get(DescriptorBehavior).state.deviceTypeList) {
    switch (dt.deviceType) {
      // Note - retrieve IDs from the model rather than the endpoint files because referencing the endpoints will
      // create a big wad of circular deps
      case RootNodeDt.id:
      case BridgedNodeDt.id:
      case AggregatorDt.id:
        recurse = true;
        break;
      default:
        if (agent.endpoint.type.deviceClass === DeviceClassification.Simple) {
          return dt.deviceType;
        }
    }
  }
  if (!recurse || !agent.endpoint.hasParts) {
    return;
  }
  for (const child of agent.endpoint.parts) {
    const deviceType = inferDeviceType(child.agentFor(agent.context));
    if (deviceType !== void 0) {
      return deviceType;
    }
  }
}
export {
  ProductDescriptionServer
};
//# sourceMappingURL=ProductDescriptionServer.js.map
