<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! function_exists( 'reeid_gutenberg_rules' ) ) {
    function reeid_gutenberg_rules(): array {

        $cache_key = 'reeid_gb_rules_v1';
        $cached    = get_transient( $cache_key );

        if (
            is_array( $cached )
            && ! empty( $cached['skip_prefixes'] )
            && ! empty( $cached['skip_keys'] )
            && ! empty( $cached['textish_keys'] )
        ) {
            return $cached;
        }

        $out = reeid_gutenberg_rules_fallback( 'local_only' );

        update_option( 'reeid_gb_rules_source', 'local', false );
        update_option( 'reeid_gb_rules_version', 'local', false );
        update_option( 'reeid_gb_rules_fetched_at', time(), false );

        set_transient( $cache_key, $out, 12 * HOUR_IN_SECONDS );

        return $out;
    }
}

if ( ! function_exists( 'reeid_gutenberg_rules_fallback' ) ) {
    function reeid_gutenberg_rules_fallback( string $reason = '' ): array {

        update_option( 'reeid_gb_rules_source', 'fallback', false );
        update_option( 'reeid_gb_rules_reason', $reason, false );

        return array(
            'skip_prefixes' => array(
                'style', 'color', 'background', 'border', 'epcustom', 'epanimation',
                'layout', 'spacing',
            ),
            'skip_keys' => array(
                'classname', 'anchor', 'align', 'backgroundcolor', 'textcolor',
                'gradient', 'layout', 'id', 'aria', 'arialabel', 'url', 'href',
                'rel', 'target', 'name', 'slug', 'tagname', 'tag', 'reference',
                'width', 'height', 'minheight', 'maxheight', 'gap', 'padding',
                'margin', 'border', 'radius', 'opacity',
                'mediaurl', 'src', 'srcset', 'sizes',
            ),
            'skip_path_contains' => array(
                '.attrs.style.',
            ),
            'textish_keys' => array(
                'content',
                'title',
                'caption',
                'label',
                'text',
                'value',
                'placeholder',
                'description',
                'alt',
                'subtitle',
            ),
        );
    }
}

if (! function_exists('reeid_gutenberg_classic_translate_adapter')) {
    function reeid_gutenberg_classic_translate_adapter(
        string $content,
        string $target_lang,
        string $tone,
        string $prompt
    ): array {
        if (!function_exists('reeid_gutenberg_classic_translate_via_extractor')) {
            return [
                'success' => false,
                'ok'      => false,
                'title'   => '',
                'content' => '',
                'excerpt' => '',
                'slug'    => '',
                'error'   => 'gutenberg_engine_missing',
            ];
        }

        $translated = reeid_gutenberg_classic_translate_via_extractor(
            $content,
            $target_lang,
            $tone,
            $prompt
        );

        $pack = [
            'success' => is_string($translated) && $translated !== '',
            'ok'      => is_string($translated) && $translated !== '',
            'title'   => '',
            'content' => is_string($translated) ? $translated : '',
            'excerpt' => '',
            'slug'    => '',
            'error'   => '',
        ];

        return function_exists('reeid_normalize_translation_pack')
            ? reeid_normalize_translation_pack($pack)
            : $pack;
    }
}