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

let eccmVersion: EccmConfig.Config;

export default (): EccmConfig.Config => {
  if (eccmVersion) {
    return eccmVersion;
  }

  const userDefinedConfigPath =
    process.env['ELTEX_REDMINE_HELPER_ECCM_VERSIONS_CONFIG_PATH'];
  const defaultConfigPath = join('configs', 'eccm-config.jsonc');

  const configPath = userDefinedConfigPath || defaultConfigPath;

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

  eccmVersion = parse(rawData);

  return eccmVersion;
};
