/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { CommandElement } from "../elements/index.js";
import { ModelTraversal } from "../logic/ModelTraversal.js";
import { ValueModel } from "./ValueModel.js";
class CommandModel extends ValueModel {
  tag = CommandElement.Tag;
  direction;
  response;
  operationalResponse;
  get fabricScoped() {
    return !!this.effectiveAccess.fabric;
  }
  get isRequest() {
    return this.effectiveDirection !== CommandElement.Direction.Response;
  }
  get isResponse() {
    return this.effectiveDirection === CommandElement.Direction.Response;
  }
  set isResponse(isResponse) {
    this.direction = isResponse ? CommandElement.Direction.Response : CommandElement.Direction.Request;
  }
  get responseModel() {
    switch (this.operationalResponse) {
      case void 0:
        return new ModelTraversal().findResponse(this);
      case null:
        return void 0;
      default:
        return this.operationalResponse;
    }
  }
  get effectiveDirection() {
    return this.direction ?? this.base?.effectiveDirection;
  }
  get effectiveResponse() {
    return this.response ?? this.base?.effectiveResponse;
  }
  get requiredFields() {
    return { ...super.requiredFields, id: this.id };
  }
  /**
   * Commands may re-use the ID for request and response so identification requires the ID in conjunction with the
   * direction.
   */
  get discriminator() {
    if (this.direction === void 0) {
      if (this.name.endsWith("Response")) {
        return CommandElement.Direction.Response;
      }
      return CommandElement.Direction.Request;
    }
    return this.direction;
  }
  constructor(definition, ...children) {
    super(definition, ...children);
    this.direction = definition.direction;
    this.response = definition.response;
    this.operationalResponse = definition.operationalResponse;
  }
  toElement(omitResources = false, extra) {
    return super.toElement(omitResources, {
      direction: this.direction,
      response: this.response,
      ...extra
    });
  }
  finalize() {
    if (this.isFinal) {
      return;
    }
    const operationalResponse = this.operationalResponse ?? (this.operationalResponse = this.responseModel ?? null);
    operationalResponse?.finalize();
    super.finalize();
  }
  static Tag = CommandElement.Tag;
  static requiresId = true;
}
CommandModel.register();
export {
  CommandModel
};
//# sourceMappingURL=CommandModel.js.map
