/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Node } from "#node/Node.js";
import { Invoke } from "#protocol";
import { Status, StatusResponseError } from "#types";
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(Node);
  const chunks = node.interaction.invoke(
    Invoke({
      commands: [
        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 !== Status.Success) {
            throw StatusResponseError.create(entry.status, void 0, entry.clusterStatus);
          }
          return;
        case "cmd-response":
          return entry.data;
      }
    }
  }
}
export {
  ClientCommandMethod
};
//# sourceMappingURL=ClientCommandMethod.js.map
