<commit_before>/**
 * modules/analytics data store: tags.
 *
 * Site Kit by Google, Copyright 2020 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 invariant from 'invariant';

/**
 * Internal dependencies
 */
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { getExistingTag } from '../../../util/tag';
import { STORE_NAME } from './constants';
import { isValidPropertyID } from '../util';
import { createFetchStore } from '../../../googlesitekit/data/create-fetch-store';

const { createRegistrySelector, createRegistryControl } = Data;

const fetchGetExistingTagStore = createFetchStore( {
	baseName: 'getExistingTag',
	controlCallback: () => {
		// TODO: Replace this with data from `core/site` selectors and
		// an implementation contained inside the store
		// once https://github.com/google/site-kit-wp/issues/1000 is
		// implemented.
		// TODO: Test this in the future. The underlying implementation is
		// currently quite nested and difficult to straightforwardly test.
		return getExistingTag( 'analytics' );
	},
	reducerCallback: ( state, existingTag ) => {
		return {
			...state,
			existingTag: existingTag || null,
		};
	},
} );

const fetchGetTagPermissionStore = createFetchStore( {
	baseName: 'getTagPermission',
	controlCallback: ( { propertyID } ) => {
		return API.get( 'modules', 'analytics', 'tag-permission', { propertyID }, {
			useCache: false,
		} );
	},
	reducerCallback: ( state, { accountID, permission }, { propertyID } ) => {
		return {
			...state,
			tagPermissions: {
				...state.tagPermissions || {},
				[ propertyID ]: { accountID, permission },
			},
		};
	},
	argsToParams: ( propertyID ) => {
		invariant( propertyID, 'propertyID is required.' );
		return { propertyID };
	},
} );

// Actions
const WAIT_FOR_EXISTING_TAG = 'WAIT_FOR_EXISTING_TAG';
const WAIT_FOR_EXISTING_TAG_PERMISSION = 'WAIT_FOR_EXISTING_TAG_PERMISSION';

const BASE_INITIAL_STATE = {
	existingTag: undefined,
	tagPermissions: {},
};

const baseActions = {
	waitForExistingTag() {
		return {
			payload: {},
			type: WAIT_FOR_EXISTING_TAG,
		};
	},
	waitForExistingTagPermission( existingTag ) {
		return {
			payload: { existingTag },
			type: WAIT_FOR_EXISTING_TAG_PERMISSION,
		};
	},
};

const baseControls = {
	[ WAIT_FOR_EXISTING_TAG ]: createRegistryControl( ( registry ) => () => {
		const isExistingTagLoaded = () => registry.select( STORE_NAME ).getExistingTag() !== undefined;
		if ( isExistingTagLoaded() ) {
			return true;
		}

		return new Promise( ( resolve ) => {
			const unsubscribe = registry.subscribe( () => {
				if ( isExistingTagLoaded() ) {
					unsubscribe();
					resolve();
				}
			} );
		} );
	} ),
	[ WAIT_FOR_EXISTING_TAG_PERMISSION ]: createRegistryControl( ( registry ) => ( { payload: { existingTag } } ) => {
		// Select first to ensure resolution is always triggered.
		const analyticsStore = registry.select( STORE_NAME );
		analyticsStore.getTagPermission( existingTag );
		const isTagPermissionLoaded = () => analyticsStore.hasFinishedResolution( 'getTagPermission', [ existingTag ] );
		if ( isTagPermissionLoaded() ) {
			return;
		}
		return new Promise( ( resolve ) => {
			const unsubscribe = registry.subscribe( () => {
				if ( isTagPermissionLoaded() ) {
					unsubscribe();
					resolve();
				}
			} );
		} );
	} ),
};

