import yamlSafe from 'js-yaml';

import { config, handlebars, handlebarsSync, addTemplateDir } from '@opengis/fastify-table/utils.js';


import isAdmin from './server/utils/isAdmin.js';
import addNotification from './server/utils/addNotification.js';
import sendNotification from './server/utils/sendNotification.js';

async function getAdminAccess({
  id, user = {},
}) {
  if (!id) {
    return { message: 'not enough params: id', status: 400 };
  }

  if (!config?.local && !['admin'].includes(user.user_type, user.type)) {
    return { message: 'access restricted: admin', status: 403 };
  }
  return null;
}

function loadSafe(yml) {
  try {
    return yamlSafe.load(yml);
  } catch (err) {
    return { error: err.toString() };
  }
};

Object.assign(yamlSafe, { loadSafe });

export default null;
export {
  isAdmin,
  yamlSafe,
  addNotification,
  sendNotification,
  getAdminAccess,
  handlebars,
  handlebarsSync,
  addTemplateDir,
};
