const hookQueue = [];

export function getHooks(hookType) {
  const res = [];
  hookQueue.forEach(hook => {
    if (hook && typeof hook[hookType] === 'function') {
      res.push(hook[hookType]);
    }
  });
  return res;
}

export async function executeHooks(hookType, { context, args }) {
  const hooks = getHooks(hookType);
  for (let i = 0; i < hooks.length; i++) {
    await hooks[i].call(context, ...args);
  }
}

export default function registerHooks(hook) {
  if (hook) {
    hookQueue.push(hook);
  }
}