const baseResolvers = {
	*getExistingTag() {
		const registry = yield Data.commonActions.getRegistry();

		const existingTag = registry.select( STORE_NAME ).getExistingTag();
<commit_msg>Rename actionCreator and related constant.<commit_after>/**
 * modules/analytics data store: tags.
 *
 * Site Kit by Google, Copyright 2020 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 invariant from 'invariant';

/**
 * Internal dependencies
 */
import API from 'googlesitekit-api';
import Data from 'googlesitekit-data';
import { getExistingTag } from '../../../util/tag';
import { STORE_NAME } from './constants';
import { isValidPropertyID } from '../util';
import { createFetchStore } from '../../../googlesitekit/data/create-fetch-store';

const { createRegistrySelector, createRegistryControl } = Data;

const fetchGetExistingTagStore = createFetchStore( {
	baseName: 'getExistingTag',
	controlCallback: () => {
		// TODO: Replace this with data from `core/site` selectors and
		// an implementation contained inside the store
		// once https://github.com/google/site-kit-wp/issues/1000 is
		// implemented.
		// TODO: Test this in the future. The underlying implementation is
		// currently quite nested and difficult to straightforwardly test.
		return getExistingTag( 'analytics' );
	},
	reducerCallback: ( state, existingTag ) => {
		return {
			...state,
			existingTag: existingTag || null,
		};
	},
} );

const fetchGetTagPermissionStore = createFetchStore( {
	baseName: 'getTagPermission',
	controlCallback: ( { propertyID } ) => {
		return API.get( 'modules', 'analytics', 'tag-permission', { propertyID }, {
			useCache: false,
		} );
	},
	reducerCallback: ( state, { accountID, permission }, { propertyID } ) => {
		return {
			...state,
			tagPermissions: {
				...state.tagPermissions || {},
				[ propertyID ]: { accountID, permission },
			},
		};
	},
	argsToParams: ( propertyID ) => {
		invariant( propertyID, 'propertyID is required.' );
		return { propertyID };
	},
} );

// Actions
const WAIT_FOR_EXISTING_TAG = 'WAIT_FOR_EXISTING_TAG';
const WAIT_FOR_TAG_PERMISSION = 'WAIT_FOR_TAG_PERMISSION';

const BASE_INITIAL_STATE = {
	existingTag: undefined,
	tagPermissions: {},
};

const baseActions = {
	waitForExistingTag() {
		return {
			payload: {},
			type: WAIT_FOR_EXISTING_TAG,
		};
	},
	waitForTagPermission( propertyID ) {
		return {
			payload: { propertyID },
			type: WAIT_FOR_TAG_PERMISSION,
		};
	},
};

const baseControls = {
	[ WAIT_FOR_EXISTING_TAG ]: createRegistryControl( ( registry ) => () => {
		const isExistingTagLoaded = () => registry.select( STORE_NAME ).getExistingTag() !== undefined;
		if ( isExistingTagLoaded() ) {
			return true;
		}

		return new Promise( ( resolve ) => {
			const unsubscribe = registry.subscribe( () => {
				if ( isExistingTagLoaded() ) {
					unsubscribe();
					resolve();
				}
			} );
		} );
	} ),
	[ WAIT_FOR_TAG_PERMISSION ]: createRegistryControl( ( registry ) => ( { payload: { propertyID } } ) => {
		// Select first to ensure resolution is always triggered.
		const { getTagPermission, hasFinishedResolution } = registry.select( STORE_NAME );
		getTagPermission( propertyID );
		const isTagPermissionLoaded = () => hasFinishedResolution( 'getTagPermission', [ propertyID ] );
		if ( isTagPermissionLoaded() ) {
			return;
		}
		return new Promise( ( resolve ) => {
			const unsubscribe = registry.subscribe( () => {
				if ( isTagPermissionLoaded() ) {
					unsubscribe();
					resolve();
				}
			} );
		} );
	} ),
};

const baseResolvers = {
	*getExistingTag() {
		const registry = yield Data.commonActions.getRegistry();

		const existingTag = registry.select( STORE_NAME ).getExistingTag();

{'fraction_non_alphanumeric': 0.13144615384615385, 'fraction_numerical': 0.0029538461538461537, 'mean_word_length': 3.760398359695372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 4, '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': '5386394', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2431, 'n_words': 643}