<?php

/*
 * This file is part of the Fancy Bricks package.
 *
 * (c) Joshua Gugun Siagian <suabahasa@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare (strict_types=1);
namespace Fancy\Animations\Core;

use Bricks\Elements;
use Bricks\Helpers;
use Fancy\Animations\Bricks\Elements\ToggleAnimationsElement;
use WP_Query;
/**
 * Bricks Builder' integration
 */
class Bricks
{
    public function __construct()
    {
        \add_filter('f!fancy/animations/core/bricks:loop_content', fn(array $extracted, int $post_id): array => $this->loop_content($extracted, $post_id), -1, 2);
        \add_action('init', fn() => $this->load_elements(), 1000001);
        \add_action('after_setup_theme', fn() => $this->after_setup_theme(), 1000001);
    }
    public function load_elements()
    {
        if (!\class_exists('\\Bricks\\Elements')) {
            return;
        }
        Elements::register_element((new \ReflectionClass(ToggleAnimationsElement::class))->getFileName());
    }
    public function after_setup_theme()
    {
        \add_filter('f!fancy/animations/core/cache:selectors', fn(array $selectors = []): array => $this->scan_contents($selectors), 10);
        \add_action('save_post', fn($post_id, $post) => $this->schedule_purge($post_id, $post), 1000001, 2);
    }
    public function schedule_purge($post_id, $post)
    {
        if (\wp_is_post_revision($post)) {
            return;
        }
        if (!Helpers::render_with_bricks($post_id)) {
            return;
        }
        \Fancy\Animations\Core\Cache::schedule_cache(1);
    }
    /**
     * Scan the data to catch all selectors being used
     *
     * @param array $selectors The selectors
     * @return array
     */
    public function scan_contents($selectors = [])
    {
        $posts = [];
        $post_types = \Bricks\Database::$global_settings['postTypes'] ?? [];
        $post_types[] = \BRICKS_DB_TEMPLATE_SLUG;
        $wpQuery = new WP_Query(['posts_per_page' => -1, 'fields' => 'ids', 'post_type' => $post_types, 'meta_query' => ['relation' => 'OR', ['key' => \BRICKS_DB_PAGE_HEADER], ['key' => \BRICKS_DB_PAGE_CONTENT], ['key' => \BRICKS_DB_PAGE_FOOTER]]]);
        foreach ($wpQuery->posts as $post_id) {
            $posts[] = $post_id;
        }
        $extracted = [];
        foreach ($posts as $post) {
            /**
             * @param array $class The extracted classes
             * @param int $post_id The ID of the post
             * @return array
             */
            $extracted = \apply_filters('f!fancy/animations/core/bricks:loop_content', $extracted, $post);
        }
        $global_classes_index = [];
        foreach (\get_option(\BRICKS_DB_GLOBAL_CLASSES, []) as $value) {
            $global_classes_index[$value['id']] = $value['name'];
        }
        // swap the classes with actual names (BRICKS_DB_GLOBAL_CLASSES)
        $extracted = \array_map(static fn($class) => \array_key_exists($class, $global_classes_index) ? $global_classes_index[$class] : $class, $extracted);
        // filter duplicates
        $extracted = \array_unique($extracted);
        /**
         * extract the classes from the content of the posts
         *
         * @param array $extracted The extracted classes
         * @param array $post_id The array of post IDs
         * @return array
         */
        return \apply_filters('f!fancy/animations/core/bricks:content_of_posts', \array_merge($selectors, $extracted), $posts);
    }
    /**
     * @param array $extracted The extracted classes
     * @param int $post_id The ID of the post
     * @return array
     */
    public function loop_content($extracted, $post_id)
    {
        $post_meta_keys = [\BRICKS_DB_PAGE_HEADER, \BRICKS_DB_PAGE_CONTENT, \BRICKS_DB_PAGE_FOOTER];
        foreach ($post_meta_keys as $post_metum_key) {
            $meta_value = \get_post_meta($post_id, $post_metum_key, \true);
            if ($meta_value) {
                foreach ($meta_value as $m_v) {
                    // global classes
                    if (\array_key_exists('settings', $m_v) && \array_key_exists('_cssGlobalClasses', $m_v['settings'])) {
                        $extracted = \array_merge($extracted, $m_v['settings']['_cssGlobalClasses']);
                    }
                    // plain classes
                    if (\array_key_exists('settings', $m_v) && \array_key_exists('_cssClasses', $m_v['settings']) && !empty(\trim($m_v['settings']['_cssClasses']))) {
                        $extracted = \array_merge($extracted, \explode(' ', $m_v['settings']['_cssClasses']));
                    }
                }
            }
        }
        return $extracted;
    }
}
