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

use Prodigy\Includes\Frontend\Prodigy_Product_Template_Builder;
use Prodigy\Includes\Frontend\Prodigy_Product_Template_Item;
use Prodigy\includes\helpers\Prodigy_Elementor;
use Prodigy\includes\helpers\Prodigy_Elementor_Layouts;
use Prodigy\Includes\Helpers\Prodigy_Widget_Options;
use Prodigy\Includes\Prodigy_Elementor_Template_Loader;
use Prodigy\Includes\Support\Addons\Elementor\Widgets\Product\Prodigy_Add_To_Cart;

/**
 * Prodigy product variants data mapper
 *
 * @version    3.0.4
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Product_Variants_Data_Mapper extends Prodigy_Main_Data_Mapper {

	/**
	 * Prepare data and variables to inject in template
	 *
	 * @param array $options
	 *
	 * @return array
	 */
	public function get_default_parameters( array $options ): array {
		$product_template                = new Prodigy_Product_Template_Builder( new Prodigy_Product_Template_Item() );
		$product                         = $GLOBALS['prodigy_product'] ?? $product_template->set_product( (int) Prodigy_Product_Template_Builder::get_random_product_id() );
		$prepared_variants               = ! empty( $product ) ? $product->get_prepared_variants() : array();
		$product_options                 = ! empty( $product ) ? $product->get_variant_options() : array();
		$logos                           = ! empty( $product ) ? $product->get_logos() : array();
		$logo_locations                  = ! empty( $product ) ? $product->get_logo_locations() : array();
		$is_enable_logo                  = ! isset( $options['product_logo_tool_enable_option'] ) || Prodigy_Widget_Options::$bool_mapper[ $options['product_logo_tool_enable_option'] ];
		$is_enable_logo_swatches         = ! isset( $options['product_logo_tool_enable_swatches'] ) || Prodigy_Widget_Options::$bool_mapper[ $options['product_logo_tool_enable_swatches'] ];
		$is_logo_tool_multiple_locations = ! isset( $options['product_logo_tool_multiple_locations'] ) || Prodigy_Widget_Options::$bool_mapper[ $options['product_logo_tool_multiple_locations'] ];
		$master_logos                    = $product->get_master_variant_logos();
		$is_required_locations           = $this->is_required_locations( $logo_locations );
		$show_bulk                       = ! empty( $this->customizer_shop_options['prodigy_product_control_bulk'] );
		$variants                        = array();
		$tiered_prices                   = $product->get_tiered_prices();
		$variant_options_intersect       = $product->get_option_variants_intersect() ?? array();
		if ( ! empty( $product ) && ! empty( $product->get_remote_variants() ) ) {
			$variants = $product->get_variant_options();
		}
		if ( Prodigy_Elementor_Layouts::is_elementor_template() || Prodigy_Elementor_Layouts::is_elementor_live_preview() ) {
			$show_bulk = Prodigy_Widget_Options::$bool_mapper[ $options['general_allow_multiple_quantity'] ];
		}

		$settings = array(
			'variant_options_intersect'       => $variant_options_intersect,
			'prepared_variants'               => $prepared_variants,
			'product_options'                 => $product_options,
			'logos'                           => $logos,
			'logo_locations'                  => $logo_locations,
			'options'                         => $this->get_attributes_list( $variants ),
			'product'                         => $product,
			'variants'                        => $variants,
			'show_bulk'                       => $show_bulk,
			'settings'                        => $options,
			'tiered_min_quantity_text'        => $tiered_prices[0]['attributes']['min-quantity'] ?? '',
			'is_tiered_prices'                => ! empty( $tiered_prices ),
			'is_enable_logo'                  => $is_enable_logo,
			'is_enable_logo_swatches'         => $is_enable_logo_swatches,
			'master_logos'                    => $master_logos,
			'is_logo_tool_multiple_locations' => $is_logo_tool_multiple_locations,
			'is_required_locations'           => $is_required_locations,
		);

		if ( ! empty( $variants ) && Prodigy_Elementor::is_elementor_enabled() ) {
			$settings['is_show_certain_bulk_block'] = Prodigy_Widget_Options::$bool_mapper[ $options['default_enable_multiple_quantity'] ?? 'false' ];
			$settings['number_certain_bulk_block']  = $options['default_enable_multiple_quantity_options'] ?? '';
		}

		return $settings;
	}

	/**
	 *
	 * @param array $variants
	 *
	 * @return array
	 */
	private function get_attributes_list( array $variants ): array {
		$parameters = array();
		$i          = 0;
		foreach ( $variants as $key => $option ) {
			$parameters[ $i++ ] = $key;
		}

		return $parameters;
	}

	/**
	 * @param  array $locations
	 *
	 * @return bool
	 */
	private function is_required_locations( array $locations ): bool {
		$is_required_locations = false;
		foreach ( $locations as $value ) {
			if ( isset( $value['attributes']['required'] ) && $value['attributes']['required'] ) {
				$is_required_locations = true;
				break;
			}
		}

		return $is_required_locations;
	}
}
