"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
var __typeError = (msg) => {
  throw TypeError(msg);
};
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 __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();
};
var RemoteInterface_exports = {};
__export(RemoteInterface_exports, {
  RemoteInterface: () => RemoteInterface
});
module.exports = __toCommonJS(RemoteInterface_exports);
var import_general = require("@matter/general");
var import_ApiPath = require("./api/ApiPath.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("RemoteAdapter");
class RemoteInterface {
  #node;
  #lifetime;
  #address;
  #abort = new import_general.Abort();
  #root;
  #workers;
  #certificate;
  #key;
  constructor({ node, address, certificate, key }) {
    this.assertProtocol(address.appProtocol);
    this.#node = node;
    this.#lifetime = node.env.join((0, import_general.decamelize)(this.constructor.name, " "));
    this.#workers = new import_general.BasicMultiplex();
    this.#address = address;
    this.#root = new import_ApiPath.ApiPath(address);
    this.#certificate = certificate;
    this.#key = key;
  }
  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 certificate() {
    return this.#certificate;
  }
  get key() {
    return this.#key;
  }
  get isAborted() {
    return this.#abort.aborted;
  }
  get abort() {
    return this.#abort.signal;
  }
  static async create(config) {
    const instance = new this(config);
    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}`, e);
      }
    } catch (_) {
      var _error = _, _hasError = true;
    } finally {
      __callDispose(_stack, _error, _hasError);
    }
  }
  assertProtocol(appProtocol) {
    const baseProtocol = this.constructor.protocol;
    if (appProtocol !== baseProtocol && appProtocol !== `${baseProtocol}s`) {
      throw new import_general.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();
  }
}
//# sourceMappingURL=RemoteInterface.js.map
