/**
 * Copyright IBM Corp. 2024, 2025
 */

// maintain variables and its values
export interface EnvVar {
  value: any;
  isSecret?: boolean;
}

export class VariableContext {
  private variableStore = new Map<string, EnvVar>();
  private envStore = new Map<string, EnvVar>();

  // This will be the highest priority
  setVariable(key: string, value: any, isSecret: boolean = false): void {
    this.variableStore.set(key, { value, isSecret });
  }

  set(key: string, value: any, isSecret: boolean = false): void {
    this.setVariable(key, value, isSecret);
  }

  // This will be the least priority
  setEnvVariable(key: string, value: any, isSecret: boolean = false): void {
    this.envStore.set(key, { value, isSecret });
  }

  get(key: string): EnvVar | undefined {
    return this.variableStore.get(key) ?? this.envStore.get(key);
  }

  getValue(key: string): any {
    return this.variableStore.get(key)?.value ?? this.envStore.get(key)?.value;
  }

  getAll(): Record<string, any> {
    return {
      ...Object.fromEntries(this.envStore.entries()),
      ...Object.fromEntries(this.variableStore.entries()),
    };
  }

  getEnvStore(): Record<string, any> {
    return Object.fromEntries(this.envStore.entries());
  }

  getVarStore(): Record<string, any> {
    return Object.fromEntries(this.variableStore.entries());
  }

  delete(key: string): void {
    this.variableStore.delete(key);
    this.envStore.delete(key);
  }

  clear(): void {
    this.variableStore.clear();
  }
}
