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

export interface Schematic {
  readonly schematic_id: number;
  readonly cycle_time: number;
  readonly name: LocalizedString;
  readonly materials: { [type_id: string]: TypeIDQuantity };
  readonly products: { [type_id: string]: TypeIDQuantity };
  readonly pin_type_ids: number[];
}

export function getSchematic(schematic_id: number) {
  if (!dataSets.loaded) loadModels();
  const data = dataSets.schematics[String(schematic_id)];
  if (!data) throw new Error(`Schematic ID ${schematic_id} not found in reference data`);
  return data;
}

export function getMaterialQuantities(schematic: Schematic) {
  return Object.entries(schematic.materials).map(([type_id, { quantity }]) => ({
    type: getType(Number(type_id)),
    quantity,
  }));
}

export function getProductQuantities(schematic: Schematic) {
  return Object.entries(schematic.products).map(([type_id, { quantity }]) => ({
    type: getType(Number(type_id)),
    quantity,
  }));
}

export function getPinTypes(schematic: Schematic) {
  return schematic.pin_type_ids.map(getType);
}
