"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 BasicInformationServer_exports = {};
__export(BasicInformationServer_exports, {
  BasicInformationServer: () => BasicInformationServer
});
module.exports = __toCommonJS(BasicInformationServer_exports);
var import_Events = require("#behavior/Events.js");
var import_general = require("@matter/general");
var import_model = require("@matter/model");
var import_protocol = require("@matter/protocol");
var import_types = require("@matter/types");
var import_basic_information = require("@matter/types/clusters/basic-information");
var import_basic_information_validators = require("./basic-information-validators.js");
var import_BasicInformationBehavior = require("./BasicInformationBehavior.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("BasicInformationServer");
const Base = import_BasicInformationBehavior.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", (0, import_types.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:", import_general.Diagnostic.dict(defaultsSet));
    }
    setDefault("productLabel", state.productName);
    setDefault("nodeLabel", state.productName);
    setDefault("dataModelRevision", import_model.Specification.DATA_MODEL_REVISION);
    setDefault("hardwareVersionString", state.hardwareVersion.toString());
    setDefault("softwareVersionString", state.softwareVersion.toString());
    setDefault("specificationVersion", import_model.Specification.SPECIFICATION_VERSION);
    setDefault("maxPathsPerInvoke", import_types.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 = import_BasicInformationBehavior.BasicInformationBehavior.schema.get(
        import_model.EventModel,
        import_basic_information.BasicInformation.events.reachableChanged.id
      );
      if (reachableChangedSchema === void 0) {
        throw new import_general.ImplementationError("Reachable Changed event schema is missing");
      }
      if (this.events.reachableChanged === void 0) {
        this.events.reachableChanged = new import_Events.OnlineEvent(reachableChangedSchema, this.events);
      }
      this.reactTo(this.events.reachable$Changed, this.#emitReachableChange);
    }
    (0, import_basic_information_validators.validateBasicInfoAttributes)(this.state, logger);
  }
  static schema = this.enableUniqueIdPersistence(Base.schema);
  static enableUniqueIdPersistence(schema) {
    return schema.extend({}, schema.require(import_model.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(import_protocol.FabricManager);
    this.reactTo(fabricManager.events.leaving, this.#handleFabricLeave);
  }
  #goingOffline() {
    this.events.shutDown?.emit(void 0, this.context);
  }
  #emitReachableChange(reachable) {
    this.events.reachableChanged?.emit({ reachableNewValue: reachable }, this.context);
  }
  #handleFabricLeave({ fabricIndex }) {
    this.events.leave.emit({ fabricIndex }, this.context);
  }
}
//# sourceMappingURL=BasicInformationServer.js.map
