"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 PaseCommissioner_exports = {};
__export(PaseCommissioner_exports, {
  PaseCommissioner: () => PaseCommissioner
});
module.exports = __toCommonJS(PaseCommissioner_exports);
var import_general = require("@matter/general");
var import_protocol = require("@matter/protocol");
var import_CommissioningController = require("./CommissioningController.js");
var import_MatterController = require("./MatterController.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = new import_general.Logger("PaseCommissioner");
class PaseCommissioner {
  /**
   * Creates a new CommissioningController instance
   *
   * @param options The options for the CommissioningController
   */
  constructor(options) {
    this.options = options;
    if (options.environment === void 0) {
      throw new import_general.ImplementationError("You need to prove an environment for the commissioner.");
    }
    const { environment } = options.environment;
    this.#environment = environment;
  }
  #environment;
  #controllerInstance;
  #services;
  // Keyed by JSON-stringified identifier so cancelCommissionableDeviceDiscovery() can look up active discoveries.
  #activeDiscoveries = /* @__PURE__ */ new Map();
  get nodeId() {
    return this.#controllerInstance?.nodeId;
  }
  assertControllerIsStarted(errorText) {
    if (this.#controllerInstance === void 0) {
      throw new import_general.ImplementationError(
        errorText ?? "Controller instance not yet started. Please call start() first."
      );
    }
    return this.#controllerInstance;
  }
  /** Internal method to initialize a MatterController instance. */
  async initializeController() {
    if (this.#controllerInstance !== void 0) {
      return this.#controllerInstance;
    }
    const { certificateAuthorityConfig: rootCertificateData, fabricConfig } = this.options;
    this.#services = this.#environment.asDependent();
    try {
      await this.#services.load(import_protocol.MdnsService);
    } catch {
      logger.debug("No networking available, using only BLE");
    }
    return await import_MatterController.MatterController.createAsPaseCommissioner({
      id: "PaseCommissioner",
      // In Memory anyway
      environment: this.#environment,
      certificateAuthorityConfig: rootCertificateData,
      fabricConfig,
      adminFabricLabel: this.options.fabricConfig.label
    });
  }
  /**
   * Commissions/Pairs a new device into the controller fabric. The method returns the NodeId of the commissioned node.
   *
   * Use the connectNodeAfterCommissioning callback to implement an own logic to do the operative device discovery and
   * to complete the commissioning process.
   * Return true when the commissioning process is completed successfully, false on error.
   */
  async commissionNode(nodeOptions, completeCommissioningCallback) {
    const controller = this.assertControllerIsStarted();
    return await controller.commission(nodeOptions, { completeCommissioningCallback });
  }
  /** Disconnects all connected nodes and Closes the network connections and other resources of the controller. */
  async close() {
    await this.#controllerInstance?.close();
    this.#controllerInstance = void 0;
    await this.#services?.close();
  }
  /** Initialize the controller. */
  async start() {
    const runtime = this.#environment.runtime;
    runtime.add(this);
    if (this.#controllerInstance === void 0) {
      this.#controllerInstance = await this.initializeController();
    }
  }
  cancelCommissionableDeviceDiscovery(identifierData, discoveryCapabilities) {
    (0, import_CommissioningController.cancelDiscoverCommissionableDevices)(identifierData, discoveryCapabilities, this.#activeDiscoveries);
  }
  async discoverCommissionableDevices(identifierData, discoveryCapabilities, discoveredCallback, timeout = (0, import_general.Minutes)(15)) {
    return (0, import_CommissioningController.runDiscoverCommissionableDevices)(
      this.assertControllerIsStarted().node,
      identifierData,
      discoveryCapabilities,
      discoveredCallback,
      timeout,
      this.#activeDiscoveries
    );
  }
}
//# sourceMappingURL=PaseCommissioner.js.map
