/**
 * AdSense module initialization.
 *
 * 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.
 */

/**
 * WordPress dependencies
 */
import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';

/**
 * Internal dependencies
 */
import { AREA_DASHBOARD_EARNINGS } from '../../googlesitekit/widgets/default-areas';
import { SetupMain } from './components/setup';
import {
	SettingsEdit,
	SettingsSetupIncomplete,
	SettingsView,
} from './components/settings';
import {
	DashboardSummaryWidget,
	DashboardTopEarningPagesWidget,
} from './components/dashboard';
import ModuleTopEarningPagesWidget from './components/module/ModuleTopEarningPagesWidget';
import { ModuleOverviewWidget } from './components/module';
import AdSenseIcon from '../../../svg/adsense.svg';
import { MODULES_ADSENSE } from './datastore/constants';
import {
	AREA_MODULE_ADSENSE_MAIN,
	CONTEXT_MODULE_ADSENSE,
	ERROR_CODE_ADBLOCKER_ACTIVE,
} from './constants';
import { WIDGET_AREA_STYLES } from '../../googlesitekit/widgets/datastore/constants';
export { registerStore } from './datastore';

export const registerModule = ( modules ) => {
	// This is called inside `registerModule` to prevent this file from having
	// side-effects. This is used to show notifications in the AdSense dashboard.
	/**
	 * Add components to the Notification requests.
	 */
	addFilter(
		'googlesitekit.ModulesNotificationsRequest',
		'googlesitekit.adsenseNotifications',
		( notificationModules ) => {
			return notificationModules.concat( 'adsense' );
		}
	);

	modules.registerModule( 'adsense', {
		storeName: MODULES_ADSENSE,
		SettingsEditComponent: SettingsEdit,
		SettingsViewComponent: SettingsView,
		SettingsSetupIncompleteComponent: SettingsSetupIncomplete,
		SetupComponent: SetupMain,
		Icon: AdSenseIcon,
		features: [
			__( 'Intelligent, automatic ad placement', 'google-site-kit' ),
			__( 'Revenue from ads placed on your site', 'google-site-kit' ),
			__( 'AdSense insights through Site Kit', 'google-site-kit' ),
		],
		checkRequirements: async ( registry ) => {
			const adBlockerActiveMsg = await registry
				.__experimentalResolveSelect( MODULES_ADSENSE )
				.getAdBlockerWarningMessage();

			if ( ! adBlockerActiveMsg ) {
				return;
			}

			throw {
				code: ERROR_CODE_ADBLOCKER_ACTIVE,
				message: adBlockerActiveMsg,
				data: null,
			};
		},
		screenWidgetContext: CONTEXT_MODULE_ADSENSE,
	} );
};

export const registerWidgets = ( widgets ) => {
	widgets.registerWidget(
		'adsenseSummary',
		{
			Component: DashboardSummaryWidget,
			width: widgets.WIDGET_WIDTHS.HALF,
			priority: 1,
			wrapWidget: false,
		},
		[ AREA_DASHBOARD_EARNINGS ]
	);
	widgets.registerWidget(
		'adsenseTopEarningPages',
		{
			Component: DashboardTopEarningPagesWidget,
			width: widgets.WIDGET_WIDTHS.HALF,
			priority: 2,
			wrapWidget: false,
		},
		[ AREA_DASHBOARD_EARNINGS ]
	);
	widgets.registerWidget(
		'adsenseModuleOverview',
		{
			Component: ModuleOverviewWidget,
			width: widgets.WIDGET_WIDTHS.FULL,
			priority: 1,
			wrapWidget: false,
		},
		[ AREA_MODULE_ADSENSE_MAIN ]
	);
	widgets.registerWidgetArea(
		AREA_MODULE_ADSENSE_MAIN,
		{
			priority: 1,
			style: WIDGET_AREA_STYLES.BOXES,
			title: __( 'Overview', 'google-site-kit' ),
		},
		CONTEXT_MODULE_ADSENSE
	);

	widgets.registerWidget(
		'adsenseModuleTopEarningPages',
		{
			Component: ModuleTopEarningPagesWidget,
			width: widgets.WIDGET_WIDTHS.FULL,
			priority: 2,
			wrapWidget: false,
		},
		[ AREA_MODULE_ADSENSE_MAIN ]
	);
};

@@ -2630,19 +2630,16 @@
 erActive
-Msg
  = await
@@ -2705,19 +2705,18 @@
  )%0A%09%09%09%09.
-get
+is
 AdBlocke
@@ -2720,21 +2720,13 @@
 cker
-WarningMessag
+Activ
 e();
@@ -2752,19 +2752,16 @@
 erActive
-Msg
  ) %7B%0A%09%09%09
@@ -2775,16 +2775,111 @@
 ;%0A%09%09%09%7D%0A%0A
+%09%09%09const message = registry%0A%09%09%09%09.select( MODULES_ADSENSE )%0A%09%09%09%09.getAdBlockerWarningMessage();%0A%0A
 %09%09%09throw
@@ -2935,28 +2935,8 @@
 sage
-: adBlockerActiveMsg
 ,%0A%09%09

Use a selector with a resolver for the resolveSelect call.