<?php
/**
 * Amazon discount category definitions.
 *
 * @package Affilun\Application\AmazonDiscount
 */

declare(strict_types=1);

namespace Affilun\Application\AmazonDiscount;

/**
 * Closed set of physical-product categories used by the daily sync.
 */
final class AmazonDiscountCategory {

	/**
	 * SearchIndex to Japanese label map.
	 *
	 * @var array<string,string>
	 */
	public const LABELS = array(
		'Books'                 => '本',
		'Electronics'           => '家電・カメラ',
		'Computers'             => 'パソコン・周辺機器',
		'HomeAndKitchen'        => 'ホーム・キッチン',
		'Fashion'               => 'ファッション',
		'Beauty'                => 'ビューティー',
		'HealthPersonalCare'    => 'ヘルスケア',
		'SportsAndOutdoors'     => 'スポーツ・アウトドア',
		'Hobbies'               => 'ホビー',
		'Toys'                  => 'おもちゃ',
		'Baby'                  => 'ベビー',
		'GroceryAndGourmetFood' => '食品・飲料',
		'PetSupplies'           => 'ペット用品',
	);

	/**
	 * Default categories selected in the shortcode configurator.
	 *
	 * @var list<string>
	 */
	public const DEFAULT_SELECTION = array(
		'Electronics',
		'HomeAndKitchen',
		'Fashion',
	);

	/**
	 * Return all SearchIndex values.
	 *
	 * @return list<string>
	 */
	public static function all(): array {
		return array_keys( self::LABELS );
	}

	/**
	 * Validate and deduplicate category tokens.
	 *
	 * @param list<mixed> $values Raw values.
	 *
	 * @return list<string>
	 */
	public static function normalize( array $values ): array {
		$normalized = array();
		foreach ( $values as $value ) {
			if ( ! is_string( $value ) || ! array_key_exists( $value, self::LABELS ) ) {
				continue;
			}
			if ( ! in_array( $value, $normalized, true ) ) {
				$normalized[] = $value;
			}
		}
		return $normalized;
	}

	/**
	 * Resolve a Japanese category label.
	 *
	 * @param string $searchIndex Amazon SearchIndex.
	 */
	public static function label( string $searchIndex ): string {
		return self::LABELS[ $searchIndex ] ?? $searchIndex;
	}
}
