{"size": 2050, "ext": "js", "max_stars_repo_path": "packages/core/admin/server/services/permission/sections-builder/section.js", "max_stars_repo_name": "Mithenks/strapi", "max_stars_repo_stars_event_min_datetime": "2016-08-12T11:50:57.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T22:36:15.000Z", "max_issues_repo_path": "packages/core/admin/server/services/permission/sections-builder/section.js", "max_issues_repo_name": "Mithenks/strapi", "max_issues_repo_issues_event_min_datetime": "2016-08-12T10:05:15.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T22:24:00.000Z", "max_forks_repo_path": "packages/core/admin/server/services/permission/sections-builder/section.js", "max_forks_repo_name": "Mithenks/strapi", "max_forks_repo_forks_event_min_datetime": "2016-08-29T12:14:54.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T18:46:21.000Z"}
{"max_stars_count": 46173, "max_issues_count": 10397, "max_forks_count": 7214, "avg_line_length": 28.4722222222, "max_line_length": 121, "alphanum_fraction": 0.6595121951}
'use strict';

const { eq } = require('lodash/fp');
const { hooks } = require('@strapi/utils');

/**
 * @typedef SectionOptions
 * @property {function():any} initialStateFactory - A factory function that returns the default shape of the section
 * @property {Array<Function>} handlers - An initial collection of handlers which will be registered in the handlers hook
 * @property {Array<Function>} matchers - An initial collection of matchers which will be registered in the matchers hook
 */

const emptyObjectFactory = () => ({});

/**
 * Upon call, creates a new section object
 * @param {SectionOptions} options
 */
const createSection = ({
  initialStateFactory = emptyObjectFactory,
  handlers = [],
  matchers = [],
} = {}) => {
  const state = {
    hooks: {
      handlers: hooks.createAsyncSeriesHook(),
      matchers: hooks.createAsyncParallelHook(),
    },
  };

  // Register initial hooks
  handlers.forEach(handler => state.hooks.handlers.register(handler));
  matchers.forEach(matcher => state.hooks.matchers.register(matcher));

  return {
    hooks: state.hooks,

    /**
     * Verifies if an action can be applied to the section by running the matchers hook.
     * If any of the registered matcher functions returns true, then the condition applies.
     * @param {Action} action
     * @return {Promise<boolean>}
     */
    async appliesToAction(action) {
      const results = await state.hooks.matchers.call(action);

      return results.some(eq(true));
    },

    /**
     * Init, build and returns a section object based on the given actions
     * @param {Array<Action>} actions - A list of actions used to populate the section
     * @return {Promise<any>}
     */
    async build(actions = []) {
      const section = initialStateFactory();

      for (const action of actions) {
        const applies = await this.appliesToAction(action);

        if (applies) {
          await state.hooks.handlers.call({ action, section });
        }
      }

      return section;
    },
  };
};

module.exports = createSection;
