/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { OnlineEvent } from "#behavior/Events.js";
import { BasicInformation } from "#clusters/basic-information";
import { Diagnostic, ImplementationError, Logger } from "#general";
import { AttributeModel, EventModel, 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);
    if (this.state.uniqueId === void 0) {
      this.state.uniqueId = BasicInformationServer.createUniqueId();
    }
    const lifecycle = this.endpoint.lifecycle;
    this.reactTo(lifecycle.online, this.#online);
    this.reactTo(lifecycle.goingOffline, this.#goingOffline);
    if (this.state.reachable !== void 0 && this.events.reachable$Changed !== void 0) {
      const reachableChangedSchema = BasicInformationBehavior.schema.get(
        EventModel,
        BasicInformation.Cluster.events.reachableChanged.id
      );
      if (reachableChangedSchema === void 0) {
        throw new ImplementationError("Reachable Changed event schema is missing");
      }
      if (this.events.reachableChanged === void 0) {
        this.events.reachableChanged = new OnlineEvent(reachableChangedSchema, this.events);
      }
      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.");
    }
  }
  static schema = this.enableUniqueIdPersistence(Base.schema);
  static enableUniqueIdPersistence(schema) {
    return schema.extend({}, schema.require(AttributeModel, "uniqueId").extend({ quality: "FN" }));
  }
  static createUniqueId() {
    const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    const charLength = chars.length;
    let id = "";
    for (let i = 0; i < 32; i++) {
      id += chars.charAt(Math.floor(Math.random() * charLength));
    }
    return id;
  }
  #online() {
    this.events.startUp.emit({ softwareVersion: this.state.softwareVersion }, this.context);
    const fabricManager = this.env.get(FabricManager);
    this.reactTo(fabricManager.events.deleted, this.#handleRemovedFabric);
  }
  #goingOffline() {
    this.events.shutDown?.emit(void 0, this.context);
  }
  #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
