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

let config: Record<string, any>;

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

  const userDefinedConfigPath =
    process.env['ELTEX_REDMINE_HELPER_CALENDAR_ENHANCER_CONFIG_PATH'];
  const defaultConfigPath = join('configs', 'calendar-enhancer.jsonc');

  const configPath = userDefinedConfigPath || defaultConfigPath;

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

  config = parse(rawData);

  return config;
};
