"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 ClientCommandMethod_exports = {};
__export(ClientCommandMethod_exports, {
  ClientCommandMethod: () => ClientCommandMethod
});
module.exports = __toCommonJS(ClientCommandMethod_exports);
var import_Node = require("#node/Node.js");
var import_protocol = require("#protocol");
var import_types = require("#types");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function ClientCommandMethod(name) {
  const temp = {
    // The actual implementation
    [name](fields) {
      return invokeOnPeer(this, name, fields);
    }
  };
  return temp[name];
}
async function invokeOnPeer(behavior, command, fields) {
  const node = behavior.env.get(import_Node.Node);
  const chunks = node.interaction.invoke(
    (0, import_protocol.Invoke)({
      commands: [
        import_protocol.Invoke.ConcreteCommandRequest({
          endpoint: behavior.endpoint,
          cluster: behavior.cluster,
          command,
          fields
        })
      ]
    })
  );
  for await (const chunk of chunks) {
    for (const entry of chunk) {
      switch (entry.kind) {
        case "cmd-status":
          if (entry.status !== import_types.Status.Success) {
            throw import_types.StatusResponseError.create(entry.status, void 0, entry.clusterStatus);
          }
          return;
        case "cmd-response":
          return entry.data;
      }
    }
  }
}
//# sourceMappingURL=ClientCommandMethod.js.map
