<?php

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

// SEO API
/**
 * @param string $key title|description|h1|text
 * @return string|false Needed seo data
 */
function sfs_get_seo_data($key = 'title' ){
    if( ! did_action('wp') ){
        _doing_it_wrong(
            'sfs_get_seo_data',
            sprintf(
            /* translators: %s: rest_api_init */
                esc_html__( 'Please, do not call the "%s" function before the "wp" action', 'searchfiltersort' ),
                'sfs_get_seo_data'
            ),
            '4.6'
        );
        return false;
    }

    $possibleValues = array(
        'title'       => 'seoTitle',
        'description' => 'metaDescription',
        'h1'          => 'h1',
        'text'        => 'seoDescription',
    );

    if( in_array( $key, array_keys($possibleValues), true ) ){
        $seoFrontend = \SearchFilterSort\Filter\Container::instance()->getSeoFrontendService();
        return $seoFrontend->get( $possibleValues[$key] );
    }

    return false;
}

// Posts found quantity
/**
 * @return int|null number of posts found
 */
function sfs_posts_found_quantity( $setid, $all = false )
{
    $wpManager = \SearchFilterSort\Filter\Container::instance()->getWpManager();
    $em        = \SearchFilterSort\Filter\Container::instance()->getEntityManager();

    $count = NULL;

    if( $wpManager->getQueryVar('sfswp_is_filter_request' ) ){

        $set_filter_keys = array_flip( $em->getSetFilterKeys( array( $setid ) ) );
        $queried_keys = [];

        foreach ( $wpManager->getQueryVar('queried_values') as $queried_value ){
            $key = $queried_value['entity'].'#'.$queried_value['e_name'];
            $queried_keys[ $key ] = true;
        }

        if( ! empty( array_intersect_key( $queried_keys, $set_filter_keys ) ) ){
            $count = count( $em->getAlreadyFilteredPostIds( $setid ) );
        }else if( $all ) {
            $count = count( $em->getAlreadyFilteredPostIds( $setid ) );
        }

    } else if ( $all ){
        $count = count( $em->getAlreadyFilteredPostIds( $setid ) );
    }

    return $count;
}

// Selected filter chips
/**
 * @param bool $include_reset - include Reset button term or not
 * @return array|false - selected terms
 */
function sfs_selected_filter_chips($include_reset = true )
{
    $chipsObj = new SearchFilterSort\Filter\Chips($include_reset);
    return $chipsObj->getChips();
}

// Selected filter terms
/**
 * @return array|false all selected filters or false
 */
function sfs_selected_filter_terms()
{
    $wpManager = \SearchFilterSort\Filter\Container::instance()->getWpManager();
    return $wpManager->getQueryVar('queried_values');
}

// Get all filters related with current page
/**
 * @return array of all filters from the SFS Set
 * related with current page
 * or empty array if there are no filters
 */
function sfs_get_page_related_filters(){
    /**
     * @todo add check to hook if
     */
    if( ! did_action( 'pre_get_posts' ) ) {
        _doing_it_wrong( __FUNCTION__, esc_html__('Please, do not fire this function, until the "pre_get_posts" hook has been called', 'searchfiltersort'), '1.7.16' );
    } else{
        $em = \SearchFilterSort\Filter\Container::instance()->getEntityManager();
        return $em->getSetsRelatedFilters();
    }
}