<commit_before>/**
 * `core/modules` data store: module info.
 *
 * Site Kit by Google, Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import memize from 'memize';
import defaults from 'lodash/defaults';
import merge from 'lodash/merge';
import isPlainObject from 'lodash/isPlainObject';
import invariant from 'invariant';
import { sprintf, __ } from '@wordpress/i18n';

/**
 * WordPress dependencies
 */
// This is used for JSDoc purposes.
// eslint-disable-next-line no-unused-vars
import { WPComponent } from '@wordpress/element';

/**
 * Internal dependencies
 */
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import {
	CORE_MODULES,
	ERROR_CODE_INSUFFICIENT_MODULE_DEPENDENCIES,
} from './constants';
import { CORE_SITE } from '../../datastore/site/constants';
import { CORE_USER } from '../../datastore/user/constants';
import { createFetchStore } from '../../data/create-fetch-store';
import { listFormat } from '../../../util';
import DefaultSettingsSetupIncomplete from '../../../components/settings/DefaultSettingsSetupIncomplete';
import { createValidatedAction } from '../../data/utils';

const { createRegistrySelector, createRegistryControl } = Data;

// Actions.
const REFETCH_AUTHENTICATION = 'REFETCH_AUTHENTICATION';
const SELECT_MODULE_REAUTH_URL = 'SELECT_MODULE_REAUTH_URL';
const REGISTER_MODULE = 'REGISTER_MODULE';
const RECEIVE_CHECK_REQUIREMENTS_ERROR = 'RECEIVE_CHECK_REQUIREMENTS_ERROR';
const RECEIVE_CHECK_REQUIREMENTS_SUCCESS = 'RECEIVE_CHECK_REQUIREMENTS_SUCCESS';
const RECEIVE_RECOVERABLE_MODULES = 'RECEIVE_RECOVERABLE_MODULES';
const RECEIVE_SHARED_OWNERSHIP_MODULES = 'RECEIVE_SHARED_OWNERSHIP_MODULES';

const moduleDefaults = {
	slug: '',
	storeName: null,
	name: '',
	description: '',
	homepage: null,
	internal: false,
	active: false,
	connected: false,
	dependencies: [],
	dependants: [],
	order: 10,
	features: [],
	Icon: null,
	SettingsEditComponent: null,
	SettingsViewComponent: null,
	SettingsSetupIncompleteComponent: DefaultSettingsSetupIncomplete,
	SetupComponent: null,
	checkRequirements: () => true,
};

const normalizeModules = memize( ( serverDefinitions, clientDefinitions ) => {
	// Module properties in `clientDefinitions` will overwrite `serverDefinitions`
	// but only for keys whose values are not `undefined`.
	const modules = merge( {}, serverDefinitions, clientDefinitions );

	return Object.keys( modules )
		.map( ( slug ) => {
			const module = { ...modules[ slug ], slug };
			// Fill any `undefined` values with defaults.
			defaults( module, { name: slug }, moduleDefaults );

			return module;
		} )
		.sort( ( a, b ) => a.order - b.order )
		.reduce( ( acc, module ) => {
			return { ...acc, [ module.slug ]: module };
		}, {} );
} );

/**
 * Memoized function to build an object mapping recoverable module slugs to their corresponding
 * module objects.
 *
 * @since n.e.x.t
 *
 * @param {Object} modules            Module definitions.
 * @param {Array}  recoverableModules Array of recoverable module slugs.
 * @return {Object} Map of recoverable module slugs to their corresponding module objects.
 */
const calculateRecoverableModules = memize( ( modules, recoverableModules ) =>
	Object.values( modules ).reduce( ( recoverable, module ) => {
		if ( recoverableModules.includes( module.slug ) ) {
			return {
				...recoverable,
				[ module.slug ]: module,
			};
		}

		return recoverable;
	}, {} )
);

