<?php
declare( strict_types=1 );

namespace FiboFilters\Filters\Types;

use FiboFilters\Util\FilterUtil;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Toggle
 */
class Toggle extends Filter {

	/**
	 * This is the name of this filter type.
	 *
	 * @var string
	 */
	protected $filter_type = 'toggle';

	/**
	 * Allow ony "yes" values in this type of filter
	 *
	 * @param array $values Filter values.
	 */
	protected function filter_values( array $values ): array {
		if ( count( $values ) <= 1 ) {
			return $values;
		}

		$source = $this->get_prop( 'source' );

		if ( strpos( $source, 'cf_' ) !== false ) {
			$values = array_filter(
				$values,
				function ( array $value ): bool {
					return FilterUtil::is_filter_true_value( $value['value'] );
				}
			);
		} elseif ( strpos( $source, 'tax_' ) !== false ) {
			$values = array_filter(
				$values,
				function ( array $value ): bool {
					return FilterUtil::is_filter_true_value( $value['label'] );
				}
			);
		}

		return $values;
	}

	protected function after_set_values(): void {
		$this->maybe_overwrite_value_labels();
	}
}
