<?php
/**
 * Option-backed Amazon discount priority-brand settings.
 *
 * @package Affilun\Infrastructure\Persistence
 */

declare(strict_types=1);

namespace Affilun\Infrastructure\Persistence;

use Affilun\Application\AmazonDiscount\AmazonDiscountBrand;
use Affilun\Application\AmazonDiscount\AmazonDiscountBrandSettingsRepository;

/**
 * Stores priority brands outside the general plugin settings payload.
 */
final class OptionStoreAmazonDiscountBrandSettingsRepository implements AmazonDiscountBrandSettingsRepository {

	public const OPTION_KEY = 'affilun_amazon_discount_priority_brands';

	/**
	 * Create the option-backed repository.
	 *
	 * @param OptionStore $options Plugin option store.
	 */
	public function __construct( private OptionStore $options ) {
	}

	/**
	 * Return configured brands.
	 *
	 * @return list<string>
	 */
	#[\Override]
	public function load(): array {
		$value = $this->options->get( self::OPTION_KEY, array() );
		return is_array( $value ) ? AmazonDiscountBrand::normalize( array_values( $value ) ) : array();
	}

	/**
	 * Save configured brands without autoloading.
	 *
	 * @param list<mixed> $brands Raw brand values.
	 */
	#[\Override]
	public function save( array $brands ): void {
		$this->options->updateNonAutoload(
			self::OPTION_KEY,
			AmazonDiscountBrand::normalize( $brands )
		);
	}
}
