/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { CommissioningBehavior } from "#behavior/system/commissioning/CommissioningBehavior.js";
import { NetworkServer } from "#behavior/system/network/NetworkServer.js";
import { ServerNetworkRuntime } from "#behavior/system/network/ServerNetworkRuntime.js";
import { ProductDescriptionServer } from "#behavior/system/product-description/ProductDescriptionServer.js";
import { SessionsBehavior } from "#behavior/system/sessions/SessionsBehavior.js";
import { EndpointServer } from "#endpoint/EndpointServer.js";
import { EndpointInitializer } from "#endpoint/properties/EndpointInitializer.js";
import { Construction, DiagnosticSource, MatterError, asyncNew, errorOf } from "#general";
import { RootEndpoint as BaseRootEndpoint } from "../endpoints/root.js";
import { Node } from "./Node.js";
import { Nodes } from "./Nodes.js";
import { IdentityService } from "./server/IdentityService.js";
import { ServerEndpointInitializer } from "./server/ServerEndpointInitializer.js";
import { NodeStore } from "./storage/NodeStore.js";
class FactoryResetError extends MatterError {
  constructor(message, cause) {
    super(message);
    this.cause = errorOf(cause);
  }
}
class ServerNode extends Node {
  #nodes;
  constructor(definition, options) {
    super(Node.nodeConfigFor(ServerNode.RootEndpoint, definition, options));
    DiagnosticSource.add(this);
  }
  static async create(definition, options) {
    return await asyncNew(this, definition, options);
  }
  createRuntime() {
    return new ServerNetworkRuntime(this);
  }
  async [Construction.destruct]() {
    await super[Construction.destruct]();
    if (this.env.has(NodeStore)) {
      const store = this.env.get(NodeStore);
      await store.close();
      this.env.delete(NodeStore, store);
    }
  }
  async reset() {
    await super.reset();
    await 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 Nodes(this);
    }
    return this.#nodes;
  }
  async advertiseNow() {
    await this.act(`advertiseNow<${this}>`, (agent) => agent.get(NetworkServer).advertiseNow());
  }
  async initialize() {
    const nodeStore = await NodeStore.create(this.env, this.id);
    this.env.set(NodeStore, nodeStore);
    this.env.set(EndpointInitializer, new ServerEndpointInitializer(this.env));
    this.env.set(IdentityService, new 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(NodeStore).erase();
  }
  /**
   * Normal endpoints must have an owner to complete construction but server nodes have no such precondition for
   * construction.
   */
  assertConstructable() {
  }
}
((ServerNode2) => {
  ServerNode2.RootEndpoint = BaseRootEndpoint.with(
    CommissioningBehavior,
    NetworkServer,
    ProductDescriptionServer,
    SessionsBehavior
  );
})(ServerNode || (ServerNode = {}));
export {
  ServerNode
};
//# sourceMappingURL=ServerNode.js.map
