<?php
namespace Prodigy\Includes\Content;

use Prodigy\Includes\Models\Prodigy_Attributes;
use Prodigy\Includes\Models\Prodigy_Categories;

/**
 * Prodigy content catalog filters
 *
 * @version    2.8.9
 */
class Prodigy_Catalog_Filters_Parser extends Prodigy_Main_Content {

	/**
	 * Types of content data in catalog-filters query response
	 */
	const CATEGORIES_TYPE_CONTENT       = 'categories';
	const ATTRIBUTES_TYPE_CONTENT       = 'properties';
	const ATTRIBUTES_VALUE_TYPE_CONTENT = 'options';

	/**
	 * @var Prodigy_Attributes
	 */
	public Prodigy_Attributes $attributes;

	/**
	 * @var Prodigy_Categories
	 */
	public Prodigy_Categories $categories;

	/**
	 * @param array $content
	 *
	 * @return array
	 */
	public static function get_categories( array $content ): array {
		$categories = array();
		if ( ! empty( $content['included'] ) && is_array( $content['included'] ) ) {
			foreach ( $content['included'] as $item ) {
				if ( is_array( $item ) && isset( $item['type'] ) && $item['type'] === self::CATEGORIES_TYPE_CONTENT && isset( $item['id'] ) ) {
					$categories[ $item['id'] ] = $item;
				}
			}
		}

		return $categories;
	}


	/**
	 * @param array $content
	 *
	 * @return array
	 */
	public static function get_attributes( array $content ): array {
		$attributes = array();
		if ( ! empty( $content['included'] ) && is_array( $content['included'] ) ) {
			foreach ( $content['included'] as $key => $item ) {
				if ( is_array( $item ) && isset( $item['type'] ) && $item['type'] === self::ATTRIBUTES_TYPE_CONTENT ) {
					$attributes[ $key ] = $item;
				}
			}
		}

		$options = array();
		if ( ! empty( $content['included'] ) && is_array( $content['included'] ) ) {
			foreach ( $content['included'] as $key => $item ) {
				if ( is_array( $item ) && isset( $item['type'] ) && $item['type'] === self::ATTRIBUTES_VALUE_TYPE_CONTENT ) {
					$options[ $key ] = $item;
				}
			}
		}

		$new_attributes = array();
		if ( ! empty( $attributes ) ) {
			foreach ( $attributes as $key => $attribute ) {
				if ( ! is_array( $attribute ) || ! isset( $attribute['attributes']['name'] ) || ! isset( $attribute['id'] ) ) {
					continue;
				}

				$new_attributes[ $key ]['name'] = $attribute['attributes']['name'];
				$new_attributes[ $key ]['id']   = $attribute['id'];

				if ( ! empty( $attribute['relationships']['options']['data'] ) && is_array( $attribute['relationships']['options']['data'] ) ) {
					foreach ( $attribute['relationships']['options']['data'] as $key_value => $value ) {
						foreach ( $options as $option ) {
							if ( is_array( $option ) && isset( $value['id'] ) && isset( $option['id'] ) && $value['id'] === $option['id'] && isset( $option['attributes'] ) ) {
								$option_attributes                                      = (array) $option['attributes'];
								$new_attributes[ $key ]['values'][ $key_value ]['id']   = $option['id'];
								$new_attributes[ $key ]['values'][ $key_value ]['name'] = $option_attributes['value'] ?? '';

								if ( isset( $attribute['attributes']['slug'] ) && $attribute['attributes']['slug'] && isset( $option_attributes['slug'] ) ) {
									$new_attributes[ $key ]['values'][ $key_value ]['slug'] = $option_attributes['slug'];
								}

								$new_attributes[ $key ]['values'][ $key_value ]['selected'] = $option_attributes['selected'] ?? false;
								$new_attributes[ $key ]['values'][ $key_value ]['count']    = $option_attributes['products-count'] ?? 0;
							}
						}
					}
				}
			}
		}

		return $new_attributes;
	}

	/**
	 * @param array $content
	 *
	 * @return array
	 */
	public static function get_catalog_info( array $content ): array {
		return $content['data'] ?? array();
	}
}
