export const PERF_DIAG_ENV = 'METOYOU_PERF_DIAG';
export const PERF_DIAG_FORCE_ENV = 'METOYOU_PERF_DIAG_FORCE';

const TRUTHY = new Set([
  '1',
  'true',
  'yes',
  'on'
]);

function isTruthyFlag(value: string | undefined): boolean {
  return TRUTHY.has(String(value ?? '').trim()
    .toLowerCase());
}

export function isPerfDiagEnabled(
  env: NodeJS.ProcessEnv,
  isPackaged: boolean
): boolean {
  if (!isTruthyFlag(env[PERF_DIAG_ENV])) {
    return false;
  }

  if (isPackaged && !isTruthyFlag(env[PERF_DIAG_FORCE_ENV])) {
    return false;
  }

  return true;
}
