<?php

namespace DynamicContentForElementor\Integrations;

use DynamicContentForElementor\Helper;
if (!\defined('ABSPATH')) {
    exit;
    // Exit if accessed directly.
}
class SearchAndFilterPro
{
    const QUERY_INTEGRATION_NAME = 'dynamicooo/dynamic-content-for-elementor';
    public function __construct()
    {
        add_action('search-filter/settings/init', [$this, 'add_integration_v3'], 10);
        add_filter('search-filter/queries/query/get_attributes', [$this, 'add_query_attributes_v3'], 10, 2);
        add_action('search-filter/settings/init', [$this, 'hide_css_selector_options'], 10);
    }
    /**
     * @return void
     */
    public function add_integration_v3()
    {
        if (!Helper::is_search_filter_pro_version(3.1)) {
            return;
        }
        $integration_type_setting = \Search_Filter\Queries\Settings::get_setting('queryIntegration', 'query');
        if (!$integration_type_setting) {
            return;
        }
        $integration_type_setting->add_option(['label' => DCE_PRODUCT_NAME_LONG, 'value' => self::QUERY_INTEGRATION_NAME]);
    }
    /**
     * Automatically set the Ajax CSS selectors for the query container and pagination
     * so the user doesn't have to set it themselves.
     *
     * @param array<string,string> $attributes
     * @param \Search_Filter\Queries\Query $query
     * @return array<string,string>
     */
    public function add_query_attributes_v3($attributes, $query)
    {
        // We want `queryContainer` and `paginationSelector` to be set automatically.
        $id = $query->get_id();
        if (!isset($attributes['queryIntegration'])) {
            return $attributes;
        }
        $query_integration = $attributes['queryIntegration'];
        $integration_type = $attributes['integrationType'];
        if (self::QUERY_INTEGRATION_NAME === $query_integration) {
            // The query container is a CSS selector that contains the results and pagination.
            $attributes['queryContainer'] = '.search-filter-dynamic-posts-results-' . $query->get_id();
            $dynamic_sections = '.search-filter-dynamic-google-maps-results-' . $query->get_id();
            if (!empty($attributes['dynamicSections'])) {
                $attributes['dynamicSections'] .= ', ' . $dynamic_sections;
            } else {
                $attributes['dynamicSections'] = $dynamic_sections;
            }
            // We use the posts container for when we use the "load more" pagination type
            $attributes['queryPostsContainer'] = '.search-filter-dynamic-posts-results-' . $query->get_id() . ' .dce-posts-wrapper';
            // The pagination selector can be a full select - also changed the attribute from `paginationSelector` -> `queryPaginationSelector`
            $attributes['queryPaginationSelector'] = '.search-filter-dynamic-posts-results-' . $query->get_id() . ' .dce-pagination a';
        }
        return $attributes;
    }
    /**
     * Hide CSS selector options from query editor.
     *
     * @return void
     */
    public function hide_css_selector_options()
    {
        $depends_conditions = array('relation' => 'AND', 'action' => 'hide', 'rules' => [['option' => 'queryIntegration', 'compare' => '!=', 'value' => self::QUERY_INTEGRATION_NAME]]);
        $query_container = \Search_Filter\Queries\Settings::get_setting('queryContainer');
        if ($query_container) {
            $query_container->add_depends_condition($depends_conditions);
        }
        $query_posts_container = \Search_Filter\Queries\Settings::get_setting('queryPostsContainer');
        if ($query_posts_container) {
            $query_posts_container->add_depends_condition($depends_conditions);
        }
        $pagination_selector = \Search_Filter\Queries\Settings::get_setting('queryPaginationSelector');
        if ($pagination_selector) {
            $pagination_selector->add_depends_condition($depends_conditions);
        }
    }
    /**
     * @param \Elementor\Widget_Base $context
     * @param array<string,string> $overrides
     * @return void
     */
    public function maybe_add_search_filter_class($context, array $overrides = [])
    {
        $default_config = ['query_type' => 'query_type', 'id_setting_key_v2' => 'search_filter_id', 'id_setting_key_v3' => 'search_filter_v3_id', 'class_prefix_v2' => 'search-filter-results-', 'class_prefix_v3' => ''];
        $config = \array_merge($default_config, $overrides);
        if ('search_filter' !== $context->get_settings($config['query_type'])) {
            return;
        }
        $search_filter_id = null;
        $element_class = '';
        if (Helper::is_search_filter_pro_version(2) && \version_compare(SEARCH_FILTER_VERSION, '2.5.5', '>=')) {
            $search_filter_id = $context->get_settings_for_display($config['id_setting_key_v2']);
            $search_filter_id = apply_filters('wpml_object_id', $search_filter_id, 'search-filter-widget', \true);
            $element_class = $config['class_prefix_v2'];
        } elseif (Helper::is_search_filter_pro_version(3.1)) {
            $search_filter_id = $context->get_settings_for_display($config['id_setting_key_v3']);
            $element_class = $config['class_prefix_v3'];
        }
        if (!$search_filter_id) {
            return;
        }
        $context->add_render_attribute('_wrapper', ['class' => [$element_class . \intval($search_filter_id)]]);
    }
}
