<?php

namespace HulkPlugins\Woo\ProductConditionManager\Utils;

defined( 'ABSPATH' ) || exit;

class Helper {

	private static array $cache = [];

	/**
	 * Get default terms with parent
	 * @return array
	 */
	public static function getDefaultConditions(): array {

		$newCondition = [
			'brand-new'        => 'Brand new',
			'new'              => 'New',
			'new-with-box'     => 'New with box',
			'new-with-defects' => 'New with defects',
			'new-with-tags'    => 'New with tags',
			'new-without-box'  => 'New without box',
			'new-without-tags' => 'New without tags',
		];

		$usedCondition = [
			'acceptable'          => 'Acceptable',
			'certified-pre-owned' => 'Certified pre-owned',
			'good'                => 'Good',
			'like-new'            => 'Like New',
			'pre-owned'           => 'Pre-owned',
			'used'                => 'Used',
			'very-good'           => 'Very good',
		];

		$refurbishedCondition = [
			'manufacturer-refurbished' => 'Manufacturer refurbished',
			'remanufactured'           => 'Remanufactured',
			'retread'                  => 'Retread',
			'seller-refurbished'       => 'Seller refurbished',
		];

		$damagedCondition = [
			'damaged'   => 'Damaged',
			'for-parts' => 'For parts',
		];

		/**
		 * Is expected to be one of the four OfferItemCondition values
		 * @link https://schema.org/OfferItemCondition
		 */
		$conditions = [
			'NewCondition'         => $newCondition,
			'UsedCondition'        => $usedCondition,
			'RefurbishedCondition' => $refurbishedCondition,
			'DamagedCondition'     => $damagedCondition,
		];

		return apply_filters( 'hulk_woo_pcm_conditions', $conditions );
	}

	/**
	 * Get condition with group by key
	 * @param string $key
	 *
	 * @return array
	 */
	public static function getConditionByKey( string $key ): array {
		$prefix = 'conditionByKey_' . $key;

		if ( ! isset( self::$cache[ $prefix ] ) ) {

			$data = [
				'group'     => '',
				'condition' => '',
			];

			foreach ( self::getDefaultConditions() as $group => $conditions ) {
				foreach ( $conditions as $conditionKey => $condition ) {
					if ( $conditionKey !== $key ) {
						continue;
					}
					$data = [
						'group'     => $group,
						'condition' => $condition,
					];
				}
			}

			self::$cache[ $prefix ] = $data;
		}

		return (array) self::$cache[ $prefix ];
	}
}
