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

export type SimpleKanbanBoardConfig = {
  path: string;
};

let config: SimpleKanbanBoardConfig;

export default (): SimpleKanbanBoardConfig => {
  if (config) {
    return config;
  }

  const userDefinedConfigPath =
    process.env['ELTEX_REDMINE_HELPER_KANBAN_SIMPLE_BOARD_CONFIG_PATH'];
  const defaultConfigPath = join('configs', 'simple-kanban-board-config.jsonc');

  const configPath = userDefinedConfigPath || defaultConfigPath;

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

  config = parse(rawData);

  return config;
};
