<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Themes;

use FiboFilters\Util\Helpers;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Kadence
 */
class Kadence extends AbstractTheme {

	public function init( string $slug = '', string $name = '', array $args = [] ): void {
		parent::init( $slug, $name, $args );

		add_action(
			'after_setup_theme',
			function () {
				$this->change_enable_integration_field_desc();
			}
		);
	}

	/**
	 * Adds extra description for the "Enable integration" option
	 */
	protected function change_enable_integration_field_desc(): void {
		add_filter(
			'fibofilters/settings/section=general',
			function ( array $settings ): array {
				// Filter location.
				/* translators: %s: name of the theme */
				$settings[200]['description'] = sprintf( _x( 'Filters can be displayed in these modes in the <b>%s</b> theme:', '[admin]', 'fibofilters' ), $this->name );

				// Filter location - Sidebar.
				$added        = false;
				$sidebar_html = '';

				$theme_sidebar_id = \Kadence\kadence()->option( 'product_archive_sidebar_id' ); // @phpstan-ignore-line

				foreach ( $this->wp_helper->get_widget_areas_containing_ff_elements() as $key => $widget_area ) {
					if ( $key === $theme_sidebar_id ) {
						$sidebar_html .= '<p class="fibofilters-settings-location-sidebar-ok">';
						$sidebar_html .= '<span class="dashicons dashicons-yes-alt"></span>';
						$sidebar_html .= '<span>';
						/* translators: %s: name of the sidebar */
						$sidebar_html .= sprintf( _x( '<b>Well done!</b> Filters have been successfully added as a WordPress widget under the widget area called “<b>%s</b>”.', '[admin]', 'fibofilters' ), $widget_area['name'] );
						$sidebar_html .= '</span>';
						$sidebar_html .= '</p>';
						$added        = true;
						break;
					}
				}

				if ( ! $added ) {
					/* translators: %s: name of the theme */
					$sidebar_html .= '<p>' . sprintf( _x( 'To display FiboFilters in the %s theme Sidebar, follow these steps:', '[admin]', 'fibofilters' ), $this->name ) . '</p>';
					$sidebar_html .= '<ol>';
					/* translators: %s: URL address to the WordPress customizer view */
					$sidebar_html .= '<li>' . sprintf( _x( 'Go to <code><a href="%s" target="_blank">Appearance -> Customize -> WooCommerce -> Product Catalog</a> -> Archive Layout</code>.', '[admin]', 'fibofilters' ), Helpers::get_customizer_link( '', 'woocommerce_product_catalog' ) ) . '</li>';
					$sidebar_html .= '<li>' . _x( 'Select the <i>Left Sidebar</i> or <i>Right Sidebar</i>.', '[admin]', 'fibofilters' ) . '</li>';
					$sidebar_html .= '<li>' . _x( 'Select the default sidebar in the <i>Product Archive Default Sidebar</i> option and publish changes.', '[admin]', 'fibofilters' ) . '</li>';
					/* translators: %s: URL address to the WordPress widgets view */
					$sidebar_html .= '<li>' . sprintf( _x( 'Go to <code><a href="%s" target="_blank">Appearance -> Widgets</a></code>.', '[admin]', 'fibofilters' ), $this->get_widgets_link() ) . '</li>';
					$sidebar_html .= '<li>' . _x( 'Add a block called <i>FiboFilters</i> to the area selected in step 3 and click Update.', '[admin]', 'fibofilters' ) . '</li>';
					$sidebar_html .= '</ol>';
				}

				$settings[260]['html'] = $sidebar_html;

				return $settings;
			},
			15
		);
	}
}
