"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 ServerNode_exports = {};
__export(ServerNode_exports, {
  ServerNode: () => ServerNode
});
module.exports = __toCommonJS(ServerNode_exports);
var import_CommissioningBehavior = require("#behavior/system/commissioning/CommissioningBehavior.js");
var import_NetworkServer = require("#behavior/system/network/NetworkServer.js");
var import_ServerNetworkRuntime = require("#behavior/system/network/ServerNetworkRuntime.js");
var import_ProductDescriptionServer = require("#behavior/system/product-description/ProductDescriptionServer.js");
var import_SessionsBehavior = require("#behavior/system/sessions/SessionsBehavior.js");
var import_EndpointServer = require("#endpoint/EndpointServer.js");
var import_EndpointInitializer = require("#endpoint/properties/EndpointInitializer.js");
var import_general = require("#general");
var import_root = require("../endpoints/root.js");
var import_Node = require("./Node.js");
var import_Nodes = require("./Nodes.js");
var import_IdentityService = require("./server/IdentityService.js");
var import_ServerEndpointInitializer = require("./server/ServerEndpointInitializer.js");
var import_NodeStore = require("./storage/NodeStore.js");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class FactoryResetError extends import_general.MatterError {
  constructor(message, cause) {
    super(message);
    this.cause = (0, import_general.errorOf)(cause);
  }
}
class ServerNode extends import_Node.Node {
  #nodes;
  constructor(definition, options) {
    super(import_Node.Node.nodeConfigFor(ServerNode.RootEndpoint, definition, options));
    import_general.DiagnosticSource.add(this);
  }
  static async create(definition, options) {
    return await (0, import_general.asyncNew)(this, definition, options);
  }
  createRuntime() {
    return new import_ServerNetworkRuntime.ServerNetworkRuntime(this);
  }
  async [import_general.Construction.destruct]() {
    await super[import_general.Construction.destruct]();
    if (this.env.has(import_NodeStore.NodeStore)) {
      const store = this.env.get(import_NodeStore.NodeStore);
      await store.close();
      this.env.delete(import_NodeStore.NodeStore, store);
    }
  }
  async reset() {
    await super.reset();
    await import_EndpointServer.EndpointServer.forEndpoint(this)[Symbol.asyncDispose]();
  }
  /**
   * Perform a factory reset of the node.
   */
  async factoryReset() {
    try {
      await this.construction;
      const isOnline = this.lifecycle.isOnline;
      if (isOnline) {
        await this.cancel();
      }
      this.statusUpdate("resetting to factory defaults");
      await this.reset();
      await this.resetStorage();
      this.construction.start();
      if (isOnline) {
        await this.start();
      } else {
        await this.construction.ready;
      }
    } catch (e) {
      this.construction.crash();
      throw new FactoryResetError(`Error during factory reset of ${this}`, e);
    }
  }
  /**
   * Access other nodes on the fabric.
   */
  get nodes() {
    if (!this.#nodes) {
      this.#nodes = new import_Nodes.Nodes(this);
    }
    return this.#nodes;
  }
  async advertiseNow() {
    await this.act(`advertiseNow<${this}>`, (agent) => agent.get(import_NetworkServer.NetworkServer).advertiseNow());
  }
  async initialize() {
    const nodeStore = await import_NodeStore.NodeStore.create(this.env, this.id);
    this.env.set(import_NodeStore.NodeStore, nodeStore);
    this.env.set(import_EndpointInitializer.EndpointInitializer, new import_ServerEndpointInitializer.ServerEndpointInitializer(this.env));
    this.env.set(import_IdentityService.IdentityService, new import_IdentityService.IdentityService(this));
    return super.initialize();
  }
  /**
   * By default on factory reset we erase all stored data.
   *
   * If this is inappropriate for your application you may override to alter the behavior.   Matter requires that all
   * "security- and privacy-related data and key material" is removed on factory reset.
   *
   * @see {@link MatterSpecification.v12.Core} § 13.4
   */
  async resetStorage() {
    await this.env.get(import_NodeStore.NodeStore).erase();
  }
  /**
   * Normal endpoints must have an owner to complete construction but server nodes have no such precondition for
   * construction.
   */
  assertConstructable() {
  }
}
((ServerNode2) => {
  ServerNode2.RootEndpoint = import_root.RootEndpoint.with(
    import_CommissioningBehavior.CommissioningBehavior,
    import_NetworkServer.NetworkServer,
    import_ProductDescriptionServer.ProductDescriptionServer,
    import_SessionsBehavior.SessionsBehavior
  );
})(ServerNode || (ServerNode = {}));
//# sourceMappingURL=ServerNode.js.map
