import { join } from 'path';
import { readFileSync } from 'fs';
import { parse } from 'jsonc-parser';

let currentUserRules: Record<string, string>;

export default (): Record<string, string> => {
  if (currentUserRules) {
    return currentUserRules;
  }

  const userDefinedConfigPath =
    process.env['ELTEX_REDMINE_HELPER_CURRENT_USER_RULES_CONFIG_PATH'];
  const defaultConfigPath = join('configs', 'current-user-rules.jsonc');

  const configPath = userDefinedConfigPath || defaultConfigPath;

  const rawData = readFileSync(configPath, { encoding: 'utf-8' });

  currentUserRules = parse(rawData);

  return currentUserRules;
};
