"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 CommandResource_exports = {};
__export(CommandResource_exports, {
  CommandResource: () => CommandResource
});
module.exports = __toCommonJS(CommandResource_exports);
var import_general = require("#general");
var import_ApiResource = require("../ApiResource.js");
var import_Envelope = require("../Envelope.js");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class CommandResource extends import_ApiResource.ApiResource {
  #behavior;
  supervisor;
  isInvocable = true;
  constructor(parent, behavior, schema) {
    super(parent);
    this.#behavior = behavior;
    this.supervisor = this.supervisorFor(schema);
  }
  get schema() {
    return this.supervisor.schema;
  }
  get id() {
    return (0, import_general.decamelize)(this.schema.name);
  }
  get dataModelPath() {
    return this.parent.dataModelPath.at(this.id);
  }
  get valueKind() {
    return "command";
  }
  get value() {
    return void 0;
  }
  async invoke(request) {
    let input = new import_Envelope.Envelope({ supervisor: this.supervisor, ...request });
    if (input.js === void 0 || input.js === null) {
      input = new import_Envelope.Envelope({ supervisor: this.supervisor, js: {} });
    }
    const name = (0, import_general.camelize)(this.id);
    const method = this.#behavior[name];
    if (typeof method !== "function") {
      throw new import_general.NotImplementedError();
    }
    input.validate();
    const result = await method.call(this.#behavior, input.js);
    const responseSchema = this.schema.responseModel;
    if (!responseSchema) {
      return;
    }
    const output = new import_Envelope.Envelope({ supervisor: this.supervisorFor(responseSchema), js: result });
    try {
      output.validate();
    } catch (e) {
      const error = new import_general.ImplementationError("Command output validation failed");
      error.cause = e;
      throw error;
    }
    return output;
  }
}
//# sourceMappingURL=CommandResource.js.map
