import env from "../enviroment/env";
import { IFileDB, IFolderDB, IUserDB, IThumbnailDB } from "./dbTypes";

import MongoFileDB from "./mongoDB/fileDB";
import MongoFolderDB from "./mongoDB/folderDB";
import MongoUserDB from "./mongoDB/userDB";
import MongoThumbnailDB from "./mongoDB/thumbnailDB";

// The sqlite/* modules are required lazily (rather than statically imported)
// so that opening the sqlite connection - a side effect of importing
// db/connections/sqlite - only happens when DB_ENGINE=sqlite is actually
// selected, not on every process that merely imports this factory.
const isSqlite = () => env.dbEngine === "sqlite";

export const getFileDB = (): IFileDB => {
  if (isSqlite()) {
    const SqliteFileDB = require("./sqliteDB/fileDB").default;
    return new SqliteFileDB();
  }
  return new MongoFileDB();
};

export const getFolderDB = (): IFolderDB => {
  if (isSqlite()) {
    const SqliteFolderDB = require("./sqliteDB/folderDB").default;
    return new SqliteFolderDB();
  }
  return new MongoFolderDB();
};

export const getUserDB = (): IUserDB => {
  if (isSqlite()) {
    const SqliteUserDB = require("./sqliteDB/userDB").default;
    return new SqliteUserDB();
  }
  return new MongoUserDB();
};

export const getThumbnailDB = (): IThumbnailDB => {
  if (isSqlite()) {
    const SqliteThumbnailDB = require("./sqliteDB/thumbnailDB").default;
    return new SqliteThumbnailDB();
  }
  return new MongoThumbnailDB();
};
