<?php
namespace NeoRename\NeoGlobal; if (!defined("ABSPATH")) { die("neoWP direct access not allowed"); }

//  These direct tags are used only on our fully controlled backend pages, where WordPress enqueue functions cannot output assets at the required position.
function backend_page_script_tag_start(array $attributes = []): void {
    $validated_attributes = [];
    foreach ($attributes as $attribute_name => $attribute_value) {
        if (!(is_string($attribute_name) && \NeoRename\NeoGlobal\preg_match_better('/^[a-zA-Z][a-zA-Z0-9:-]*$/', $attribute_name))) { \NeoRename\NeoGlobal\throw_global_exception("Invalid backend page script attribute name: " . $attribute_name); }
        if ($attribute_value === null || $attribute_value === false) { continue; }
        if ($attribute_value === true) { $validated_attributes[$attribute_name] = true; continue; }
        if (!is_scalar($attribute_value)) { \NeoRename\NeoGlobal\throw_global_exception("Invalid backend page script attribute value for '" . $attribute_name . "'."); }
        $validated_attributes[$attribute_name] = (string)$attribute_value;
    }
    _backend_page_tag_buffer_start("script", $validated_attributes);
}

function backend_page_script_tag_end(): void {
    [$tag_content, $attributes] = _backend_page_tag_buffer_end("script");
    wp_print_inline_script_tag($tag_content, $attributes);
}

function backend_page_style_tag_start(array $attributes = []): void {
    if (\NeoRename\NeoGlobal\array_diff_better(array_keys($attributes), ["id"])) { \NeoRename\NeoGlobal\throw_global_exception("Backend page style tags only support the id attribute."); }
    if (isset($attributes["id"]) && !(is_string($attributes["id"]) && \NeoRename\NeoGlobal\preg_match_better('/^[a-zA-Z][a-zA-Z0-9_-]*-inline-css$/', $attributes["id"]))) { \NeoRename\NeoGlobal\throw_global_exception("Invalid backend page style id attribute. Style IDs must end with -inline-css: " . $attributes["id"]); }
    _backend_page_tag_buffer_start("style", $attributes);
}

function backend_page_style_tag_end(): void {
    [$tag_content, $attributes] = _backend_page_tag_buffer_end("style");
    static $style_counter = 0;
    $style_counter++;
    $handle = isset($attributes["id"]) ? \NeoRename\NeoGlobal\preg_replace_better('/-inline-css$/', "", $attributes["id"], 1) : "neo-rename--backend-page-inline-style-" . $style_counter;
    global $wp_styles;
    $global_wp_styles_to_do_count = isset($wp_styles) ? count($wp_styles->to_do ?? []) : null;
    $styles = new \WP_Styles();
    $styles->add($handle, false, [], "1");
    $styles->enqueue($handle);
    $styles->add_inline_style($handle, $tag_content);
    $styles->do_items([$handle]);
    if ($global_wp_styles_to_do_count !== (isset($wp_styles) ? count($wp_styles->to_do ?? []) : null)) { \NeoRename\NeoGlobal\throw_global_exception("Local backend page style output unexpectedly changed the global WP_Styles queue."); }
}

function _backend_page_tag_buffer_start(string $tag_name, array $attributes): void {
    \NeoRename\NeoGlobal\val("neo_global__backend_page_" . $tag_name . "_tag_buffers", []);
    \NeoRename\NeoGlobal\val("neo_global__backend_page_" . $tag_name . "_tag_buffers")[] = $attributes;
    ob_start();
}

function _backend_page_tag_buffer_end(string $tag_name): array {
    if (empty(\NeoRename\NeoGlobal\val("neo_global__backend_page_" . $tag_name . "_tag_buffers"))) { \NeoRename\NeoGlobal\throw_global_exception("Backend page " . $tag_name . " tag buffer was not started."); }
    $attributes = array_pop(\NeoRename\NeoGlobal\val("neo_global__backend_page_" . $tag_name . "_tag_buffers"));
    return [ob_get_clean(), $attributes];
}