const fetchGetModulesStore = createFetchStore( {
	baseName: 'getModules',<commit_msg>Update JSDoc to adhere to conventions.<commit_after>/**
 * `core/modules` data store: module info.
 *
 * Site Kit by Google, Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import memize from 'memize';
import defaults from 'lodash/defaults';
import merge from 'lodash/merge';
import isPlainObject from 'lodash/isPlainObject';
import invariant from 'invariant';
import { sprintf, __ } from '@wordpress/i18n';

/**
 * WordPress dependencies
 */
// This is used for JSDoc purposes.
// eslint-disable-next-line no-unused-vars
import { WPComponent } from '@wordpress/element';

/**
 * Internal dependencies
 */
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import {
	CORE_MODULES,
	ERROR_CODE_INSUFFICIENT_MODULE_DEPENDENCIES,
} from './constants';
import { CORE_SITE } from '../../datastore/site/constants';
import { CORE_USER } from '../../datastore/user/constants';
import { createFetchStore } from '../../data/create-fetch-store';
import { listFormat } from '../../../util';
import DefaultSettingsSetupIncomplete from '../../../components/settings/DefaultSettingsSetupIncomplete';
import { createValidatedAction } from '../../data/utils';

const { createRegistrySelector, createRegistryControl } = Data;

// Actions.
const REFETCH_AUTHENTICATION = 'REFETCH_AUTHENTICATION';
const SELECT_MODULE_REAUTH_URL = 'SELECT_MODULE_REAUTH_URL';
const REGISTER_MODULE = 'REGISTER_MODULE';
const RECEIVE_CHECK_REQUIREMENTS_ERROR = 'RECEIVE_CHECK_REQUIREMENTS_ERROR';
const RECEIVE_CHECK_REQUIREMENTS_SUCCESS = 'RECEIVE_CHECK_REQUIREMENTS_SUCCESS';
const RECEIVE_RECOVERABLE_MODULES = 'RECEIVE_RECOVERABLE_MODULES';
const RECEIVE_SHARED_OWNERSHIP_MODULES = 'RECEIVE_SHARED_OWNERSHIP_MODULES';

const moduleDefaults = {
	slug: '',
	storeName: null,
	name: '',
	description: '',
	homepage: null,
	internal: false,
	active: false,
	connected: false,
	dependencies: [],
	dependants: [],
	order: 10,
	features: [],
	Icon: null,
	SettingsEditComponent: null,
	SettingsViewComponent: null,
	SettingsSetupIncompleteComponent: DefaultSettingsSetupIncomplete,
	SetupComponent: null,
	checkRequirements: () => true,
};

const normalizeModules = memize( ( serverDefinitions, clientDefinitions ) => {
	// Module properties in `clientDefinitions` will overwrite `serverDefinitions`
	// but only for keys whose values are not `undefined`.
	const modules = merge( {}, serverDefinitions, clientDefinitions );

	return Object.keys( modules )
		.map( ( slug ) => {
			const module = { ...modules[ slug ], slug };
			// Fill any `undefined` values with defaults.
			defaults( module, { name: slug }, moduleDefaults );

			return module;
		} )
		.sort( ( a, b ) => a.order - b.order )
		.reduce( ( acc, module ) => {
			return { ...acc, [ module.slug ]: module };
		}, {} );
} );

/**
 * Gets a memoized object mapping recoverable module slugs to their corresponding
 * module objects.
 *
 * @since n.e.x.t
 *
 * @param {Object} modules            Module definitions.
 * @param {Array}  recoverableModules Array of recoverable module slugs.
 * @return {Object} Map of recoverable module slugs to their corresponding module objects.
 */
const calculateRecoverableModules = memize( ( modules, recoverableModules ) =>
	Object.values( modules ).reduce( ( recoverable, module ) => {
		if ( recoverableModules.includes( module.slug ) ) {
			return {
				...recoverable,
				[ module.slug ]: module,
			};
		}

		return recoverable;
	}, {} )
);

const fetchGetModulesStore = createFetchStore( {
	baseName: 'getModules',
{'fraction_non_alphanumeric': 0.11983319023672268, 'fraction_numerical': 0.002943701704893904, 'mean_word_length': 4.325930764206401, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1081179', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2408, 'n_words': 714}