import {
	isUpgradeNudgeEnabled,
	isUpgradable,
	isStillUsableWithFreePlan,
} from '@automattic/jetpack-shared-extension-utils';
import domReady from '@wordpress/dom-ready';
import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import paidBlockMediaPlaceholder from './media-placeholder';
import paidBlockMediaReplaceFlow from './media-replace-flow';
import renderPaidIcon from './render-paid-icon.js';
import { blockEditWithUpgradeBanner } from './with-upgrade-banner';

import './editor.scss';

const stylesByPaidBlocks = [];

const jetpackPaidBlock = ( settings, name ) => {
	if ( isUpgradable( name ) ) {
		if ( ! stylesByPaidBlocks.includes( name ) ) {
			stylesByPaidBlocks.push( name );
		}

		// Populate block keywords.
		settings.keywords = [
			...new Set( [ ...settings.keywords, 'premium', __( 'premium', 'jetpack' ) ] ),
		];

		// Extend Icon for Paid blocks.
		if ( ! isStillUsableWithFreePlan( name ) ) {
			settings.icon = renderPaidIcon( settings.icon );
		}

		// Add the attributes for rendering upgrade nudges.
		if ( ! settings.attributes.shouldDisplayFrontendBanner ) {
			settings.attributes.shouldDisplayFrontendBanner = {
				type: 'boolean',
				default: true,
			};
		}

		// Ensure that the toolbar of the inner blocks doesn't overlap the upgrade banner by displaying the controls
		// of the inner blocks in the parent block toolbar (which is always placed above the upgrade banner).
		// The cover block is excluded from this behavior because the toolbars of its inner blocks do not interfere.
		if ( name !== 'core/cover' ) {
			settings.supports = {
				...settings.supports,
				__experimentalExposeControlsToChildren: true,
			};
		}
	}

	return settings;
};

// Extend BlockType.
addFilter( 'blocks.registerBlockType', 'jetpack/paid-block', jetpackPaidBlock );

addFilter(
	'blocks.registerBlockType',
	'jetpack/paid-block-with-upgrade-banner',
	blockEditWithUpgradeBanner
);

// Take the control of the MediaPlaceholder.
addFilter(
	'editor.MediaPlaceholder',
	'jetpack/paid-block-media-placeholder',
	paidBlockMediaPlaceholder
);

// Take the control of the MediaReplaceFlow.
addFilter(
	'editor.MediaReplaceFlow',
	'jetpack/paid-block-media-placeholder',
	paidBlockMediaReplaceFlow
);

/*
 * Add the `jetpack-enable-upgrade-nudge` css Class
 * to the document body if the feature is enabled.
 */
domReady( function () {
	if ( isUpgradeNudgeEnabled() ) {
		document.body.classList.add( 'jetpack-enable-upgrade-nudge' );
	}
} );
