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

/**
 * WordPress dependencies
 */
import { addFilter } from '@wordpress/hooks';
import domReady from '@wordpress/dom-ready';

/**
 * Internal dependencies
 */
import Widgets from 'googlesitekit-widgets';
import { AREA_DASHBOARD_SPEED, AREA_PAGE_DASHBOARD_SPEED } from '../../googlesitekit/widgets/default-areas';
import './datastore';
import { fillFilterWithComponent, getModulesData } from '../../util';
import { createAddToFilter } from '../../util/helpers';
import { SettingsMain as PageSpeedInsightsSettings } from './components/settings';
import DashboardPageSpeedWidget from './components/dashboard/DashboardPageSpeedWidget';
import DashboardPageSpeedCTA from './components/dashboard/DashboardPageSpeedCTA';
import LegacyDashboardSpeed from './components/dashboard/LegacyDashboardSpeed';

/**
 * Add components to the settings page.
 */
addFilter(
	'googlesitekit.ModuleSettingsDetails-pagespeed-insights',
	'googlesitekit.PageSpeedInsightsModuleSettingsDetails',
	fillFilterWithComponent( PageSpeedInsightsSettings )
);

const {
	active,
	setupComplete,
} = getModulesData()[ 'pagespeed-insights' ];

// @TODO: remove LegacyDashboardSpeed once all widgets have been migrated.
if ( active && setupComplete ) {
	// Add to main dashboard.
	addFilter(
		'googlesitekit.DashboardModule',
		'googlesitekit.PageSpeedInsights',
		createAddToFilter( <LegacyDashboardSpeed /> ),
		45
	);

	// Add to dashboard-details view.
	addFilter(
		'googlesitekit.DashboardDetailsModule',
		'googlesitekit.PageSpeedInsights',
		createAddToFilter( <LegacyDashboardSpeed /> ),
		45
	);
} else {
	addFilter(
		'googlesitekit.DashboardModule',
		'googlesitekit.PageSpeedInsights',
		createAddToFilter( <DashboardPageSpeedCTA /> ),
		45
	);
}

domReady( () => {
	Widgets.registerWidget( 'pagespeedInsightsWebVitals', {
		component: DashboardPageSpeedWidget,
		width: Widgets.WIDGET_WIDTHS.FULL,
		wrapWidget: false,
	}, [ AREA_DASHBOARD_SPEED, AREA_PAGE_DASHBOARD_SPEED ] );
} );

@@ -813,24 +813,63 @@
 dencies%0A */%0A
+import Data from 'googlesitekit-data';%0A
 import Widge
@@ -897,24 +897,118 @@
 t-widgets';%0A
+import %7B STORE_NAME as CORE_MODULES %7D from '../../googlesitekit/modules/datastore/constants';%0A
 import %7B ARE
@@ -1138,33 +1138,8 @@
 rt %7B
- fillFilterWithComponent,
  get
@@ -1248,41 +1248,12 @@
 ings
-Main as PageSpeedInsightsSettings
+View
  %7D f
@@ -1536,241 +1536,8 @@
 ';%0A%0A
-/**%0A * Add components to the settings page.%0A */%0AaddFilter(%0A%09'googlesitekit.ModuleSettingsDetails-pagespeed-insights',%0A%09'googlesitekit.PageSpeedInsightsModuleSettingsDetails',%0A%09fillFilterWithComponent( PageSpeedInsightsSettings )%0A);%0A%0A
 cons
@@ -2247,16 +2247,140 @@
 () =%3E %7B%0A
+%09Data.dispatch( CORE_MODULES ).registerModule(%0A%09%09'pagespeed-insights',%0A%09%09%7B%0A%09%09%09settingsViewComponent: SettingsView,%0A%09%09%7D%0A%09);%0A%0A
 %09Widgets

Refactor PageSpeed Insights with registered components.