"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 BridgedDeviceBasicInformationServer_exports = {};
__export(BridgedDeviceBasicInformationServer_exports, {
  BridgedDeviceBasicInformationServer: () => BridgedDeviceBasicInformationServer
});
module.exports = __toCommonJS(BridgedDeviceBasicInformationServer_exports);
var import_basic_information = require("#behaviors/basic-information");
var import_descriptor = require("#behaviors/descriptor");
var import_aggregator = require("#endpoints/aggregator");
var import_general = require("@matter/general");
var import_BridgedDeviceBasicInformationBehavior = require("./BridgedDeviceBasicInformationBehavior.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("BridgedDeviceBasicInformationServer");
class BridgedDeviceBasicInformationServer extends import_BridgedDeviceBasicInformationBehavior.BridgedDeviceBasicInformationBehavior {
  async initialize() {
    if (this.endpoint.lifecycle.isInstalled) {
      await this.agent.load(import_descriptor.DescriptorServer);
      await this.#configurePart();
    } else {
      this.reactTo(this.endpoint.lifecycle.installed, this.#configurePart, { once: true });
    }
    this.reactTo(this.events.reachable$Changed, this.#emitReachableChange);
    const { uniqueId, serialNumber } = this.state;
    if (uniqueId === void 0) {
      this.state.uniqueId = import_basic_information.BasicInformationServer.createUniqueId();
    }
    if (serialNumber !== void 0 && uniqueId === this.state.serialNumber) {
      logger.warn("uniqueId and serialNumber shall not be the same.");
    }
  }
  static schema = import_basic_information.BasicInformationServer.enableUniqueIdPersistence(
    import_BridgedDeviceBasicInformationBehavior.BridgedDeviceBasicInformationBehavior.schema
  );
  /**
   * Per the specification.  Not sure what this adds vs. subscribing to attribute changes.
   */
  #emitReachableChange(reachable) {
    this.events.reachableChanged.emit({ reachableNewValue: reachable }, this.context);
  }
  /**
   * Per the specification, BridgedDeviceBasicInformation may only appear on bridged nodes, and bridged nodes may only
   * appear under aggregator nodes.
   *
   * Therefore, this default implementation of BridgedDeviceBasicInformation injects the BridgedNode device type on the
   * associated {@link Endpoint} and asserts that its parent is a {@link AggregatorEndpoint}.
   */
  async #configurePart() {
    const owner = this.agent.owner;
    if (owner === void 0) {
      throw new import_general.ImplementationError(`Bridged node ${this.endpoint} has no parent`);
    }
    if (!owner.get(import_descriptor.DescriptorServer).hasDeviceType(import_aggregator.AggregatorEndpoint.deviceType)) {
      throw new import_general.ImplementationError(`Bridged node ${this.endpoint} owner ${owner} is not an aggregator`);
    }
    (await this.agent.load(import_descriptor.DescriptorServer)).addDeviceTypes("BridgedNode");
  }
}
((BridgedDeviceBasicInformationServer2) => {
  class State extends import_BridgedDeviceBasicInformationBehavior.BridgedDeviceBasicInformationBehavior.State {
    // Assume Device is online when it is added, but developers should set correctly if needed
    reachable = true;
  }
  BridgedDeviceBasicInformationServer2.State = State;
})(BridgedDeviceBasicInformationServer || (BridgedDeviceBasicInformationServer = {}));
//# sourceMappingURL=BridgedDeviceBasicInformationServer.js.map
