import { BaseNode } from './BaseNode.js';

/**
 * AST node for function/evidence parameters
 * Represents: user: User, role: string
 */
export class ParameterNode extends BaseNode {
  constructor(name, type, location = null) {
    super('Parameter', location);
    this.name = name;
    this.type = type;
    this.isOptional = false;
    this.defaultValue = null;
    this.isArray = false;
  }

  /**
   * Set whether this parameter is optional
   * @param {boolean} optional - Whether parameter is optional
   */
  setOptional(optional) {
    this.isOptional = optional;
  }

  /**
   * Set default value for this parameter
   * @param {*} value - Default value
   */
  setDefaultValue(value) {
    this.defaultValue = value;
  }

  /**
   * Set whether this parameter is an array
   * @param {boolean} isArray - Whether parameter is an array
   */
  setArray(isArray) {
    this.isArray = isArray;
  }

  /**
   * Get the full type string including array notation
   * @returns {string} Full type string
   */
  getFullType() {
    let typeStr = this.type;
    if (this.isArray) {
      typeStr += '[]';
    }
    if (this.isOptional) {
      typeStr += '?';
    }
    return typeStr;
  }

  /**
   * Validate the parameter
   * @returns {string[]} Array of error messages
   */
  validate() {
    const errors = [];

    // Validate parameter name
    if (!this.name || typeof this.name !== 'string') {
      errors.push(`Invalid parameter name: ${this.name}`);
    }

    // Validate type
    if (!this.type || typeof this.type !== 'string') {
      errors.push(`Invalid parameter type: ${this.type}`);
    }

    return errors;
  }

  toString() {
    return `Parameter(${this.name}: ${this.getFullType()})`;
  }
}
