<?php
namespace Prodigy\Includes\Frontend\Mappers;

use Prodigy\Includes\Content\Prodigy_Main_Content;
use Prodigy\includes\helpers\Prodigy_Elementor;
use Prodigy\Includes\Prodigy_Elementor_Template_Loader;
use Prodigy\Includes\Support\Customizer\Prodigy_Customizer;

/**
 * Prodigy shop sortable
 *
 * @version    3.0.4
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Shop_Sortable_Data_Mapper extends Prodigy_Main_Data_Mapper {

	/**
	 * Get default labels for sorting options
	 *
	 * @return array
	 */
	private function get_default_sort_labels(): array {
		return array(
			'-created_at' => esc_html__( 'Date, new to old', 'prodigy-commerce' ),
			'created_at'  => esc_html__( 'Date, old to new', 'prodigy-commerce' ),
			'title'       => esc_html__( 'Title, A-Z', 'prodigy-commerce' ),
			'-title'      => esc_html__( 'Title, Z-A', 'prodigy-commerce' ),
			'-price'      => esc_html__( 'Price, high to low', 'prodigy-commerce' ),
			'price'       => esc_html__( 'Price, low to high', 'prodigy-commerce' ),
			'-rating'     => esc_html__( 'Rating, high to low', 'prodigy-commerce' ),
			'rating'      => esc_html__( 'Rating, low to high', 'prodigy-commerce' ),
		);
	}

	/**
	 * Prepare data and variables to inject in template
	 *
	 * @param array $options
	 *
	 * @return array
	 */
	public function get_default_parameters( array $options ): array {
		$search       = wp_strip_all_tags( sanitize_text_field( wp_unslash( $_GET['search'] ?? '' ) ) );
		$show_sorting = $this->customizer_shop_options['prodigy_shop_products_sortable'] ?? true;
		if ( ! Prodigy_Elementor_Template_Loader::is_include_archive_template() && ! Prodigy_Elementor_Template_Loader::is_include_shop_template() ) {
			$default_sorting = $this->customizer_shop_options['prodigy_shop_default_sortable'] ?? '-created_at';
		} else {
			$order_option = get_option( 'elementor_order_option' );
			$sort_option  = get_option( 'elementor_sort_option' );
			if ( isset( $order_option, $sort_option ) ) {
				$default_sorting = Prodigy_Main_Content::mapping_order_params( $order_option ) . $sort_option;
			} else {
				$default_sorting = $this->customizer_shop_options['prodigy_shop_default_sortable'] ?? '-created_at';
			}
		}
		$prodigy_shop_sidebar = $this->customizer_shop_options['prodigy_shop_sidebar_display'] ?? Prodigy_Customizer::PRODIGY_SHOW_SIDEBAR;
		$this->main_content->set_sort_products_query_parameter( $_GET, $options );

		$sort_param = null;
		if ( isset( $_GET['sort'] ) ) {
			$sort_param = sanitize_text_field( wp_unslash( $_GET['sort'] ) );
		}

		$sort_array = array();
		if ( ! empty( $options['prg_sorting_options'] ) && is_array( $options['prg_sorting_options'] ) ) {
			$default_labels = $this->get_default_sort_labels();
			foreach ( $options['prg_sorting_options'] as $option ) {
				if ( ! empty( $option['enabled'] ) && $option['enabled'] === 'yes' && ! empty( $option['sort_key'] ) ) {
					$sort_key                = sanitize_text_field( $option['sort_key'] );
					$label                   = ! empty( $option['label'] ) ? sanitize_text_field( $option['label'] ) : ( $default_labels[ $sort_key ] ?? $sort_key );
					$sort_array[ $sort_key ] = $label;
				}
			}
		}

		if ( empty( $sort_array ) ) {
			$sort_array = $this->get_default_sort_labels();
		}

		// Template needs sort key for selected(). When no URL param, use Product Archive default (customizer or Elementor order/sort).
		if ( $sort_param === null ) {
			$sort_param = $default_sorting ?? array_key_first( $sort_array );
			if ( $sort_param === null ) {
				$sort_param = '-created_at';
			}
		}

		$has_sidebar = $prodigy_shop_sidebar === Prodigy_Customizer::PRODIGY_SHOW_FILTER_BUTTON;

		return compact( 'show_sorting', 'sort_array', 'has_sidebar', 'prodigy_shop_sidebar', 'sort_param', 'search' );
	}
}
