context.ts

// Intialize firebase
// Firebase
import * as firebase from 'firebase/app';
import 'firebase/firestore';
import { PPP, PROD } from '../config/constants';
import { config } from '../firebase/config';
import { getActiveDb } from './helpers/active-db';
// import { graphQlAuth } from './helpers/authenticate';

firebase.initializeApp(config);

function getEnvironment(referer) {
  switch (referer) {
    case 'https://coco.is/':
      return PROD;
    case 'http://localhost:7575/':
    case 'https://coco-ppp.web.app/':
    default:
      return PPP;
  }
}

async function getActiveDbId(firestore, envmt) {
  const result = await getActiveDb({ firestore, envmt });
  const id = result?.id || 0;
  console.log('getActiveDb():', id);
  return id;
}

export async function getContextObject(req) {
  // const user = await graphQlAuth(req);
  const firestore = firebase.firestore();
  const { referer } = req.headers;
  const envmt = getEnvironment(referer);
  const activeDbId = await getActiveDbId(firestore, envmt);
  return {
    firestore,
    envmt,
    activeDbId
  };
}