<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Loader\WPQuery;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Entity\Export;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Helper\PluginHelper;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Conditional\Conditional;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Hookable\Hookable;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Config\Config;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Request\Request;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DAO\ProductDAO;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\TitleField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\ContainsCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\NotContainsCondition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\Abstraction\AllowedConditions;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Fields\Abstraction\WPPostsField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\Component\ConditionalLogicComponent;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\Abstraction\Condition;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\Conditions\Abstraction\ConditionOperator;
/**
 *
 * @package WPDesk\Library\DropshippingExport\Action\Loader\WPQuery
 */
class WPQueryLoaderAction implements Hookable, Conditional
{
    /**
     * @var Config
     */
    private $config;
    /**
     * @var Request
     */
    private $request;
    /**
     * @var PluginHelper
     */
    private $plugin_helper;
    public function __construct(Config $config, Request $request, PluginHelper $helper)
    {
        $this->config = $config;
        $this->request = $request;
        $this->plugin_helper = $helper;
    }
    public function isActive(): bool
    {
        return \true;
    }
    public function hooks()
    {
        add_filter('posts_where', [$this, 'register_custom_post_queries'], 10, 2);
    }
    public function register_custom_post_queries($where, $wp_query)
    {
        global $wpdb;
        $fields = ConditionalLogicComponent::get_product_fields();
        foreach ($fields as $field) {
            if (\is_subclass_of($field, WPPostsField::class) && \is_subclass_of($field, AllowedConditions::class)) {
                $allowed_conditions = $field::get_allowed_conditions();
                foreach ($allowed_conditions as $condition) {
                    if (\is_subclass_of($condition, Condition::class) && \is_subclass_of($condition, ConditionOperator::class)) {
                        $col_name = $field::get_column_name();
                        $key = ProductDAO::create_post_args_key($col_name, $condition::get_name());
                        if (!empty($wp_query->get($key)) && is_array($wp_query->get($key))) {
                            $values = [];
                            foreach ($wp_query->get($key) as $search_term) {
                                $val = $condition::get_operator_value($search_term);
                                $operator = $condition::get_operator();
                                $values[] = $wpdb->posts . '.' . $col_name . ' ' . $operator . ' \'' . $val . '\'';
                            }
                            $sql = implode(' OR ', $values);
                            $where .= ' AND (' . $sql . ')';
                        }
                    }
                }
            }
        }
        return $where;
    }
}
