"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 NetworkRuntime_exports = {};
__export(NetworkRuntime_exports, {
  NetworkRuntime: () => NetworkRuntime
});
module.exports = __toCommonJS(NetworkRuntime_exports);
var import_general = require("#general");
var import_NodeActivity = require("../../context/NodeActivity.js");
var import_NetworkBehavior = require("./NetworkBehavior.js");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class NetworkRuntime {
  #construction;
  #owner;
  get construction() {
    return this.#construction;
  }
  constructor(owner) {
    this.#owner = owner;
    const internals = owner.behaviors.internalsOf(import_NetworkBehavior.NetworkBehavior);
    if (internals.runtime) {
      throw new import_general.ImplementationError("Network is already active");
    }
    internals.runtime = this;
    this.#construction = (0, import_general.Construction)(this);
  }
  async [import_general.Construction.construct]() {
    await this.start();
    await this.#owner.act((agent) => this.owner.lifecycle.online.emit(agent.context));
  }
  async [import_general.Construction.destruct]() {
    this.blockNewActivity();
    const activity = this.#owner.env.get(import_NodeActivity.NodeActivity);
    if (activity.isActive) {
      await activity.inactive;
    }
    try {
      await this.stop();
    } finally {
      this.#owner.behaviors.internalsOf(import_NetworkBehavior.NetworkBehavior).runtime = void 0;
    }
    await this.#owner.act((agent) => this.owner.lifecycle.offline.emit(agent.context));
  }
  async close() {
    await this.construction.close();
  }
  get owner() {
    return this.#owner;
  }
}
//# sourceMappingURL=NetworkRuntime.js.map
