<?php // phpcs:ignore

namespace SEOPress\Services\Metas\Description\Specifications;

/**
 * TaxonomySpecification
 */
class TaxonomySpecification {


	/**
	 * The name service.
	 *
	 * @var string
	 */
	const NAME_SERVICE = 'TaxonomyDescriptionSpecification';

	/**
	 * The getValue function.
	 *
	 * @param array $params The params.
	 *
	 * @example [
	 *     'context' => array
	 *
	 * ]
	 * @return string
	 */
	public function getValue( $params ) { // phpcs:ignore -- TODO: check if method is outside this class before renaming.
		$context = $params['context'];

		$post_type = isset( $context['post'] ) ? $context['post']->post_type : null;
		$term      = isset( $context['term'] ) ? $context['term'] : null;

		if ( $term && get_term_meta( $term->term_id, '_seopress_titles_desc', true ) && ! empty( $params['description'] ) ) {
			$value = $params['description'];
		} else {
			$value = seopress_get_service( 'TitleOption' )->getTaxonomyCptDescription( $post_type );
		}

		if ( empty( $value ) || ! $value ) {
			return '';
		}

		return seopress_get_service( 'TagsToString' )->replace( $value, $context );
	}



	/**
	 * The isSatisfyBy function.
	 *
	 * @param array $params The params.
	 *
	 * @example [
	 *     'post' => \WP_Post
	 *     'description' => string
	 *     'context' => array
	 *
	 * ]
	 * @return boolean
	 */
	public function isSatisfyBy( $params ) { // phpcs:ignore -- TODO: check if method is outside this class before renaming.
		$context = $params['context'];

		if ( ( $context['is_tax'] || $context['is_category'] || $context['is_tag'] ) && ! $context['is_search'] ) {
			if ( empty( $this->getValue( $params ) ) ) {
				return false;
			}

			return true;
		}

		return false;
	}
}
