<?php
/**
 * Tabs widget.
 *
 * @package Rsmspark_Content_Widgets
 */

/*
Widget Name: RSM Tabs
Description: Tabbed content panels.
Author: RSMSpark
Author URI: https://rsmspark.com/
*/

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'SiteOrigin_Widget', false ) ) {
	return;
}

/**
 * Rsmspark_Content_Widgets_Tabs_Widget
 */
class Rsmspark_Content_Widgets_Tabs_Widget extends SiteOrigin_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'tabs',
			__( 'RSM Tabs', 'rsm-content-widgets' ),
			array(
				'description' => __( 'Switch between content panels with tabs.', 'rsm-content-widgets' ),
				'panels_icon' => 'dashicons dashicons-index-card',
				'has_preview' => true,
				'keywords'    => array( 'tabs', 'panels', 'RSM' ),
			),
			array(),
			array(),
			plugin_dir_path( __FILE__ )
		);
	}

	/**
	 * @return array<string, mixed>
	 */
	public function get_widget_form() {
		return Rsmspark_Content_Widgets_SiteOrigin::prepend_import_export_form(
			'tabs',
			array(
			'content' => array(
				'type'   => 'section',
				'label'  => __( 'Tabs', 'rsm-content-widgets' ),
				'fields' => array(
					'items' => array(
						'type'      => 'repeater',
						'label'     => __( 'Tab items', 'rsm-content-widgets' ),
						'item_name' => __( 'Tab', 'rsm-content-widgets' ),
						'fields'    => array(
							'title' => array(
								'type'    => 'text',
								'label'   => __( 'Tab title', 'rsm-content-widgets' ),
								'default' => __( 'Tab', 'rsm-content-widgets' ),
							),
							'content' => array_merge(
								Rsmspark_Rich_Text_Registry::siteorigin_tinymce_field( Rsmspark_Rich_Text_Registry::PROFILE_BODY, 12 ),
								array(
									'label'          => __( 'Tab content', 'rsm-content-widgets' ),
									'default'        => __( 'Tab content goes here.', 'rsm-content-widgets' ),
									'rsm_rt_profile' => Rsmspark_Rich_Text_Registry::PROFILE_BODY,
								)
							),
						),
						'default'   => array(
							array(
								'title'   => __( 'First tab', 'rsm-content-widgets' ),
								'content' => __( 'Content for the first tab.', 'rsm-content-widgets' ),
							),
							array(
								'title'   => __( 'Second tab', 'rsm-content-widgets' ),
								'content' => __( 'Content for the second tab.', 'rsm-content-widgets' ),
							),
						),
					),
				),
			),
		)
		);
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_dir( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_style_name( $instance ) {
		unset( $instance );
		return '';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @return string
	 */
	public function get_template_name( $instance ) {
		unset( $instance );
		return 'tabs.tpl';
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 * @param array<string, mixed> $args     Args.
	 * @return array<string, mixed>
	 */
	public function get_template_variables( $instance, $args ) {
		unset( $args );
		$vars = Rsmspark_Content_Widgets_Widget_Tabs::prepare_from_instance(
			Rsmspark_Content_Widgets_Loader::section_instance( $instance, 'content' )
		);
		$vars['style_vars'] = Rsmspark_Content_Widgets_Loader::inline_style_vars();
		return $vars;
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_admin_scripts() {
		if ( class_exists( 'Rsmspark_Rich_Text_Assets', false ) ) {
			Rsmspark_Rich_Text_Assets::enqueue_admin();
		}
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}

	/**
	 * @param array<string, mixed> $instance Instance.
	 */
	public function enqueue_frontend_scripts( $instance ) {
		unset( $instance );
		if ( class_exists( 'Rsmspark_Rich_Text_Assets', false ) ) {
			Rsmspark_Rich_Text_Assets::enqueue_siteorigin_frontend();
		}
		Rsmspark_Content_Widgets_Loader::enqueue_shared_assets( $this );
	}
}

if ( function_exists( 'siteorigin_widget_register' ) ) {
	siteorigin_widget_register( 'tabs', __FILE__, 'Rsmspark_Content_Widgets_Tabs_Widget' );
}
