<?php
/**
 * Location settings dialog: Display Options tab — Divi Builder module row.
 *
 * @package megamenu
 */

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

/**
 * Prepend the Divi module display option when Divi integration is active.
 *
 * @param array  $settings         Location settings schema from core.
 * @param string $location         Menu location slug.
 * @param array  $plugin_settings  Full megamenu_settings option.
 * @return array
 */
function megamenu_divi_filter_location_settings_display_module_option( $settings, $location, $plugin_settings ) {
	if ( ! apply_filters( 'megamenu_divi_location_settings_display_module_option_enabled', true, $location, $settings, $plugin_settings ) ) {
		return $settings;
	}

	if ( empty( $settings['output_options']['settings'] ) || ! is_array( $settings['output_options']['settings'] ) ) {
		return $settings;
	}

	$default_group = [
		'priority'    => 5,
		'title'       => __( 'Divi', 'megamenu' ),
		'description' => __( 'Display this location using the Divi Builder.', 'megamenu' ),
		'settings'    => [
			[
				'type'  => 'megamenu_divi_location_display_option',
				'key'   => 'megamenu_divi_location_display_option',
				'value' => $location,
			],
		],
	];

	/**
	 * Filter the whole "Divi" display-options group (title, description, nested settings rows).
	 *
	 * Return an empty array to skip registering this option for a location.
	 *
	 * @param array  $group             Group definition (priority, title, description, settings).
	 * @param string $location          Menu location slug.
	 * @param array  $plugin_settings   Full megamenu_settings option.
	 * @param array  $settings          Full location settings schema after core assembly.
	 */
	$group = apply_filters( 'megamenu_divi_location_settings_display_module_option', $default_group, $location, $plugin_settings, $settings );

	if ( ! is_array( $group ) || [] === $group ) {
		return $settings;
	}

	$settings['output_options']['settings'] = array_merge(
		[ 'megamenu_divi_module' => $group ],
		$settings['output_options']['settings']
	);

	return $settings;
}

add_filter( 'megamenu_location_settings', 'megamenu_divi_filter_location_settings_display_module_option', 10, 3 );

/**
 * Instructional copy in the value column (same pattern as Block / Widget rows; not a code textarea).
 *
 * @param string $key      Setting key.
 * @param string $location Menu location slug.
 * @param array  $setting  Full setting definition.
 */
function megamenu_divi_action_print_location_display_option( $key, $location, $setting = [] ) {
	$loc = isset( $setting['value'] ) ? (string) $setting['value'] : (string) $location;

	/**
	 * Text shown for the Divi display option.
	 *
	 * @param string $text     Default instructional text.
	 * @param string $loc      Location slug from the setting row.
	 * @param string $location Dialog location slug (same as $loc when generated by this integration).
	 * @param array  $setting  Full setting definition.
	 */
	$text = apply_filters(
		'megamenu_divi_location_display_option_text',
		__( "Add the 'Max Mega Menu' module to your page layout using the Divi Builder.", 'megamenu' ),
		$loc,
		$location,
		$setting
	);

	$class = 'mmm-location-output-instruction mega-setting-megamenu_divi_location_display_option';
	/**
	 * Filter the root element `class` attribute for the Divi display option (paragraph, not textarea).
	 *
	 * @param string $class    CSS class list.
	 * @param string $location Menu location slug.
	 * @param array  $setting  Full setting definition.
	 */
	$class = apply_filters( 'megamenu_divi_location_display_option_textarea_class', $class, $location, $setting );

	echo '<p class="' . esc_attr( $class ) . '">' . esc_html( $text ) . '</p>';
}

add_action( 'megamenu_print_location_option_megamenu_divi_location_display_option', 'megamenu_divi_action_print_location_display_option', 10, 3 );
