"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 ProductDescriptionServer_exports = {};
__export(ProductDescriptionServer_exports, {
  ProductDescriptionServer: () => ProductDescriptionServer
});
module.exports = __toCommonJS(ProductDescriptionServer_exports);
var import_EndpointType = require("#endpoint/type/EndpointType.js");
var import_model = require("@matter/model");
var import_types = require("@matter/types");
var import_BasicInformationBehavior = require("../../../behaviors/basic-information/BasicInformationBehavior.js");
var import_DescriptorBehavior = require("../../../behaviors/descriptor/DescriptorBehavior.js");
var import_Behavior = require("../../Behavior.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class ProductDescriptionServer extends import_Behavior.Behavior {
  static id = "productDescription";
  initialize() {
    const pd = this.state;
    const bi = this.agent.get(import_BasicInformationBehavior.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 !== import_EndpointType.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 = (0, import_types.VendorId)(-1, false);
  ProductDescriptionServer2.UNKNOWN_PRODUCT_ID = -1;
  class State {
    /**
     * The device name for commissioning announcements.
     */
    name = "";
    /**
     * The device type for commissioning announcements.
     */
    deviceType = import_EndpointType.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(import_DescriptorBehavior.DescriptorBehavior)) {
    return;
  }
  let recurse = false;
  for (const dt of agent.get(import_DescriptorBehavior.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 import_model.RootNodeDt.id:
      case import_model.BridgedNodeDt.id:
      case import_model.AggregatorDt.id:
        recurse = true;
        break;
      default:
        if (agent.endpoint.type.deviceClass === import_model.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;
    }
  }
}
//# sourceMappingURL=ProductDescriptionServer.js.map
