<?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\Prodigy;

/**
 * Prodigy breadcrumbs data mapper
 *
 * @version    3.1.2
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Product_Breadcrumbs_Data_Mapper extends Prodigy_Main_Data_Mapper {

	/**
	 * @param array $options
	 *
	 * @return array
	 */
	public function get_default_parameters( array $options ): array {
		$params          = array();
		$product         = $this->get_current_product();
		$params['title'] = $this->get_product_title( $product );

		$categories       = $this->get_product_categories( $product );
		$referer_category = $this->get_referer_category( $categories );

		$params['category']         = $this->determine_category( $categories, $referer_category );
		$params['link_to_shop']     = get_permalink( prodigy_get_post_by_title( 'Shop' ) );
		$params['link_to_category'] = get_term_link( $params['category'], Prodigy::get_prodigy_category_type() );
		$params['category']         = $this->get_category_name( $params['category'] );
		$params['show_breadcrumbs'] = isset( $options['idwidget'] )
			? true
			: ( $this->customizer_product_options['prodigy_product_breadcrumbs'] ?? true );
		$params['style_curr_page']  = $options['style_curr_page'] ?? 'yes';

		return $params;
	}


	/**
	 * @return mixed|Prodigy_Product_Template_Item
	 */
	private function get_current_product() {
		$product_template_builder = new Prodigy_Product_Template_Builder( new Prodigy_Product_Template_Item() );

		return $GLOBALS['prodigy_product']
				?? $product_template_builder->set_product( (int) Prodigy_Product_Template_Builder::get_random_product_id() );
	}

	/**
	 * @param Prodigy_Product_Template_Item $product
	 *
	 * @return string
	 */
	private function get_product_title( Prodigy_Product_Template_Item $product ): string {
		return $product->get_remote_title() ?: $product->get_field( 'post_title' );
	}

	/**
	 * @param Prodigy_Product_Template_Item $product
	 *
	 * @return array
	 */
	private function get_product_categories( Prodigy_Product_Template_Item $product ): array {
		$categories_terms = get_the_terms( $product->get_field( 'ID' ), Prodigy::get_prodigy_category_type() );

		if ( ! empty( $categories_terms ) ) {
			return wp_list_pluck( $categories_terms, 'slug' );
		}

		return array();
	}

	/**
	 * @param array $categories
	 *
	 * @return array
	 */
	private function get_referer_category( array $categories ): array {
		if ( ! isset( $_SERVER['HTTP_REFERER'] ) || empty( $categories ) ) {
			return array();
		}
		$referer_path  = wp_parse_url( esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) )['path'] ?? '';
		$referer_parts = explode( '/', $referer_path );

		return array_intersect( $categories, $referer_parts );
	}

	/**
	 * @param array $categories
	 * @param array $referer_category
	 *
	 * @return string
	 */
	private function determine_category( array $categories, array $referer_category ): string {
		if ( ! empty( $referer_category ) ) {
			return array_values( $referer_category )[0];
		}

		return $categories[0] ?? '';
	}

	/**
	 * Retrieve category name based on slug.
	 *
	 * @param string $category_slug
	 *
	 * @return string
	 */
	private function get_category_name( string $category_slug ): string {
		$category_info = get_term_by( 'slug', $category_slug, Prodigy::get_prodigy_category_type() );

		return $category_info->name ?? $category_slug;
	}
}
