/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { limitNodeDataToAllowedFabrics } from "#behavior/cluster/FabricScopedDataHandler.js";
import { EndpointInitializer } from "#endpoint/properties/EndpointInitializer.js";
import { ChangeNotificationService } from "#node/integration/ChangeNotificationService.js";
import { ServerEndpointInitializer } from "#node/server/ServerEndpointInitializer.js";
import { ServerNodeStore } from "#storage/server/ServerNodeStore.js";
import { Crypto, Observable } from "@matter/general";
import {
  FabricAuthority,
  FabricManager,
  MdnsService,
  OccurrenceManager,
  PeerSet,
  SessionManager
} from "@matter/protocol";
import { IdentityService } from "./IdentityService.js";
var ServerEnvironment;
((ServerEnvironment2) => {
  ServerEnvironment2.fabricScopedDataSanitized = Observable();
  async function initialize(node) {
    const { env } = node;
    await SharedNodeServices.install(env);
    const store = await ServerNodeStore.create(env, node.id);
    env.set(ServerNodeStore, store);
    env.set(EndpointInitializer, new ServerEndpointInitializer(env));
    env.set(IdentityService, new IdentityService(node));
    env.set(ChangeNotificationService, new ChangeNotificationService(node));
    const fabrics = await env.load(FabricManager);
    fabrics.events.deleting.on(async () => {
      const fabricIndices = fabrics.fabrics.map((fabric) => fabric.fabricIndex);
      if (fabricIndices.length > 0) {
        await limitNodeDataToAllowedFabrics(node, fabricIndices);
      }
      ServerEnvironment2.fabricScopedDataSanitized.emit();
    });
    await env.load(SessionManager);
    env.get(PeerSet);
    env.get(Crypto).reportUsage(node.id);
  }
  ServerEnvironment2.initialize = initialize;
  async function close(node) {
    const { env } = node;
    env.close(FabricManager);
    await env.close(PeerSet);
    await env.close(ChangeNotificationService);
    await env.close(SessionManager);
    await env.close(OccurrenceManager);
    await env.close(ServerNodeStore);
    await env.close(SharedNodeServices);
    env.close(FabricAuthority);
  }
  ServerEnvironment2.close = close;
})(ServerEnvironment || (ServerEnvironment = {}));
class SharedNodeServices {
  #services;
  static async install(env) {
    const services = env.asDependent();
    await services.load(MdnsService);
    env.set(SharedNodeServices, new SharedNodeServices(services));
  }
  constructor(services) {
    this.#services = services;
  }
  async close() {
    await this.#services.close();
  }
}
export {
  ServerEnvironment
};
//# sourceMappingURL=ServerEnvironment.js.map
