import * as logSymbols from 'log-symbols';
import { AppJsonFile, appInstallModel } from '../../../core/app';
import { Spawner, runWithSpinner, echo } from '../../../util';

export async function appInstallModelsComponent(spawner: Spawner) {
  const appJsonFile = AppJsonFile();
  const appJson = appJsonFile.read();

  let hasModels = false;
  if (appJson.models) {
    for (const [id, version] of Object.entries(appJson.models)) {
      hasModels = true;
      await runWithSpinner(
        appInstallModel,
        [spawner, id, version],
        `Install model ${id}`
      );
    }
  }

  if (!hasModels) {
    echo(`${logSymbols.warning} Application has no models`);
  }
}
