/**
 * Trigger filter types for conditional workflow execution.
 */

import { TriggerFieldType } from './trigger';

/**
 * Filter operator - dynamically loaded from capabilities.
 * Base operators in free plugin, extended by Pro.
 */
export type FilterOperator = string;

/**
 * Logic type for filter groups.
 */
export type FilterLogic = 'and' | 'or';

/**
 * A single filter condition comparing a field against a value.
 */
export interface FilterCondition {
  /** The trigger field name to filter on */
  field: string;
  /** The comparison operator */
  operator: FilterOperator;
  /** The value to compare against (optional for is_empty/is_not_empty) */
  value?: string | number | boolean;
}

/**
 * A group of filter conditions with AND/OR logic.
 */
export interface FilterGroup {
  /** Logic to combine conditions */
  logic: FilterLogic;
  /** Array of conditions or nested groups */
  conditions: (FilterCondition | FilterGroup)[];
}

/**
 * Type guard to check if a filter node is a group.
 */
export function isFilterGroup(node: FilterCondition | FilterGroup): node is FilterGroup {
  return 'logic' in node && 'conditions' in node;
}

/**
 * Mapping of field types to their valid operators.
 */
export type OperatorsByType = Record<TriggerFieldType, FilterOperator[]>;
