"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 RemoteInterface_exports = {};
__export(RemoteInterface_exports, {
  RemoteInterface: () => RemoteInterface
});
module.exports = __toCommonJS(RemoteInterface_exports);
var import_general = require("#general");
var import_ApiPath = require("./api/ApiPath.js");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("RemoteAdapter");
class RemoteInterface {
  #node;
  #address;
  #abort = new import_general.Abort();
  #root;
  #workers = new import_general.BasicMultiplex();
  constructor(node, address) {
    if (address.appProtocol !== this.constructor.protocol) {
      throw new import_general.ImplementationError(
        `API endpoint type ${this.constructor.name} does not support address ${address}`
      );
    }
    this.#node = node;
    this.#address = address;
    this.#root = new import_ApiPath.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 import_general.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();
  }
}
//# sourceMappingURL=RemoteInterface.js.map
