/**
 * 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 ] );
} );

/**
 * 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 Data from 'googlesitekit-data';
import Widgets from 'googlesitekit-widgets';
import { STORE_NAME as CORE_MODULES } from '../../googlesitekit/modules/datastore/constants';
import { AREA_DASHBOARD_SPEED, AREA_PAGE_DASHBOARD_SPEED } from '../../googlesitekit/widgets/default-areas';
import './datastore';
import { getModulesData } from '../../util';
import { createAddToFilter } from '../../util/helpers';
import { SettingsView } from './components/settings';
import DashboardPageSpeedWidget from './components/dashboard/DashboardPageSpeedWidget';
import DashboardPageSpeedCTA from './components/dashboard/DashboardPageSpeedCTA';
import LegacyDashboardSpeed from './components/dashboard/LegacyDashboardSpeed';

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( () => {
	Data.dispatch( CORE_MODULES ).registerModule(
		'pagespeed-insights',
		{
			settingsViewComponent: SettingsView,
		}
	);

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

Refactor PageSpeed Insights with registered components.