<commit_before>/**
 * WordPress dependencies
 */
import { withSelect } from '@wordpress/data';
import { Fragment } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { compose, createHigherOrderComponent } from '@wordpress/compose';

/**
 * Internal dependencies
 */
import { ALLOWED_TOP_LEVEL_BLOCKS } from '../constants';
import { withBlockName } from './';

const applyWithSelect = withSelect( ( select, props ) => {
	const {
		getBlockOrder,
	} = select( 'core/editor' );

	return {
		pageNumber: getBlockOrder().indexOf( props.clientId ) + 1,
	};
} );

const wrapperWithSelect = compose(
	applyWithSelect,
	withBlockName,
);

/**
 * Add page number label to page blocks
 *
 * @param {Object} BlockListBlock BlockListBlock element.
 * @return {Function} Handler.
 */
export default createHigherOrderComponent(
	( BlockEdit ) => {
		return wrapperWithSelect( ( props ) => {
			const { blockName, pageNumber } = props;

			// If it's not an allowed block then lets return original;
			if ( ! ALLOWED_TOP_LEVEL_BLOCKS.includes( blockName ) ) {
				return <BlockEdit { ...props } />;
			}

			return (
				<Fragment>
					<div className="amp-story-page-number">
						{
							/* translators: %s: Page number */
							sprintf( __( 'Page %s', 'amp' ), pageNumber )
						}
					</div>
					<BlockEdit { ...props } />
				</Fragment>
			);
		} );
	},
	'withPageNumber'
);<commit_msg>Only show page number for top level blocks<commit_after>/**
 * WordPress dependencies
 */
import { withSelect } from '@wordpress/data';
import { Fragment } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { compose, createHigherOrderComponent } from '@wordpress/compose';

/**
 * Internal dependencies
 */
import { ALLOWED_TOP_LEVEL_BLOCKS } from '../constants';
import { withBlockName } from './';

const applyWithSelect = withSelect( ( select, props ) => {
	const {
		getBlockOrder,
		getBlockRootClientId,
	} = select( 'core/editor' );

	return {
		pageNumber: '' === getBlockRootClientId() ? getBlockOrder().indexOf( props.clientId ) + 1 : undefined,
	};
} );

const wrapperWithSelect = compose(
	applyWithSelect,
	withBlockName,
);

/**
 * Add page number label to page blocks
 *
 * @param {Object} BlockListBlock BlockListBlock element.
 * @return {Function} Handler.
 */
export default createHigherOrderComponent(
	( BlockEdit ) => {
		return wrapperWithSelect( ( props ) => {
			const { blockName, pageNumber } = props;

			// Not a valid top level block.
			if ( ! ALLOWED_TOP_LEVEL_BLOCKS.includes( blockName ) || ! pageNumber ) {
				return <BlockEdit { ...props } />;
			}

			return (
				<Fragment>
					<div className="amp-story-page-number">
						{
							/* translators: %s: Page number */
							sprintf( __( 'Page %s', 'amp' ), pageNumber )
						}
					</div>
					<BlockEdit { ...props } />
				</Fragment>
			);
		} );
	},
	'withPageNumber'
);
{'fraction_non_alphanumeric': 0.14845360824742268, 'fraction_numerical': 0.002061855670103093, 'mean_word_length': 3.325408618127786, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2637434', 'n_tokens_mistral': 1038, 'n_tokens_neox': 895, 'n_words': 223}