"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 ServerEnvironment_exports = {};
__export(ServerEnvironment_exports, {
  ServerEnvironment: () => ServerEnvironment
});
module.exports = __toCommonJS(ServerEnvironment_exports);
var import_FabricScopedDataHandler = require("#behavior/cluster/FabricScopedDataHandler.js");
var import_EndpointInitializer = require("#endpoint/properties/EndpointInitializer.js");
var import_ChangeNotificationService = require("#node/integration/ChangeNotificationService.js");
var import_ServerEndpointInitializer = require("#node/server/ServerEndpointInitializer.js");
var import_ClientCacheBuffer = require("#storage/client/ClientCacheBuffer.js");
var import_ServerNodeStore = require("#storage/server/ServerNodeStore.js");
var import_general = require("@matter/general");
var import_protocol = require("@matter/protocol");
var import_BindingManager = require("../../behaviors/binding/BindingManager.js");
var import_IdentityService = require("./IdentityService.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
var ServerEnvironment;
((ServerEnvironment2) => {
  ServerEnvironment2.fabricScopedDataSanitized = (0, import_general.Observable)();
  async function initialize(node) {
    const { env } = node;
    await SharedNodeServices.install(env);
    if (env.get(import_general.StorageService).hasFilesystem) {
      const fs = env.get(import_general.Filesystem);
      const root = new import_general.DatafileRoot(fs.directory(node.id));
      env.set(import_general.DatafileRoot, root);
      if (env.vars.boolean("storage.lock")) {
        env.set(import_general.DatafileRoot.Lock, await root.lock());
      }
    }
    const store = await import_ServerNodeStore.ServerNodeStore.create(env, node.id);
    env.set(import_ServerNodeStore.ServerNodeStore, store);
    env.set(import_EndpointInitializer.EndpointInitializer, new import_ServerEndpointInitializer.ServerEndpointInitializer(env));
    env.set(import_IdentityService.IdentityService, new import_IdentityService.IdentityService(node));
    env.set(import_ChangeNotificationService.ChangeNotificationService, new import_ChangeNotificationService.ChangeNotificationService(node));
    const fabrics = await env.load(import_protocol.FabricManager);
    fabrics.events.deleting.on(async () => {
      const fabricIndices = fabrics.fabrics.map((fabric) => fabric.fabricIndex);
      if (fabricIndices.length > 0) {
        await (0, import_FabricScopedDataHandler.limitNodeDataToAllowedFabrics)(node, fabricIndices);
      }
      ServerEnvironment2.fabricScopedDataSanitized.emit();
    });
    await env.load(import_protocol.SessionManager);
    env.get(import_protocol.PeerSet);
    env.get(import_general.Crypto).reportUsage(node.id);
  }
  ServerEnvironment2.initialize = initialize;
  async function close(node) {
    const { env } = node;
    await env.close(import_protocol.FabricManager);
    await env.close(import_protocol.PeerSet);
    await env.close(import_ChangeNotificationService.ChangeNotificationService);
    await env.close(import_protocol.SessionManager);
    await env.close(import_protocol.OccurrenceManager);
    await env.close(import_BindingManager.BindingManager);
    if (env.has(import_ClientCacheBuffer.ClientCacheBuffer)) {
      await env.get(import_ClientCacheBuffer.ClientCacheBuffer).close();
    }
    await env.close(import_ServerNodeStore.ServerNodeStore);
    await env.close(SharedNodeServices);
    await env.close(import_protocol.FabricAuthority);
    await env.close(import_protocol.CertificateAuthority);
    if (env.has(import_general.DatafileRoot.Lock)) {
      await env.get(import_general.DatafileRoot.Lock).close();
    }
  }
  ServerEnvironment2.close = close;
})(ServerEnvironment || (ServerEnvironment = {}));
class SharedNodeServices {
  #services;
  static async install(env) {
    const services = env.asDependent();
    await services.load(import_protocol.MdnsService);
    env.set(SharedNodeServices, new SharedNodeServices(services));
  }
  constructor(services) {
    this.#services = services;
  }
  async close() {
    await this.#services.close();
  }
}
//# sourceMappingURL=ServerEnvironment.js.map
