/**
 * CompleteModuleActivationCTA component.
 *
 * Site Kit by Google, Copyright 2019 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.
 */

/**
 * External dependencies
 */
import PropTypes from 'prop-types';

/**
 * WordPress dependencies
 */
import { useCallback } from '@wordpress/element';
import { sprintf, __ } from '@wordpress/i18n';

/**
 * Internal dependencies
 */
import CTA from './legacy-notifications/cta';
import Data from 'googlesitekit-data';
import { STORE_NAME as CORE_USER, PERMISSION_MANAGE_OPTIONS } from '../googlesitekit/datastore/user/constants';
import { STORE_NAME as MODULES_STORE } from '../googlesitekit/modules/datastore/constants';
const { useSelect } = Data;

const CompleteModuleActivationCTA = ( { slug, title, description } ) => {
	const module = useSelect( ( select ) => select( MODULES_STORE ).getModule( slug ) );
	const adminReauthURL = useSelect( ( select ) => select( `modules/${ slug }` )?.getAdminReauthURL() );
	const canManageOptions = useSelect( ( select ) => select( CORE_USER ).hasCapability( PERMISSION_MANAGE_OPTIONS ) );
	const { name } = module;

	const onCTAClick = useCallback( async () => {
		global.location = adminReauthURL;
	} );

	if ( ! canManageOptions ) {
		return null;
	}

	const moduleTitle = title ||
		sprintf(
			/* translators: %s: Module name */
			__( 'Complete %s activation', 'google-site-kit' ),
			name,
		);
	const moduleDescription = description ||
		sprintf(
			/* translators: %s: Module name */
			__( '%s module setup needs to be completed', 'google-site-kit' ),
			name,
		);
	const moduleCTA = __( 'Complete setup', 'google-site-kit' );

	const moduleAriaLabel = sprintf(
		/* translators: %s: Module name */
		__( 'Complete %s setup', 'google-site-kit' ),
		name,
	);

	return (
		<CTA
			title={ moduleTitle }
			description={ moduleDescription }
			onClick={ onCTAClick }
			ctaLabel={ moduleCTA }
			aria-label={ moduleAriaLabel }
		/>
	);
};

CompleteModuleActivationCTA.propTypes = {
	slug: PropTypes.string.isRequired,
	title: PropTypes.string,
	description: PropTypes.string,
};

export default CompleteModuleActivationCTA;

@@ -1591,34 +1591,8 @@
 ) );
-%0A%09const %7B name %7D = module;
 %0A%0A%09c
@@ -1653,18 +1653,24 @@
 location
- =
+.assign(
  adminRe
@@ -1680,22 +1680,62 @@
 hURL
+ )
 ;%0A%09%7D
- );%0A%0A%09if (
+, %5B adminReauthURL %5D );%0A%0A%09if ( ! module?.name %7C%7C
  ! c
@@ -1778,36 +1778,47 @@
 %7D%0A%0A%09
-const moduleTitle = 
+return (%0A%09%09%3CCTA%0A%09%09%09title=%7B%0A%09%09%09%09
 title %7C%7C
 %0A%09%09s
@@ -1813,19 +1813,17 @@
 title %7C%7C
-%0A%09%09
+ 
 sprintf(
@@ -1815,32 +1815,34 @@
 tle %7C%7C sprintf(%0A
+%09%09
 %09%09%09/* translator
@@ -1862,24 +1862,26 @@
  name */%0A%09%09%09
+%09%09
 __( 'Complet
@@ -1926,33 +1926,38 @@
 %0A%09%09%09
+%09%09module.
 name,%0A
+%09%09
 %09%09)
-;
 %0A%09
-const moduleD
+%09%09%7D%0A%09%09%09d
 escr
@@ -1962,19 +1962,23 @@
 cription
- = 
+=%7B%0A%09%09%09%09
 descript
@@ -1983,19 +1983,17 @@
 ption %7C%7C
-%0A%09%09
+ 
 sprintf(
@@ -1993,16 +1993,18 @@
 printf(%0A
+%09%09
 %09%09%09/* tr
@@ -2036,16 +2036,18 @@
 e */%0A%09%09%09
+%09%09
 __( '%25s 
@@ -2111,38 +2111,52 @@
 %0A%09%09%09
+%09%09module.
 name,%0A
+%09%09
 %09%09)
-;%0A%09const moduleCTA = 
+%0A%09%09%09%7D%0A%09%09%09ctaLabel=%7B%0A%09%09%09%09
 __( 
@@ -2196,36 +2196,34 @@
 t' )
-;%0A%0A%09const moduleAriaL
+%0A%09%09%09%7D%0A%09%09%09aria-l
 abel
- = 
+=%7B%0A%09%09%09%09
 spri
@@ -2227,16 +2227,19 @@
 printf(%0A
+%09%09%09
 %09%09/* tra
@@ -2267,16 +2267,19 @@
 name */%0A
+%09%09%09
 %09%09__( 'C
@@ -2324,181 +2324,58 @@
 ,%0A%09%09
-name,%0A%09);%0A%0A%09return (%0A%09%09%3CCTA%0A%09%09%09title=%7B moduleTitle %7D%0A%09%09%09description=%7B moduleDescription %7D%0A%09%09%09onClick=%7B onCTAClick %7D%0A%09%09%09ctaLabel=%7B moduleCTA %7D%0A%09%09%09aria-label=%7B moduleAriaLabel
+%09%09%09module.name,%0A%09%09%09%09)%0A%09%09%09%7D%0A%09%09%09onClick=%7B onCTAClick
  %7D%0A%09

Update CompleteModuleActivationCTA to not assume module presence.