var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
var __typeError = (msg) => {
  throw TypeError(msg);
};
var __using = (stack, value, async) => {
  if (value != null) {
    if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
    var dispose, inner;
    if (async) dispose = value[__knownSymbol("asyncDispose")];
    if (dispose === void 0) {
      dispose = value[__knownSymbol("dispose")];
      if (async) inner = dispose;
    }
    if (typeof dispose !== "function") __typeError("Object not disposable");
    if (inner) dispose = function() {
      try {
        inner.call(this);
      } catch (e) {
        return Promise.reject(e);
      }
    };
    stack.push([async, dispose, value]);
  } else if (async) {
    stack.push([async]);
  }
  return value;
};
var __callDispose = (stack, error, hasError) => {
  var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
    return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
  };
  var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
  var next = (it) => {
    while (it = stack.pop()) {
      try {
        var result = it[1] && it[1].call(it[2]);
        if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
      } catch (e) {
        fail(e);
      }
    }
    if (hasError) throw error;
  };
  return next();
};
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import {
  Abort,
  BasicMultiplex,
  decamelize,
  ImplementationError,
  Logger
} from "#general";
import { ApiPath } from "./api/ApiPath.js";
const logger = Logger.get("RemoteAdapter");
class RemoteInterface {
  #node;
  #lifetime;
  #address;
  #abort = new Abort();
  #root;
  #workers;
  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.#lifetime = node.env.join(decamelize(this.constructor.name, " "));
    this.#workers = new BasicMultiplex();
    this.#address = address;
    this.#root = new ApiPath(address);
  }
  join(...name) {
    return this.#lifetime.join(...name);
  }
  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() {
    var _stack = [];
    try {
      if (this.isAborted) {
        return;
      }
      const _closing = __using(_stack, this.#lifetime.closing());
      this.#abort();
      try {
        await this.stop();
      } catch (e) {
        logger.error(`Error terminating API endpoint ${this.address}`);
      }
    } catch (_) {
      var _error = _, _hasError = true;
    } finally {
      __callDispose(_stack, _error, _hasError);
    }
  }
  assertProtocol(appProtocol) {
    if (this.address.appProtocol !== appProtocol) {
      throw new ImplementationError(
        `Invalid protocol ${this.address} for API endpoin type ${this.constructor.name}`
      );
    }
  }
  addWorker(worker) {
    this.#workers.add(worker);
  }
  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
