<?php
/**
 * GravityView View Details Module for Beaver Builder
 *
 * @package GravityKit\GravityView\PageBuilder\BeaverBuilder
 * @since 3.0.0
 */

namespace GravityKit\GravityView\PageBuilder\BeaverBuilder;

use FLBuilderModule;
use FLBuilder;

/** If this file is called directly, abort. */
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
	die();
}

/**
 * GravityView View Details Module for Beaver Builder.
 *
 * Displays View metadata (total entries, page info, etc.).
 *
 * @since 3.0.0
 */
class ViewDetailsModule extends FLBuilderModule {

	use BeaverBuilderModuleTrait;

	/** @inheritDoc */
	protected static function get_block_type() {
		return 'view-details';
	}

	/**
	 * Module constructor.
	 *
	 * @since 3.0.0
	 */
	public function __construct() {
		$metadata = self::get_builder_integration()->get_block_types_metadata()['view-details'] ?? [];

		parent::__construct(
			[
				// translators: "GV" is short for "GravityView". Keep it short to avoid truncation in Beaver Builder.
				'name'            => esc_html__( 'GV View Details', 'gk-gravityview' ),
				'description'     => $metadata['description'] ?? '',
				'category'        => esc_html__( 'Basic', 'gk-gravityview' ),
				'group'           => esc_html__( 'GravityKit', 'gk-gravityview' ),
				'dir'             => __DIR__,
				'url'             => plugins_url( '', __FILE__ ),
				'editor_export'   => true,
				'enabled'         => true,
				'partial_refresh' => true,
			]
		);
	}

	/**
	 * Get module icon SVG.
	 *
	 * @since 3.0.0
	 *
	 * @param string $icon Icon name (unused).
	 *
	 * @return string SVG icon markup.
	 */
	public function get_icon( $icon = '' ) {
		return self::get_builder_integration()->get_block_type_icon_svg( 'view-details' );
	}

	/** @inheritDoc */
	public function frontend() {
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in render_module/render_placeholder.
		echo $this->render_module();
	}
}

/**
 * Register the module with Beaver Builder.
 */
$integration = ViewDetailsModule::get_builder_integration();
$fields_meta = $integration->get_block_type_field_metadata( 'view-details' );

FLBuilder::register_module(
	__NAMESPACE__ . '\ViewDetailsModule',
	[
		'general' => [
			'title'    => esc_html__( 'General', 'gk-gravityview' ),
			'sections' => [
				'view_selection' => [
					'title'  => esc_html__( 'View Selection', 'gk-gravityview' ),
					'fields' => [
						'viewId' => [
							'type'    => 'select',
							'label'   => $fields_meta['viewId']['label'] ?? '',
							'default' => '',
							'options' => $integration->get_views_list(),
							'preview' => [ 'type' => 'refresh' ],
						],
						'detail' => [
							'type'    => 'select',
							'label'   => $fields_meta['detail']['label'] ?? '',
							'default' => $fields_meta['detail']['default'] ?? '',
							'options' => $fields_meta['detail']['options'] ?? [],
							'preview' => [ 'type' => 'refresh' ],
						],
					],
				],
			],
		],
	]
);
