/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Abort, BasicMultiplex, ImplementationError, Logger } from "#general";
import { ApiPath } from "./api/ApiPath.js";
const logger = Logger.get("RemoteAdapter");
class RemoteInterface {
  #node;
  #address;
  #abort = new Abort();
  #root;
  #workers = new BasicMultiplex();
  constructor(node, address) {
    if (address.appProtocol !== this.constructor.protocol) {
      throw new ImplementationError(
        `API endpoint type ${this.constructor.name} does not support address ${address}`
      );
    }
    this.#node = node;
    this.#address = address;
    this.#root = new ApiPath(address);
  }
  get root() {
    return this.#root;
  }
  get env() {
    return this.#node.env;
  }
  get node() {
    return this.#node;
  }
  get address() {
    return this.#address;
  }
  get isAborted() {
    return this.#abort.aborted;
  }
  get abort() {
    return this.#abort.signal;
  }
  static async create(node, address) {
    const instance = new this(node, address);
    try {
      await instance.start();
    } catch (e) {
      await instance.close();
      throw e;
    }
    return instance;
  }
  async close() {
    if (this.isAborted) {
      return;
    }
    this.#abort();
    try {
      await this.stop();
    } catch (e) {
      logger.error(`Error terminating API endpoint ${this.address}`);
    }
  }
  assertProtocol(appProtocol) {
    if (this.address.appProtocol !== appProtocol) {
      throw new ImplementationError(
        `Invalid protocol ${this.address} for API endpoin type ${this.constructor.name}`
      );
    }
  }
  addWorker(worker, description) {
    this.#workers.add(worker, description);
  }
  static protocol = "";
  /**
   * Stop servicing requests.  Called on close.  The default implementation just waits for any workers to complete.
   */
  async stop() {
    await this.#workers.close();
  }
}
export {
  RemoteInterface
};
//# sourceMappingURL=RemoteInterface.js.map
