import type { LocalizedString } from './shared-types';
import { dataSets, loadModels } from './loadModels';

export interface MarketGroup {
  readonly market_group_id: number;
  readonly parent_group_id: number;
  readonly name: LocalizedString;
  readonly description: LocalizedString;
  readonly child_market_group_ids: number[];
  readonly icon_id: number;
  readonly has_types: boolean;
}

export function getMarketGroup(market_group_id: number) {
  if (!dataSets.loaded) loadModels();
  const data = dataSets.market_groups[String(market_group_id)];
  if (!data) throw new Error(`Market group ID ${market_group_id} not found in reference data`);
  return data;
}

export async function getAllChildMarketGroups(marketGroup: MarketGroup): Promise<MarketGroup[]> {
  const children = await Promise.all(marketGroup.child_market_group_ids.map((id) => getMarketGroup(id)));
  return children.concat(...(await Promise.all(children.map((child) => getAllChildMarketGroups(child)))));
}
