"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 ClientNetworkRuntime_exports = {};
__export(ClientNetworkRuntime_exports, {
  ClientNetworkRuntime: () => ClientNetworkRuntime,
  OfflineError: () => OfflineError,
  UncommissionedError: () => UncommissionedError
});
module.exports = __toCommonJS(ClientNetworkRuntime_exports);
var import_general = require("@matter/general");
var import_protocol = require("@matter/protocol");
var import_CommissioningClient = require("../commissioning/CommissioningClient.js");
var import_NetworkRuntime = require("./NetworkRuntime.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class UncommissionedError extends import_general.MatterError {
}
class OfflineError extends import_general.MatterError {
}
class ClientNetworkRuntime extends import_NetworkRuntime.NetworkRuntime {
  #observers = new import_general.ObserverGroup();
  #isReady = false;
  constructor(owner) {
    super(owner);
  }
  set isReady(isReady) {
    this.#isReady = isReady;
    this.#syncOnlineStatus();
  }
  get owner() {
    return super.owner;
  }
  async start() {
    if (!this.owner.lifecycle.isCommissioned) {
      throw new UncommissionedError(`Cannot interact with ${this.owner} because it is uncommissioned`);
    }
    if (this.owner.state.network.isDisabled) {
      throw new UncommissionedError(`Cannot interact with ${this.owner} because it is disabled`);
    }
    const address = (0, import_protocol.PeerAddress)(this.owner.stateOf(import_CommissioningClient.CommissioningClient).peerAddress);
    if (address === void 0) {
      throw new import_general.InternalError(`Commissioned node ${this.owner} has no peer address`);
    }
    const server = this.owner.owner;
    if (!server.lifecycle.isOnline) {
      await server.start();
    }
    const { env } = this.owner;
    const peers = env.get(import_protocol.PeerSet);
    const peer = peers.get(address);
    if (peer === void 0) {
      throw new import_general.InternalError(`Commissioned node ${this.owner} has no peer ${address.toString()} installed`);
    }
    env.set(import_protocol.ExchangeProvider, peer.exchangeProvider);
    const syncOnlineStatus = this.#syncOnlineStatus.bind(this);
    this.#observers.on(peer.sessions.added, syncOnlineStatus);
    this.#observers.on(peer.sessions.deleted, syncOnlineStatus);
  }
  async stop() {
    this.isReady = false;
    await this.construction;
    this.#observers.close();
  }
  #syncOnlineStatus() {
    let shouldBeOnline;
    if (!this.#isReady) {
      shouldBeOnline = false;
    } else {
      const peer = this.owner.env.maybeGet(import_protocol.Peer);
      if (peer === void 0) {
        shouldBeOnline = false;
      } else {
        shouldBeOnline = peer.hasSession;
      }
    }
    if (this.owner.lifecycle.isOnline === shouldBeOnline) {
      return;
    }
    this.owner.act(({ context }) => {
      this.owner.lifecycle[shouldBeOnline ? "online" : "offline"].emit(context);
    });
  }
}
//# sourceMappingURL=ClientNetworkRuntime.js.map
