<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Library_Registry
{
    public const CONFIG_RELATIVE = 'config/component-libraries.json';

    public const HANDLE_PREFIX = 'puffergo-lib-';

    private static $registry_cache = null;

    public static function init()
    {
        add_filter('script_loader_tag', array(__CLASS__, 'filter_script_loader_tag_module'), 10, 3);
        add_action('enqueue_block_editor_assets', array(__CLASS__, 'localize_for_block_editor'), 27);
    }

    public static function get_all()
    {
        if (null !== self::$registry_cache) {
            return self::$registry_cache;
        } $path = PUFFERGO_PLUGIN_DIR . '/' . self::CONFIG_RELATIVE;

        if (! is_readable($path)) {
            self::$registry_cache = array();

            return self::$registry_cache;
        } $json = file_get_contents($path);
        $data = json_decode($json, true);
        self::$registry_cache = is_array($data) ? $data : array();

        return self::$registry_cache;
    }

    public static function get($handle)
    {
        $key = sanitize_key($handle);

        if ('' === $key) {
            return null;
        } $all = self::get_all();

        return isset($all[ $key ]) && is_array($all[ $key ]) ? $all[ $key ] : null;
    }

    public static function script_handle($handle)
    {
        return self::HANDLE_PREFIX . sanitize_key($handle);
    }

    public static function enqueue($handle)
    {
        $entry = self::get($handle);

        if (null === $entry) {
            return false;
        } $rel_file = isset($entry['file']) ? $entry['file'] : '';
        $rel_file = is_string($rel_file) ? $rel_file : '';
        $rel_file = ltrim($rel_file, '/');

        if ('' === $rel_file) {
            return false;
        } $full_path = PUFFERGO_PLUGIN_DIR . '/' . $rel_file;

        if (! is_readable($full_path)) {
            return false;
        } $script_handle = self::script_handle($handle);

        if (wp_script_is($script_handle, 'enqueued')) {
            return true;
        } $version = isset($entry['version']) ? (string) $entry['version'] : PUFFERGO_CORE_VERSION;

        if ('' === $version) {
            $version = PUFFERGO_CORE_VERSION;
        } $src = plugins_url($rel_file, PUFFERGO_PLUGIN_FILE);
        wp_enqueue_script($script_handle, $src, array(), $version, true);

        return true;
    }

    public static function filter_script_loader_tag_module($tag, $handle, $src)
    {
        unset($src);
        $prefix = self::HANDLE_PREFIX;
        $len = strlen($prefix);

        if (strlen($handle) <= $len || substr($handle, 0, $len) !== $prefix) {
            return $tag;
        } $logical = substr($handle, $len);
        $entry = self::get($logical);

        if (null === $entry || empty($entry['module'])) {
            return $tag;
        }

if (preg_match('/\btype\s*=\s*["\']/', $tag)) {
            return $tag;
        }

return str_replace('<script ', '<script type="module" ', $tag, 1);
    }

    public static function localize_for_block_editor()
    {
        if (! function_exists('register_block_type')) {
            return;
        } $registry = WP_Block_Type_Registry::get_instance();
        $block = $registry->get_registered('puffergo/tailwind-container');

        if (! $block || empty($block->editor_script)) {
            return;
        } $handles = is_array($block->editor_script) ? $block->editor_script : array($block->editor_script);
        $map = array();

        foreach (self::get_all() as $logical => $entry) {
            if (! is_string($logical) || '' === $logical) {
                continue;
            }

if (! is_array($entry)) {
                continue;
            } $rel_file = isset($entry['file']) ? $entry['file'] : '';
            $rel_file = is_string($rel_file) ? ltrim($rel_file, '/') : '';

            if ('' === $rel_file) {
                continue;
            } $full = PUFFERGO_PLUGIN_DIR . '/' . $rel_file;

            if (! is_readable($full)) {
                continue;
            } $key = sanitize_key($logical);
            $map[ $key ] = array('url' => esc_url_raw(plugins_url($rel_file, PUFFERGO_PLUGIN_FILE)), 'module' => ! empty($entry['module']), );
        }

foreach ($handles as $editor_handle) {
            if (empty($editor_handle)) {
                continue;
            } wp_localize_script($editor_handle, 'puffergoLibraries', $map);
        }
    }
} PufferGo_Library_Registry::init();
