import type { RpcFunctionDefinitionAny } from './types'
import { diagnostics } from './diagnostics'

/**
 * Validates RPC function definitions.
 * Action and event functions cannot have dumps (side effects should not be cached).
 *
 * @throws {Error} If an action or event function has a dump configuration
 */
export function validateDefinitions(definitions: readonly RpcFunctionDefinitionAny[]): void {
  for (const definition of definitions) {
    const type = definition.type || 'query'

    if ((type === 'action' || type === 'event') && definition.dump) {
      throw diagnostics.DF0027({ name: definition.name, type })
    }

    if (definition.snapshot && type !== 'query') {
      throw diagnostics.DF0028({ name: definition.name, type })
    }
  }
}

/**
 * Validates a single RPC function definition.
 *
 * @throws {Error} If an action or event function has a dump configuration
 */
export function validateDefinition(definition: RpcFunctionDefinitionAny): void {
  validateDefinitions([definition])
}
