/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Diagnostic, Logger, Observable } from "#general";
import { Specification } from "#model";
import { FabricManager } from "#protocol";
import { DEFAULT_MAX_PATHS_PER_INVOKE, VendorId } from "#types";
import { BasicInformationBehavior } from "./BasicInformationBehavior.js";
const logger = Logger.get("BasicInformationServer");
const Base = BasicInformationBehavior.enable({
  events: { startUp: true, shutDown: true, leave: true }
}).set({ maxPathsPerInvoke: 0 });
class BasicInformationServer extends Base {
  initialize() {
    const state = this.state;
    const defaultsSet = {};
    function setDefault(name, value) {
      if (state[name] === void 0 || state[name] === 0) {
        state[name] = value;
        defaultsSet[name] = value;
      }
    }
    setDefault("vendorId", VendorId(65521));
    setDefault("vendorName", "Matter.js Test Vendor");
    setDefault("productId", 32768);
    setDefault("productName", "Matter.js Test Product");
    setDefault("hardwareVersion", 0);
    setDefault("softwareVersion", 0);
    if (Object.keys(defaultsSet).length) {
      logger.warn("Using development values for some BasicInformation attributes:", Diagnostic.dict(defaultsSet));
    }
    setDefault("productLabel", state.productName);
    setDefault("nodeLabel", state.productName);
    setDefault("dataModelRevision", Specification.DATA_MODEL_REVISION);
    setDefault("hardwareVersionString", state.hardwareVersion.toString());
    setDefault("softwareVersionString", state.softwareVersion.toString());
    setDefault("specificationVersion", Specification.SPECIFICATION_VERSION);
    setDefault("maxPathsPerInvoke", DEFAULT_MAX_PATHS_PER_INVOKE);
    const lifecycle = this.endpoint.lifecycle;
    if (lifecycle.online !== void 0) {
      this.reactTo(lifecycle.online, this.#online);
    }
    if (this.state.reachable !== void 0 && this.events.reachable$Changed !== void 0) {
      if (this.events.reachableChanged === void 0) {
        this.events.reachableChanged = Observable();
      }
      this.reactTo(this.events.reachable$Changed, this.#emitReachableChange);
    }
    if (this.state.uniqueId !== void 0 && this.state.serialNumber !== void 0 && this.state.uniqueId === this.state.serialNumber) {
      logger.warn("uniqueId and serialNumber shall not be the same.");
    }
  }
  [Symbol.asyncDispose]() {
    this.events.shutDown?.emit(void 0, this.context);
  }
  #online() {
    this.events.startUp.emit({ softwareVersion: this.state.softwareVersion }, this.context);
    const fabricManager = this.env.get(FabricManager);
    this.reactTo(fabricManager.events.deleted, this.#handleRemovedFabric);
  }
  #emitReachableChange(reachable) {
    this.events.reachableChanged?.emit({ reachableNewValue: reachable }, this.context);
  }
  #handleRemovedFabric({ fabricIndex }) {
    this.events.leave.emit({ fabricIndex }, this.context);
  }
}
export {
  BasicInformationServer
};
//# sourceMappingURL=BasicInformationServer.js.map
