import { apiApp } from './api';

/**
 *
 * @param {string} databaseName
 * @returns {Promise<import('@type/AutoShopee/Db').CouchDbReturn>}
 */
export const getCouchDatabase = async (databaseName) => {
  return (await apiApp.post('/extension/db/get', {
    databaseName,
  })).data;
};

/**
 *
 * @param {string} databaseName
 * @returns {Promise<import('@type/AutoShopee/Db').CouchDbReturn>}
 */
export const createCouchDatabase = async (databaseName) => {
  return (await apiApp.post('/extension/db/create', {
    databaseName,
  })).data;
};

/**
 *
 * @param {string} databaseName
 * @returns {Promise<import('@type/AutoShopee/Db').CouchDbReturn>}
 */
export const fetchCouchDatabase = async (databaseName) => {
  const result = await getCouchDatabase(databaseName);

  if (result && result.connect && result.db) {
    return result;
  }

  return createCouchDatabase(databaseName);
};
