'use strict';

const passport = require('koa-passport');
const { isFunction } = require('lodash/fp');

const {createLocalStrategy, createLocalStrategySlack } = require('./passport/local-strategy');

const authEventsMapper = {
  onConnectionSuccess: 'admin.auth.success',
  onConnectionError: 'admin.auth.error',
};

const valueIsFunctionType = ([, value]) => isFunction(value);
const keyIsValidEventName = ([key]) => {
  return Object.keys(strapi.admin.services.passport.authEventsMapper).includes(key);
};

const getPassportStrategies = () => [{"type": "local", "strategy": createLocalStrategy(strapi)}, {"type": "slack", "strategy": createLocalStrategySlack(strapi)}];

const registerAuthEvents = () => {
  const { events = {} } = strapi.config.get('server.admin.auth', {});
  const { authEventsMapper } = strapi.admin.services.passport;

  const eventList = Object.entries(events)
    .filter(keyIsValidEventName)
    .filter(valueIsFunctionType);

  for (const [eventName, handler] of eventList) {
    strapi.eventHub.on(authEventsMapper[eventName], handler);
  }
};

const init = () => {
  strapi.admin.services.passport
    .getPassportStrategies()
    .forEach(strategy => passport.use(strategy.type, strategy.strategy));

  registerAuthEvents();

  return passport.initialize();
};

module.exports = {
  init,
  getPassportStrategies,
  authEventsMapper,
};
