import { StatusesConfig } from 'src/models/statuses-config.model';
import { parse } from 'jsonc-parser';
import { join } from 'path';
import { readFileSync } from 'fs';

let redmineStatues: StatusesConfig.Config;

export default (): StatusesConfig.Config => {
  if (redmineStatues) {
    return redmineStatues;
  }

  const userDefinedConfigPath =
    process.env['ELTEX_REDMINE_HELPER_STATUSES_CONFIG_PATH'];
  const defaultConfigPath = join('configs', 'redmine-statuses-config.jsonc');
  const configPath = userDefinedConfigPath || defaultConfigPath;

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

  redmineStatues = parse(rawData);

  return redmineStatues;
};
