<?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\Utils;

use _FancyAnimations\FANCY_ANIMATIONS;
/**
 * Helper tools to interact with Bricks.
 *
 * @since 1.0.0
 */
class BricksHelper
{
    public static function register_selectors()
    {
        $prefix = 'fba_';
        $selectors = \file_get_contents(\dirname(FANCY_ANIMATIONS::FILE) . '/build/builder-selectors.json');
        $selectors = \json_decode($selectors, \true, 512, \JSON_THROW_ON_ERROR)['classes'];
        $global_classes = (array) \get_option('bricks_global_classes', []);
        $global_class_names = \array_column($global_classes, 'name');
        $locked_classes = (array) \get_option('bricks_global_classes_locked', []);
        /**
         * Global classes array structure:
         * 0 => array (
         *      'id' => a random string,
         *      'name' => the actual class name,
         *      'settings' => array()
         *  )
         *
         * Locked classes array structure:
         * array (
         *  0 => 'align-content--baseline'
         *  )
         */
        foreach ($selectors as $selector) {
            // STEP: add our class to Bricks global classes, if it's not there yet.
            if (!\in_array($selector, $global_class_names, \true)) {
                $global_classes[] = ['id' => $prefix . $selector, 'name' => $selector, 'settings' => []];
            }
            // STEP: add our class to Bricks locked classes, if it's not there yet.
            if (!\in_array($prefix . $selector, $locked_classes, \true)) {
                $locked_classes[] = $prefix . $selector;
            }
        }
        self::register_toggle_animation_css($global_classes, $global_class_names);
        // STEP: update the options.
        \update_option('bricks_global_classes', $global_classes, \false);
        \update_option('bricks_global_classes_locked', $locked_classes, \false);
    }
    public static function deregister_selectors()
    {
        $prefix = 'fba_';
        $selectors = \file_get_contents(\dirname(FANCY_ANIMATIONS::FILE) . '/build/builder-selectors.json');
        $selectors = \json_decode($selectors, \true, 512, \JSON_THROW_ON_ERROR)['classes'];
        $global_classes = (array) \get_option('bricks_global_classes', []);
        $locked_classes = (array) \get_option('bricks_global_classes_locked', []);
        /**
         * Global classes array structure:
         * 0 => array (
         *      'id' => a random string,
         *      'name' => the actual class name,
         *      'settings' => array()
         *  )
         *
         * Locked classes array structure:
         * array (
         *  0 => 'align-content--baseline'
         *  )
         */
        // remove our classes (id prefix: fba_) from Bricks global classes and locked classes
        $global_classes = \array_filter($global_classes, static fn($global_class) => \strpos($global_class['id'], $prefix) === \false);
        $locked_classes = \array_filter($locked_classes, static fn($locked_class) => \strpos($locked_class, $prefix) === \false);
        self::deregister_toggle_animation_css($global_classes);
        // STEP: update the options.
        \update_option('bricks_global_classes', $global_classes, \false);
        \update_option('bricks_global_classes_locked', $locked_classes, \false);
    }
    public static function register_toggle_animation_css(&$global_classes, $global_class_names)
    {
        $selectors = ['fb-disable-animations' => ['_width' => 'fit-content', '_position' => 'relative', '_cssCustom' => ".fb-disable-animations:focus-within .fb-disable-animations__tooltip, .fb-disable-animations:hover .fb-disable-animations__tooltip{\n  visibility: visible;\n  opacity: 1;\n}"], 'fb-disable-animations__btn' => ['_width' => '3rem', '_height' => '3rem', '_padding' => ['top' => '.3em', 'right' => '.3em', 'bottom' => '.3em', 'left' => '.3em'], '_background' => ['color' => ['hex' => '#f5f5f5', 'name' => 'Color #1']], '_border' => ['radius' => ['top' => '50%', 'right' => '50%', 'bottom' => '50%', 'left' => '50%'], 'width' => ['top' => '1', 'right' => '1', 'bottom' => '1', 'left' => '1'], 'style' => 'solid', 'color' => ['hex' => '#000000', 'name' => 'black']], '_background:hover' => ['color' => ['hex' => '#e0e0e0', 'name' => 'Color #2']], '_typography' => ['color' => ['hex' => '#000000', 'name' => 'black']], '_cssCustom' => ".fb-disable-animations__btn[aria-checked=\"false\"]{\n\n  opacity: .6;\n  filter: grayscale(1);\n  \n}\n\n.fb-disable-animations__btn:hover{\n  filter: grayscale(0);\n}"], 'fb-disable-animations__icon' => ['_width' => '2rem', '_height' => '2rem', 'iconSize' => '2rem', 'iconColor' => ['raw' => 'inherit']], 'fb-disable-animations__tooltip' => ['_position' => 'absolute', '_width' => 'max-content', '_widthMin' => '100px', '_typography' => ['text-align' => 'center', 'line-height' => '1em', 'color' => ['hex' => '#f5f5f5', 'name' => 'Color #1']], '_padding' => ['top' => '.5em', 'right' => '.5em', 'bottom' => '.5em', 'left' => '.5em'], '_transform' => ['translateX' => '-50%', 'translateY' => '100%'], '_bottom' => '-.5rem', '_left' => '50%', '_background' => ['color' => ['hex' => '#000000', 'name' => 'black']], '_border' => ['radius' => ['top' => '.5em', 'right' => '.5em', 'bottom' => '.5em', 'left' => '.5em']], '_visibility' => 'hidden', '_opacity' => '0', '_cssTransition' => 'all .5s']];
        foreach ($selectors as $selector => $properties) {
            // STEP: add our class to Bricks global classes, if it's not there yet.
            if (!\in_array($selector, $global_class_names, \true)) {
                $global_classes[] = ['id' => $selector, 'name' => $selector, 'settings' => $properties];
            }
        }
    }
    public static function deregister_toggle_animation_css(&$global_classes)
    {
        $selectors = ['fb-disable-animations', 'fb-disable-animations__btn', 'fb-disable-animations__icon', 'fb-disable-animations__tooltip'];
        // remove our classes from Bricks global classes
        $global_classes = \array_filter($global_classes, static fn($global_class) => !\in_array($global_class['id'], $selectors, \true));
    }
}
