/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Abort, Construction, ImplementationError } from "#general";
import { NodeActivity } from "../../context/NodeActivity.js";
import { NetworkBehavior } from "./NetworkBehavior.js";
class NetworkRuntime {
  #construction;
  #owner;
  #abort = new Abort();
  get abortSignal() {
    return this.#abort.signal;
  }
  get construction() {
    return this.#construction;
  }
  constructor(owner) {
    this.#owner = owner;
    const internals = owner.behaviors.internalsOf(NetworkBehavior);
    if (internals.runtime) {
      throw new ImplementationError("Network is already active");
    }
    internals.runtime = this;
    this.#construction = Construction(this);
  }
  async [Construction.construct]() {
    await this.start();
  }
  async [Construction.destruct]() {
    this.#abort();
    const activity = this.#owner.env.get(NodeActivity);
    await activity.inactive;
    try {
      await this.stop();
    } finally {
      this.#owner.behaviors.internalsOf(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;
  }
}
export {
  NetworkRuntime
};
//# sourceMappingURL=NetworkRuntime.js.map
