

import { FirebaseApp } from '@firebase/app-types';
import { AppConfig } from '../interfaces/app-config';
import { ERROR_FACTORY, ErrorCode } from '../util/errors';

export function extractAppConfig(app: FirebaseApp): AppConfig {
  if (!app || !app.options) {
    throw ERROR_FACTORY.create(ErrorCode.MISSING_APP_CONFIG_VALUES);
  }

  const appName = app.name;
  const { projectId, apiKey, appId } = app.options;

  if (!appName || !projectId || !apiKey || !appId) {
    throw ERROR_FACTORY.create(ErrorCode.MISSING_APP_CONFIG_VALUES);
  }

  return { appName, projectId, apiKey, appId };
}
