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

let statusChanges: StatusChangesConfig.Config;

export default (): StatusChangesConfig.Config => {
  if (statusChanges) {
    return statusChanges;
  }

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

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

  statusChanges = parse(rawData);

  return statusChanges;
};
