<?php
// Missing border
// submenu z-index i background
// dynamic micro parts button i id ili smeni go u metafield block


add_filter( 'register_block_type_args', 'modify_core_group_block_args', 10, 2 );
function modify_core_group_block_args( $args, $name ) {
//if ( 'stepfox/metafield-block' === $name ) {

            // Change hide attributes from boolean to string with a default of "not-selected"



//            $args['attributes']['backgroundColor'] = [ "type" => "string", "default" => "" ];
//            $args['attributes']['gradient'] = [ "type" => "string", "default" => "" ];
//            $args['attributes']['linkColor'] = [ "type" => "string", "default" => "" ];
//            $args['attributes']['textColor'] = [ "type" => "string", "default" => "" ];
//            $args['attributes']['layout'] = [ "type" => "object", "default" => [ "type" => "default" ] ];

            $args['attributes']['font_size_desktop'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['line_height_desktop'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['order_desktop'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['width_desktop'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['desktop_padding'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['desktop_margin'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['desktop_border'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['desktop_pos'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['desktop_position'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['desktop_display'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['desktop_textShadow'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['desktop_flexWrap'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['desktop_columns'] = [ "type" => "string", "default" => "" ];

            $args['attributes']['font_size_tablet'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['line_height_tablet'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['order_tablet'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['width_tablet'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['tablet_padding'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['tablet_margin'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['tablet_border'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['tablet_pos'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['tablet_position'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['tablet_display'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['tablet_textShadow'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['tablet_flexWrap'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['tablet_columns'] = [ "type" => "string", "default" => "" ];

            $args['attributes']['font_size_mobile'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['line_height_mobile'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['order_mobile'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['width_mobile'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['mobile_padding'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['mobile_margin'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['mobile_border'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];

            $args['attributes']['mobile_pos'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
            $args['attributes']['mobile_position'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['mobile_display'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['mobile_textShadow'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['mobile_flexWrap'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['mobile_columns'] = [ "type" => "string", "default" => "" ];








            $args['attributes']['customId'] = [ "type" => "string", "default" => "stepfox-not-set-id" ];
            $args['attributes']['custom_css'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['custom_js'] = [ "type" => "string", "default" => "" ];
            $args['attributes']['device'] = [ "type" => "string", "default" => "" ];




$args['attributes']['desktop_textAlign'] = [ "type" => "string", "default" => "" ];
$args['attributes']['desktop_justify'] = [ "type" => "string", "default" => "" ];

$args['attributes']['desktop_z_index'] = [ "type" => "string", "default" => "" ];
$args['attributes']['desktop_overflow'] = [ "type" => "string", "default" => "" ];
$args['attributes']['desktop_zoom'] = [ "type" => "string", "default" => "" ];
$args['attributes']['desktop_height'] = [ "type" => "string", "default" => "" ];
$args['attributes']['desktop_flex_direction'] = [ "type" => "string", "default" => "" ];
$args['attributes']['desktop_opacity'] = [ "type" => "string", "default" => "" ];
$args['attributes']['desktop_white_space'] = [ "type" => "string", "default" => "" ];

$args['attributes']['desktop_flex_grow'] = [ "type" => "string", "default" => "" ];
$args['attributes']['desktop_transition'] = [ "type" => "string", "default" => "" ];




$args['attributes']['tablet_justify'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_textAlign'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_z_index'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_overflow'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_zoom'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_height'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_flex_direction'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_opacity'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_white_space'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_flex_grow'] = [ "type" => "string", "default" => "" ];
$args['attributes']['tablet_transition'] = [ "type" => "string", "default" => "" ];

$args['attributes']['mobile_justify'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_textAlign'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_z_index'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_overflow'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_zoom'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_height'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_flex_direction'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_opacity'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_white_space'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_flex_grow'] = [ "type" => "string", "default" => "" ];
$args['attributes']['mobile_transition'] = [ "type" => "string", "default" => "" ];

$args['attributes']['animation'] = [ "type" => "string", "default" => "" ];
$args['attributes']['animation_delay'] = [ "type" => "string", "default" => "" ];
$args['attributes']['animation_duration'] = [ "type" => "string", "default" => "" ];





//$args['attributes']['transition'] = [ "type" => "string", "default" => "" ];
//$args['attributes']['flex_grow'] = [ "type" => "string", "default" => "" ];
//}

    return $args;
}


add_action( 'wp_loaded', function() {

//    $registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
//
//    foreach( $registered_blocks as $name => $block ) {
//        // For core/group block, set some basic attributes.
//        if ( $block->name == 'core/group' ) {
//
//            $block->attributes['layout'] = [ "type" => "object", "default" => [ "type" => "default" ] ];
//            $block->attributes['backgroundColor'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['gradient'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['linkColor'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['textColor'] = [ "type" => "string", "default" => "" ];
//            // Overwrite layout attribute again (if needed).
//            $block->attributes['layout'] = [ "type" => "object", "default" => [ "type" => "default" ] ];
//        }
//
//        // For our custom stepfox blocks.
//        if ( $block->name == 'stepfox/intellibridge-query-block' ||   $block->name == 'stepfox/metafield-block1') {
//
//
//            $block->attributes['theme_colors'] = [ "type" => "object", "default" => [ "white" => "#FFF" ] ];
//            $block->attributes['theme_colors_update'] = [ "type" => "string", "default" => [] ];
//
//            // Change hide attributes from boolean to string with a default of "not-selected"
//            $block->attributes['hide_tablet'] = [ "type" => "string", "default" => "not-selected" ];
//            $block->attributes['text_align_tablet'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['hide_mobile'] = [ "type" => "string", "default" => "not-selected" ];

//            $block->attributes['text_align_mobile'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['justify_tablet'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['justify_mobile'] = [ "type" => "string", "default" => "" ];
//
//            $block->attributes['backgroundColor'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['gradient'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['linkColor'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['textColor'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['layout'] = [ "type" => "object", "default" => [ "type" => "default" ] ];
//
//            $block->attributes['font_size_desktop'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['line_height_desktop'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['order_desktop'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['width_desktop'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['desktop_padding'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['desktop_margin'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['desktop_border'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['desktop_pos'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['desktop_position'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['desktop_display'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['desktop_textShadow'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['desktop_flexWrap'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['desktop_columns'] = [ "type" => "string", "default" => "" ];
//
//            $block->attributes['font_size_tablet'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['line_height_tablet'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['order_tablet'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['width_tablet'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['tablet_padding'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['tablet_margin'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['tablet_border'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['tablet_pos'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['tablet_position'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['tablet_display'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['tablet_textShadow'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['tablet_flexWrap'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['tablet_columns'] = [ "type" => "string", "default" => "" ];
//
//            $block->attributes['font_size_mobile'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['line_height_mobile'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['order_mobile'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['width_mobile'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['mobile_padding'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['mobile_margin'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['mobile_border'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//
//            $block->attributes['mobile_pos'] = [ "type" => "object", "default" => [ "top" => "0", "left" => "0", "right" => "0", "bottom" => "0" ] ];
//            $block->attributes['mobile_position'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['mobile_display'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['mobile_textShadow'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['mobile_flexWrap'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['mobile_columns'] = [ "type" => "string", "default" => "" ];
//
//
//
//
//
//
//
//
//            $block->attributes['customId'] = [ "type" => "string", "default" => "stepfox-not-set-id" ];
//            $block->attributes['custom_css'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['custom_js'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['device'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['desktop_textAlign'] = [ "type" => "string", "default" => "" ];
//            $block->attributes['desktop_justify'] = [ "type" => "string", "default" => "" ];
//
//
//
//        }
//    }

}, 100);





function wrap_group_and_columns($block_content, $block)
{
    // If the block has a customId (and is not a spacer) add the custom id to the outer tag.
    if ( ! empty( $block['attrs']['customId'] ) && $block['blockName'] != 'core/spacer' ) {

        $content = preg_replace( '(>+)', ' id="block_' . $block['attrs']['customId'] . '" > ', $block_content, 1 );

        preg_match( '/(<[^>]*>)/i', $content, $first_line );
        if(!empty($first_line[0])) {
            if (strpos($first_line[0], 'style="') !== false &&
                strpos($first_line[0], 'id="block_' . $block['attrs']['customId'] . '"') !== false) {
                $content = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $content, 1);
            }
        }

        return $content;
    }

    if ( ! empty( $block['attrs']['image_block_id'] ) ) {
        // Additional image_block_id processing if needed.
    }

    return $block_content;
}
add_filter( 'render_block', 'wrap_group_and_columns', 10, 2 );


function search($array, $key)
{
    $results = array();

    if ( is_array( $array ) ) {
        if ( isset( $array[$key] ) ) {
            $results[] = $array;
        }
        foreach ( $array as $subarray ) {
            $results = array_merge( $results, search( $subarray, $key ) );
        }
    }

    return $results;
}


function stepfox_styling() {
    wp_reset_postdata();
    global $_wp_current_template_content;
    $page_content = get_the_content();
    $full_content = $_wp_current_template_content . $page_content;

    if ( has_blocks( $full_content ) ) {
        $blocks = parse_blocks( $full_content );
        $all_blocks = search( $blocks, 'blockName' );
        // Get template parts content.
        foreach ( $all_blocks as $block ) {
            $full_content .= get_template_parts_as_content( $block );
        }
        $blocks = parse_blocks( $full_content );
        $all_blocks = search( $blocks, 'blockName' );
        $inline_style = '';
        wp_register_style( 'intellibridge-custom-style', false );
        wp_enqueue_style( 'intellibridge-custom-style' );

        foreach ( $all_blocks as $block ) {
            if ( ( $block['blockName'] === 'core/block' && ! empty( $block['attrs']['ref'] ) ) ||
                ( $block['blockName'] === 'core/navigation' && ! empty( $block['attrs']['ref'] ) ) ) {
                $content = get_post_field( 'post_content', $block['attrs']['ref'] );
                $reusable_blocks = parse_blocks( $content );
                $all_reusable_blocks = search( $reusable_blocks, 'blockName' );
                foreach ( $all_reusable_blocks as $reusable_block ) {
                    $inline_style .= inline_styles_for_blocks( $reusable_block );
                }
            }

            if ( $block['blockName'] == 'stepfox/intellibridge-query-block' && $block['attrs']['className'] == 'is-style-custom-template-part' ) {
                if ( is_numeric( $block['attrs']['template_part'] ) ) {
                    $template_part = get_block_template( 'intellibridge//' . basename( get_permalink( $block['attrs']['template_part'] ) ), 'wp_template_part' );
                } else {
                    $template_part = get_block_template( 'intellibridge//' . $block['attrs']['template_part'], 'wp_template_part' );
                }
                $inline_style .= inline_styles_for_blocks( $block );
                $template_part_content = $template_part->content;
                if ( $block['attrs']["post_type"] == 'wp_template_part' && $block['attrs']['source'] == 'manual_selection' ) {
                    $manual_selection = $block['attrs']['manual_selection'];
                    foreach ( $manual_selection as $item ) {
                        if ( is_numeric( $item['toplistitem'] ) ) {
                            $template_part_inner = get_block_template( 'intellibridge//' . basename( get_permalink( $item['toplistitem'] ) ), 'wp_template_part' );
                        } else {
                            $template_part_inner = get_block_template( 'intellibridge//' . $item['toplistitem'], 'wp_template_part' );
                        }
                        $template_part_content .= $template_part_inner->content;
                    }
                }
                $template_blocks = parse_blocks( $template_part_content );
                $all_template_blocks = search( $template_blocks, 'blockName' );
                foreach ( $all_template_blocks as $template_block ) {
                    $inline_style .= inline_styles_for_blocks( $template_block );
                }
            } elseif ( $block['blockName'] == 'stepfox/intellibridge-query-block' && $block['attrs']['className'] != 'is-style-custom-template-part' ) {
                $inline_style .= inline_styles_for_blocks( $block );
            } elseif ( $block['blockName'] == 'stepfox/casino-query-block' && $block['attrs']['className'] != 'is-style-custom-template-part' ) {
                $card = str_replace( 'is-style-', '', $block['attrs']['className'] );
                wp_enqueue_style( 'casino-query-block-' . $card, get_template_directory_uri() . '/blocks/casino_query_block/cards/css/card-' . $card . '.css' );
                $inline_style .= inline_styles_for_blocks( $block );
            } else {
                $inline_style .= inline_styles_for_blocks( $block );
            }
        }

        wp_add_inline_style( 'intellibridge-custom-style', $inline_style );
    }
}
add_action( 'wp_head', 'stepfox_styling' );


function get_template_parts_as_content($block) {
    $template_parts_content = '';
    if ( $block['blockName'] == 'core/template-part' ) {
        $template_part = get_block_template( $block['attrs']['theme'] . '//' . $block['attrs']['slug'], 'wp_template_part' );
        $template_part_content = $template_part->content;
        $template_blocks = parse_blocks( $template_part_content );
        $all_template_blocks = search( $template_blocks, 'blockName' );
        $template_parts_content .= $template_part_content;
        foreach ( $all_template_blocks as $template_block ) {
            if ( $template_block['blockName'] == 'core/template-part' ) {
                $template_part_1 = get_block_template( $template_block['attrs']['theme'] . '//' . $template_block['attrs']['slug'], 'wp_template_part' );
                $template_part_content_1 = $template_part_1->content;
                $template_parts_content .= $template_part_content_1;
            }
        }
    }
    return $template_parts_content;
}

function decode_css_var( $input ) {
    // Check if the string starts with "var:"
    if ( strpos( $input, 'var:' ) === 0 ) {
        // Remove the "var:" prefix.
        $trimmed = substr( $input, 4 );
        // Replace '|' with '--'
        $variable_part = str_replace( '|', '--', $trimmed );
        // Return the final CSS variable format.
        return 'var(--wp--' . $variable_part . ')';
    }

    // If the string doesn't start with "var:", return it unchanged.
    return $input;
}

function inline_styles_for_blocks($block) {


    if ( ! empty( $block['attrs']['customId'] ) || ! empty( $block['attrs']['image_block_id'] ) ) {

        // Set width if desktop width is provided.
        if ( ! empty( $block['attrs']['width_desktop'] ) ) {
            $block['attrs']['width'] = $block['attrs']['width_desktop'];
        }
        if ( ! empty( $block['attrs']['font_size_desktop'] ) ) {
            $block['attrs']['style']['typography']['fontSize'] = $block['attrs']['font_size_desktop'];
        }
        if ( ! empty( $block['attrs']['line_height_desktop'] ) ) {
            $block['attrs']['style']['typography']['lineHeight'] = $block['attrs']['line_height_desktop'];
        }
        if ( ! empty( $block['attrs']['desktop_padding']['right'] ) ) {
            $block['attrs']['style']['spacing']['padding']['right'] = $block['attrs']['desktop_padding']['right'];
        }
        if ( ! empty( $block['attrs']['desktop_padding']['left'] ) ) {
            $block['attrs']['style']['spacing']['padding']['left'] = $block['attrs']['desktop_padding']['left'];
        }
        if ( ! empty( $block['attrs']['desktop_padding']['bottom'] ) ) {
            $block['attrs']['style']['spacing']['padding']['bottom'] = $block['attrs']['desktop_padding']['bottom'];
        }
        if ( ! empty( $block['attrs']['desktop_padding']['top'] ) ) {
            $block['attrs']['style']['spacing']['padding']['top'] = $block['attrs']['desktop_padding']['top'];
        }

        if ( ! empty( $block['attrs']['desktop_border']['right'] ) ) {
            $block['attrs']['style']['spacing']['border']['right'] = $block['attrs']['desktop_border']['right'];
        }
        if ( ! empty( $block['attrs']['desktop_border']['left'] ) ) {
            $block['attrs']['style']['spacing']['border']['left'] = $block['attrs']['desktop_border']['left'];
        }
        if ( ! empty( $block['attrs']['desktop_border']['bottom'] ) ) {
            $block['attrs']['style']['spacing']['border']['bottom'] = $block['attrs']['desktop_border']['bottom'];
        }
        if ( ! empty( $block['attrs']['desktop_border']['top'] ) ) {
            $block['attrs']['style']['spacing']['border']['top'] = $block['attrs']['desktop_border']['top'];
        }

        if ( ! empty( $block['attrs']['desktop_margin']['right'] ) ) {
            $block['attrs']['style']['spacing']['margin']['right'] = $block['attrs']['desktop_margin']['right'];
        }
        if ( ! empty( $block['attrs']['desktop_margin']['left'] ) ) {
            $block['attrs']['style']['spacing']['margin']['left'] = $block['attrs']['desktop_margin']['left'];
        }
        if ( ! empty( $block['attrs']['desktop_margin']['bottom'] ) ) {
            $block['attrs']['style']['spacing']['margin']['bottom'] = $block['attrs']['desktop_margin']['bottom'];
        }
        if ( ! empty( $block['attrs']['desktop_margin']['top'] ) ) {
            $block['attrs']['style']['spacing']['margin']['top'] = $block['attrs']['desktop_margin']['top'];
        }

        if ( ! empty( $block['attrs']['tablet_padding'] ) ) {
            $block['attrs']['padding_tablet'] = $block['attrs']['tablet_padding'];
        }
        if ( ! empty( $block['attrs']['tablet_margin'] ) ) {
            $block['attrs']['margin_tablet'] = $block['attrs']['tablet_margin'];
        }

        if ( ! empty( $block['attrs']['mobile_padding'] ) ) {
            $block['attrs']['padding_mobile'] = $block['attrs']['mobile_padding'];
        }
        if ( ! empty( $block['attrs']['mobile_margin'] ) ) {
            $block['attrs']['margin_mobile'] = $block['attrs']['mobile_margin'];
        }

        if ( strpos( $block['blockName'], 'acf/' ) !== false ) {
            $block['attrs']['customId'] = str_replace( 'block_', '', $block['attrs']['customId'] );
        }
        if ( ! empty( $block['attrs']['image_block_id'] ) ) {
            $block['attrs']['customId'] = $block['attrs']['image_block_id'];
        }

        $inlineStyles = '#block_' . $block['attrs']['customId'] . '{';
        if ( ! empty( $block['attrs']['style']['color']['background'] ) ) {
            $inlineStyles .= 'background-color:' . $block['attrs']['style']['color']['background'] . ';';
        }

        if ( ! empty( $block['attrs']['style']['background']['backgroundImage'] ) ) {
            $inlineStyles .= 'background:url(' . $block['attrs']['style']['background']['backgroundImage']['url'] . ');background-size: 100%;';
        }
        if ( ! empty( $block['attrs']['theme_colors'] ) ) {
            foreach ( $block['attrs']['theme_colors'] as $theme_color => $value ) {
                if ( $theme_color == 'gradient_override' ) {
                    $inlineStyles .= '--wp--preset--gradient--flag: ' . $value . ';';
                } else {
                    $inlineStyles .= '--wp--preset--color--' . $theme_color . ':' . $value . ';';
                }
            }
        }

        if ( ! empty( $block['attrs']['start_from'] ) ) {
            $inlineStyles .= '--start-from:' . $block['attrs']['start_from'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['color']['gradient'] ) ) {
            $inlineStyles .= 'background:' . $block['attrs']['style']['color']['gradient'] . ';';
        }
        if ( ! empty( $block['attrs']['backgroundColor'] ) ) {
            $inlineStyles .= 'background-color:var(--wp--preset--color--' . $block['attrs']['backgroundColor'] . ');';
        }
        if ( ! empty( $block['attrs']['style']['color']['text'] ) ) {
            $inlineStyles .= 'color:' . $block['attrs']['style']['color']['text'] . ';';
        }
        if ( ! empty( $block['attrs']['textColor'] ) ) {
            $inlineStyles .= 'color:var(--wp--preset--color--' . $block['attrs']['textColor'] . ');';
        }
        // shadow
        if ( ! empty( $block['attrs']['style']['shadow'] ) ) {
            $inlineStyles .= 'box-shadow:' . $block['attrs']['style']['shadow'] . ';';
        }
        if ( ! empty( $block['attrs']['animation'] ) ) {
            $inlineStyles .= 'animation:' . $block['attrs']['animation'] . ';';
        }
        if ( ! empty( $block['attrs']['animation_delay'] ) ) {
            $inlineStyles .= 'animation-delay:' . $block['attrs']['animation_delay'] . 's;';
        }
        if ( ! empty( $block['attrs']['animation_duration'] ) ) {
            $inlineStyles .= 'animation-duration:' . $block['attrs']['animation_duration'] . 's;';
        }

        // Typography
        if ( ! empty( $block['attrs']['style']['typography']['fontSize'] ) ) {
            $inlineStyles .= 'font-size:' . $block['attrs']['style']['typography']['fontSize'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['typography']['textDecoration'] ) ) {
            $inlineStyles .= 'text-decoration:' . $block['attrs']['style']['typography']['textDecoration'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['typography']['letterSpacing'] ) ) {
            $inlineStyles .= 'letter-spacing:' . $block['attrs']['style']['typography']['letterSpacing'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['typography']['lineHeight'] ) ) {
            $inlineStyles .= 'line-height:' . $block['attrs']['style']['typography']['lineHeight'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['typography']['fontWeight'] ) ) {
            $inlineStyles .= 'font-weight:' . $block['attrs']['style']['typography']['fontWeight'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['typography']['fontStyle'] ) ) {
            $inlineStyles .= 'font-style:' . $block['attrs']['style']['typography']['fontStyle'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['typography']['textTransform'] ) ) {
            $inlineStyles .= 'text-transform:' . $block['attrs']['style']['typography']['textTransform'] . ';';
        }

        if ( ! empty( $block['attrs']['style']['border']['width'] ) ) {
            $inlineStyles .= 'border-width:' . $block['attrs']['style']['border']['width'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['border']['style'] ) ) {
            $inlineStyles .= 'border-style:' . $block['attrs']['style']['border']['style'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['border']['radius'] ) ) {
            $inlineStyles .= 'border-radius:' . $block['attrs']['style']['border']['radius'] . ';';
            $inlineStyles .= 'overflow:hidden;';
        }
        if ( ! empty( $block['attrs']['style']['border']['color'] ) ) {
            $inlineStyles .= 'border-color:' . $block['attrs']['style']['border']['color'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['borderColor'] ) ) {
            $inlineStyles .= 'border-color:' . $block['attrs']['style']['borderColor'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['border'] ) ) {
            foreach ( $block['attrs']['style']['border'] as $border_side_key => $border_side_value ) {
                if ( ! empty( $border_side_value["width"] ) ) {
                    $inlineStyles .= 'border-' . $border_side_key . '-width:' . decode_css_var($border_side_value['width']) . ';';
                    if(!empty($border_side_value['color'])) {
                        $inlineStyles .= 'border-' . $border_side_key . '-color:' . decode_css_var($border_side_value['color']) . ';';
                    }
                    $inlineStyles .= 'border-' . $border_side_key . '-style:solid;';
                    if(!empty($border_side_value['radius'])) {
                        $inlineStyles .= 'border-' . $border_side_key . '-radius:' . decode_css_var($border_side_value['radius']) . ';';
                    }
                }
            }
        }
        if ( ! empty( $block['attrs']['desktop_border']['top'] ) ) {
            $inlineStyles .= 'border-top-width:' . $block['attrs']['desktop_border']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_border']['bottom'] ) ) {
            $inlineStyles .= 'border-bottom-width:' . $block['attrs']['desktop_border']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_border']['left'] ) ) {
            $inlineStyles .= 'border-left-width:' . $block['attrs']['desktop_border']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_border']['right'] ) ) {
            $inlineStyles .= 'border-right-width:' . $block['attrs']['desktop_border']['right'] . ';';
        }

        if ( ! empty( $block['attrs']['image_block_id'] ) ) {
            if ( ! empty( $block['attrs']['width'] ) ) {
                $inlineStyles .= 'width:' . $block['attrs']['width'] . 'px;';
                $block['attrs']['width'] = '';
            }
            if ( ! empty( $block['attrs']['height'] ) ) {
                $inlineStyles .= 'height:' . $block['attrs']['height'] . 'px;';
                $block['attrs']['height'] = '';
            }
        }
        // Block width
        if ( ! empty( $block['attrs']['layout']['contentSize'] ) && $block['blockName'] != 'core/group' ) {
            $inlineStyles .= 'max-width:' . $block['attrs']['layout']['contentSize'] . ';';
        }
        if ( ! empty( $block['attrs']['layout']['contentSize'] ) && $block['blockName'] == 'core/group' ) {
            $inlineStyles .= 'flex-basis:' . $block['attrs']['layout']['contentSize'] . '; max-width:100%;';
        }
        if ( ! empty( $block['attrs']['width'] ) && $block['blockName'] != 'core/column' ) {
            $inlineStyles .= 'width:' . $block['attrs']['width'] . ';';
        }
        if ( ! empty( $block['attrs']['width'] ) && $block['blockName'] === 'core/column' ) {
            $inlineStyles .= 'flex-basis:' . $block['attrs']['width'] . ' !important;';
        }

        if ( ! empty( $block['attrs']['layout']['type'] ) && $block['attrs']['layout']['type'] == 'flex' ) {
            $inlineStyles .= 'display:' . $block['attrs']['layout']['type'] . ';';
            if ( ! empty( $block['attrs']['layout']['flexWrap'] ) ) {
                $inlineStyles .= 'flex-wrap:' . $block['attrs']['layout']['flexWrap'] . ';';
            } else {
                $inlineStyles .= 'flex-wrap:wrap;';
            }
            if ( ! empty( $block['attrs']['layout']['verticalAlignment'] ) ) {
                $verticalAlignment = 'center';
                if ( $block['attrs']['layout']['verticalAlignment'] == 'bottom' ) {
                    $verticalAlignment = 'flex-end';
                } elseif ( $block['attrs']['layout']['verticalAlignment'] == 'top' ) {
                    $verticalAlignment = 'flex-start';
                }
                $inlineStyles .= 'align-items:' . $verticalAlignment . ';';
            } else {
                $inlineStyles .= 'align-items:center;';
            }
            if ( ! empty( $block['attrs']['layout']['justifyContent'] ) ) {
                if ( $block['attrs']['layout']['justifyContent'] == 'left' ) {
                    $inlineStyles .= 'justify-content:flex-start;';
                }
                if ( $block['attrs']['layout']['justifyContent'] == 'center' ) {
                    $inlineStyles .= 'justify-content:center;';
                }
                if ( $block['attrs']['layout']['justifyContent'] == 'right' ) {
                    $inlineStyles .= 'justify-content:flex-end;';
                }
                if ( $block['attrs']['layout']['justifyContent'] == 'space-between' ) {
                    $inlineStyles .= 'justify-content:space-between;';
                }
            }
            if ( ! empty( $block['attrs']['layout']['orientation'] ) ) {
                if ( $block['attrs']['layout']['orientation'] == 'vertical' ) {
                    $inlineStyles .= 'flex-direction:column;';
                    if ( ! empty( $block['attrs']['layout']['justifyContent'] ) ) {
                        if ( $block['attrs']['layout']['justifyContent'] == 'left' ) {
                            $inlineStyles .= 'align-items:flex-start;';
                        }
                        if ( $block['attrs']['layout']['justifyContent'] == 'center' ) {
                            $inlineStyles .= 'align-items:center;';
                        }
                        if ( $block['attrs']['layout']['justifyContent'] == 'right' ) {
                            $inlineStyles .= 'align-items:flex-end;';
                        }
                    }
                } else {
                    $inlineStyles .= 'flex-direction:row;';
                    if ( $block['attrs']['layout']['justifyContent'] == 'left' ) {
                        $inlineStyles .= 'justify-content:flex-start;';
                    }
                    if ( $block['attrs']['layout']['justifyContent'] == 'center' ) {
                        $inlineStyles .= 'justify-content:center;';
                    }
                    if ( $block['attrs']['layout']['justifyContent'] == 'right' ) {
                        $inlineStyles .= 'justify-content:flex-end;';
                    }
                    if ( $block['attrs']['layout']['justifyContent'] == 'space-between' ) {
                        $inlineStyles .= 'justify-content:space-between;';
                    }
                }
            }
        }
        if ( strpos( $block['blockName'], 'core/column' ) !== false ) {
            if ( ! empty( $block['attrs']['align'] ) ) {
                $inlineStyles .= 'align-self:' . $block['attrs']['align'] . ';';
            }
        }

        if ( ! empty( $block['attrs']['minHeight'] ) ) {
            if ( empty( $block['attrs']['minHeightUnit'] ) ) {
                $block['attrs']['minHeightUnit'] = 'px';
            }
            $inlineStyles .= 'min-height:' . $block['attrs']['minHeight'] . $block['attrs']['minHeightUnit'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['dimensions']['minHeight'] ) ) {
            $inlineStyles .= 'min-height:' . $block['attrs']['style']['dimensions']['minHeight']. ';';
        }
        // Spacing (gap, padding, margin)
        if ( ! empty( $block['attrs']['style']['spacing']['blockGap'] ) ) {
    $blockGap = $block['attrs']['style']['spacing']['blockGap'];
    if ( is_array( $blockGap ) ) {
        // Convert array to string, e.g., join with a space or comma.
        $blockGap = implode(' ', $blockGap);
    }
    $inlineStyles .= 'gap:' . $blockGap . ';';
}
        if ( ! empty( $block['attrs']['style']['spacing']['padding']['top'] ) ) {
            $inlineStyles .= 'padding-top:' . $block['attrs']['style']['spacing']['padding']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['spacing']['padding']['right'] ) ) {
            $inlineStyles .= 'padding-right:' . $block['attrs']['style']['spacing']['padding']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['spacing']['padding']['bottom'] ) ) {
            $inlineStyles .= 'padding-bottom:' . $block['attrs']['style']['spacing']['padding']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['spacing']['padding']['left'] ) ) {
            $inlineStyles .= 'padding-left:' . $block['attrs']['style']['spacing']['padding']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['spacing']['margin']['top'] ) ) {
            $inlineStyles .= 'margin-top:' . $block['attrs']['style']['spacing']['margin']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['spacing']['margin']['right'] ) ) {
            $inlineStyles .= 'margin-right:' . $block['attrs']['style']['spacing']['margin']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['spacing']['margin']['bottom'] ) ) {
            $inlineStyles .= 'margin-bottom:' . $block['attrs']['style']['spacing']['margin']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['style']['spacing']['margin']['left'] ) ) {
            $inlineStyles .= 'margin-left:' . $block['attrs']['style']['spacing']['margin']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_position'] ) ) {
            $inlineStyles .= 'position:' . $block['attrs']['desktop_position'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_display'] ) ) {
            $inlineStyles .= 'display:' . $block['attrs']['desktop_display'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_textShadow'] ) ) {
            $inlineStyles .= 'text-shadow:' . $block['attrs']['desktop_textShadow'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_flexWrap'] ) ) {
            $inlineStyles .= 'flex-wrap:' . $block['attrs']['desktop_flexWrap'] . ' !important;';
        }
        if ( ! empty( $block['attrs']['desktop_pos']['top'] ) ) {
            $inlineStyles .= 'top:' . $block['attrs']['desktop_pos']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_pos']['right'] ) ) {
            $inlineStyles .= 'right:' . $block['attrs']['desktop_pos']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_pos']['bottom'] ) ) {
            $inlineStyles .= 'bottom:' . $block['attrs']['desktop_pos']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_pos']['left'] ) ) {
            $inlineStyles .= 'left:' . $block['attrs']['desktop_pos']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['desktop_columns'] ) ) {
            $inlineStyles .= 'grid-template-columns:repeat(' . $block['attrs']['desktop_columns'] . ', minmax(0, 1fr)) !important;';
        }
if ( ! empty( $block['attrs']['desktop_z_index'] ) ) {
    $inlineStyles .= 'z-index:' . $block['attrs']['desktop_z_index'] . ' !important;';
}
if ( ! empty( $block['attrs']['desktop_overflow'] ) ) {
    $inlineStyles .= 'overflow:' . $block['attrs']['desktop_overflow'] . ' !important;';
}
if ( ! empty( $block['attrs']['desktop_zoom'] ) ) {
    $inlineStyles .= 'zoom:' . $block['attrs']['desktop_zoom'] . ' !important;';
}
if ( ! empty( $block['attrs']['desktop_height'] ) ) {
    $inlineStyles .= 'height:' . $block['attrs']['desktop_height'] . ' !important;';
    $inlineStyles .= 'min-height:' . $block['attrs']['desktop_height']  . ';';
}
if ( ! empty( $block['attrs']['desktop_flex_direction'] ) ) {
    $inlineStyles .= 'flex-direction:' . $block['attrs']['desktop_flex_direction'] . ' !important;';
}
if ( ! empty( $block['attrs']['desktop_opacity'] ) ) {
    $inlineStyles .= 'opacity:' . $block['attrs']['desktop_opacity'] . ' !important;';
}
if ( ! empty( $block['attrs']['desktop_white_space'] ) ) {
    $inlineStyles .= 'white-space:' . $block['attrs']['desktop_white_space'] . ' !important;';
}
if ( ! empty( $block['attrs']['desktop_flex_grow'] ) ) {
    $inlineStyles .= 'flex-grow:' . $block['attrs']['desktop_flex_grow'] . ' !important;';
}
if ( ! empty( $block['attrs']['desktop_transition'] ) ) {
    $inlineStyles .= 'transition: all ' . $block['attrs']['desktop_transition'] . 's !important;';
}
if ( ! empty( $block['attrs']['desktop_textAlign'] ) ) {
    $inlineStyles .= 'text-align:' . $block['attrs']['desktop_textAlign'] . ';';
}
 if ( ! empty( $block['attrs']['desktop_justify'] ) ) {
    $inlineStyles .= 'justify-content:' . $block['attrs']['desktop_justify'] . ';';
}
        $inlineStyles .= '}';

        // Tablet media query
        $inlineStyles .= '@media screen and (max-width: 1024px){ #block_' . $block['attrs']['customId'] . '{';
        if ( ! empty( $block['attrs']['font_size_tablet'] ) ) {
            $inlineStyles .= 'font-size:' . $block['attrs']['font_size_tablet'] . ';';
        }
        if (  empty( $block['attrs']['isStackedOnMobile'] ) ) {
            $inlineStyles .= 'flex-wrap:wrap !important;';
        }
        if ( ! empty( $block['attrs']['line_height_tablet'] ) ) {
            $inlineStyles .= 'line-height:' . $block['attrs']['line_height_tablet'] . ';';
        }
        if ( ! empty( $block['attrs']['gap_tablet'] ) ) {
            $inlineStyles .= 'gap:' . $block['attrs']['gap_tablet'] . ';';
        }
        if ( ! empty( $block['attrs']['width_tablet'] ) ) {
            $inlineStyles .= 'width:' . $block['attrs']['width_tablet'] . ';';
        }
        if ( ! empty( $block['attrs']['width_tablet'] ) ) {
            $inlineStyles .= 'flex-basis:' . $block['attrs']['width_tablet'] . ' !important;';
        }
        if ( ! empty( $block['attrs']['order_tablet'] ) ) {
            $inlineStyles .= 'order:' . $block['attrs']['order_tablet'] . ';';
        }
        if ( ! empty( $block['attrs']['height'] ) ) {
            $inlineStyles .= 'height:' . $block['attrs']['height'] . ';';
        }
        if ( ! empty( $block['attrs']['minHeight'] ) ) {
            if ( empty( $block['attrs']['minHeightUnit'] ) ) { $block['attrs']['minHeightUnit'] = 'px'; }
            $inlineStyles .= 'min-height:' . $block['attrs']['minHeight'] . $block['attrs']['minHeightUnit'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_border']['top'] ) ) {
            $inlineStyles .= 'border-top-width:' . $block['attrs']['tablet_border']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_border']['bottom'] ) ) {
            $inlineStyles .= 'border-bottom-width:' . $block['attrs']['tablet_border']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_border']['left'] ) ) {
            $inlineStyles .= 'border-left-width:' . $block['attrs']['tablet_border']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_border']['right'] ) ) {
            $inlineStyles .= 'border-right-width:' . $block['attrs']['tablet_border']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['padding_tablet']['top'] ) ) {
            $inlineStyles .= 'padding-top:' . $block['attrs']['padding_tablet']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['padding_tablet']['right'] ) ) {
            $inlineStyles .= 'padding-right:' . $block['attrs']['padding_tablet']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['padding_tablet']['bottom'] ) ) {
            $inlineStyles .= 'padding-bottom:' . $block['attrs']['padding_tablet']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['padding_tablet']['left'] ) ) {
            $inlineStyles .= 'padding-left:' . $block['attrs']['padding_tablet']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['margin_tablet']['top'] ) ) {
            $inlineStyles .= 'margin-top:' . $block['attrs']['margin_tablet']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['margin_tablet']['right'] ) ) {
            $inlineStyles .= 'margin-right:' . $block['attrs']['margin_tablet']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['margin_tablet']['bottom'] ) ) {
            $inlineStyles .= 'margin-bottom:' . $block['attrs']['margin_tablet']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['margin_tablet']['left'] ) ) {
            $inlineStyles .= 'margin-left:' . $block['attrs']['margin_tablet']['left'] . ';';
        }

        if ( ! empty( $block['attrs']['tablet_position'] ) ) {
            $inlineStyles .= 'position:' . $block['attrs']['tablet_position'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_display'] ) ) {
            $inlineStyles .= 'display:' . $block['attrs']['tablet_display'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_textShadow'] ) ) {
            $inlineStyles .= 'text-shadow:' . $block['attrs']['tablet_textShadow'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_flexWrap'] ) ) {
            $inlineStyles .= 'flex-wrap:' . $block['attrs']['tablet_flexWrap'] . ' !important;';
        }
        if ( ! empty( $block['attrs']['tablet_pos']['top'] ) ) {
            $inlineStyles .= 'top:' . $block['attrs']['tablet_pos']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_pos']['right'] ) ) {
            $inlineStyles .= 'right:' . $block['attrs']['tablet_pos']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_pos']['bottom'] ) ) {
            $inlineStyles .= 'bottom:' . $block['attrs']['tablet_pos']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_pos']['left'] ) ) {
            $inlineStyles .= 'left:' . $block['attrs']['tablet_pos']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['tablet_columns'] ) ) {
            $inlineStyles .= 'grid-template-columns:repeat(' . $block['attrs']['tablet_columns'] . ', minmax(0, 1fr)) !important;';
        }

if ( ! empty( $block['attrs']['tablet_z_index'] ) ) {
    $inlineStyles .= 'z-index:' . $block['attrs']['tablet_z_index'] . ' !important;';
}
if ( ! empty( $block['attrs']['tablet_overflow'] ) ) {
    $inlineStyles .= 'overflow:' . $block['attrs']['tablet_overflow'] . ' !important;';
}
if ( ! empty( $block['attrs']['tablet_zoom'] ) ) {
    $inlineStyles .= 'zoom:' . $block['attrs']['tablet_zoom'] . ' !important;';
}
if ( ! empty( $block['attrs']['tablet_height'] ) ) {
    $inlineStyles .= 'height:' . $block['attrs']['tablet_height'] . ' !important;';
    $inlineStyles .= 'min-height:' . $block['attrs']['tablet_height'] . ';';
}
if ( ! empty( $block['attrs']['tablet_flex_direction'] ) ) {
    $inlineStyles .= 'flex-direction:' . $block['attrs']['tablet_flex_direction'] . ' !important;';
}
if ( ! empty( $block['attrs']['tablet_opacity'] ) ) {
    $inlineStyles .= 'opacity:' . $block['attrs']['tablet_opacity'] . ' !important;';
}
if ( ! empty( $block['attrs']['tablet_white_space'] ) ) {
    $inlineStyles .= 'white-space:' . $block['attrs']['tablet_white_space'] . ' !important;';
}
if ( ! empty( $block['attrs']['tablet_flex_grow'] ) ) {
    $inlineStyles .= 'flex-grow:' . $block['attrs']['tablet_flex_grow'] . ' !important;';
}
if ( ! empty( $block['attrs']['tablet_transition'] ) ) {
    $inlineStyles .= 'transition: all ' . $block['attrs']['tablet_transition'] . 's !important;';
}
if ( ! empty( $block['attrs']['tablet_textAlign'] ) ) {
    $inlineStyles .= 'text-align:' . $block['attrs']['tablet_textAlign'] . ';';
}
 if ( ! empty( $block['attrs']['tablet_justify'] ) ) {
    $inlineStyles .= 'justify-content:' . $block['attrs']['tablet_justify'] . ';';
}
        $inlineStyles .= '} }';

        // Mobile media query
        $inlineStyles .= '@media screen and (max-width: 768px){ #block_' . $block['attrs']['customId'] . '{';
        if ( ! empty( $block['attrs']['font_size_mobile'] ) ) {
            $inlineStyles .= 'font-size:' . $block['attrs']['font_size_mobile'] . ';';
        }
        if ( ! empty( $block['attrs']['line_height_mobile'] ) ) {
            $inlineStyles .= 'line-height:' . $block['attrs']['line_height_mobile'] . ';';
        }
        if ( ! empty( $block['attrs']['gap_mobile'] ) ) {
            $inlineStyles .= 'gap:' . $block['attrs']['gap_mobile'] . ';';
        }
        if ( ! empty( $block['attrs']['width_mobile'] ) ) {
            $inlineStyles .= 'width:' . $block['attrs']['width_mobile'] . ';';
        }
        if ( ! empty( $block['attrs']['width_mobile'] ) ) {
            $inlineStyles .= 'flex-basis:' . $block['attrs']['width_mobile'] . ' !important;';
        }
        if ( ! empty( $block['attrs']['order_mobile'] ) ) {
            $inlineStyles .= 'order:' . $block['attrs']['order_mobile'] . ';';
        }
        if ( ! empty( $block['attrs']['padding_mobile']['top'] ) ) {
            $inlineStyles .= 'padding-top:' . $block['attrs']['padding_mobile']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['padding_mobile']['right'] ) ) {
            $inlineStyles .= 'padding-right:' . $block['attrs']['padding_mobile']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['padding_mobile']['bottom'] ) ) {
            $inlineStyles .= 'padding-bottom:' . $block['attrs']['padding_mobile']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['padding_mobile']['left'] ) ) {
            $inlineStyles .= 'padding-left:' . $block['attrs']['padding_mobile']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_border']['top'] ) ) {
            $inlineStyles .= 'border-top-width:' . $block['attrs']['mobile_border']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_border']['bottom'] ) ) {
            $inlineStyles .= 'border-bottom-width:' . $block['attrs']['mobile_border']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_border']['left'] ) ) {
            $inlineStyles .= 'border-left-width:' . $block['attrs']['mobile_border']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_border']['right'] ) ) {
            $inlineStyles .= 'border-right-width:' . $block['attrs']['mobile_border']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['margin_mobile']['top'] ) ) {
            $inlineStyles .= 'margin-top:' . $block['attrs']['margin_mobile']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['margin_mobile']['right'] ) ) {
            $inlineStyles .= 'margin-right:' . $block['attrs']['margin_mobile']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['margin_mobile']['bottom'] ) ) {
            $inlineStyles .= 'margin-bottom:' . $block['attrs']['margin_mobile']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['margin_mobile']['left'] ) ) {
            $inlineStyles .= 'margin-left:' . $block['attrs']['margin_mobile']['left'] . ';';
        }
        //new
        if ( ! empty( $block['attrs']['mobile_position'] ) ) {
            $inlineStyles .= 'position:' . $block['attrs']['mobile_position'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_display'] ) ) {
            $inlineStyles .= 'display:' . $block['attrs']['mobile_display'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_textShadow'] ) ) {
            $inlineStyles .= 'text-shadow:' . $block['attrs']['mobile_textShadow'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_flexWrap'] ) ) {
            $inlineStyles .= 'flex-wrap:' . $block['attrs']['mobile_flexWrap'] . ' !important;';
        }
        if ( ! empty( $block['attrs']['mobile_pos']['top'] ) ) {
            $inlineStyles .= 'top:' . $block['attrs']['mobile_pos']['top'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_pos']['right'] ) ) {
            $inlineStyles .= 'right:' . $block['attrs']['mobile_pos']['right'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_pos']['bottom'] ) ) {
            $inlineStyles .= 'bottom:' . $block['attrs']['mobile_pos']['bottom'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_pos']['left'] ) ) {
            $inlineStyles .= 'left:' . $block['attrs']['mobile_pos']['left'] . ';';
        }
        if ( ! empty( $block['attrs']['mobile_columns'] ) ) {
            $inlineStyles .= 'grid-template-columns:repeat(' . $block['attrs']['mobile_columns'] . ', minmax(0, 1fr)) !important;';
        }

        
if ( ! empty( $block['attrs']['mobile_z_index'] ) ) {
    $inlineStyles .= 'z-index:' . $block['attrs']['mobile_z_index'] . ' !important;';
}
if ( ! empty( $block['attrs']['mobile_overflow'] ) ) {
    $inlineStyles .= 'overflow:' . $block['attrs']['mobile_overflow'] . ' !important;';
}
if ( ! empty( $block['attrs']['mobile_zoom'] ) ) {
    $inlineStyles .= 'zoom:' . $block['attrs']['mobile_zoom'] . ' !important;';
}
if ( ! empty( $block['attrs']['mobile_height'] ) ) {
    $inlineStyles .= 'height:' . $block['attrs']['mobile_height'] . ' !important;';
    $inlineStyles .= 'min-height:' . $block['attrs']['mobile_height'] . ';';
}
if ( ! empty( $block['attrs']['mobile_flex_direction'] ) ) {
    $inlineStyles .= 'flex-direction:' . $block['attrs']['mobile_flex_direction'] . ' !important;';
}
if ( ! empty( $block['attrs']['mobile_opacity'] ) ) {
    $inlineStyles .= 'opacity:' . $block['attrs']['mobile_opacity'] . ' !important;';
}
if ( ! empty( $block['attrs']['mobile_white_space'] ) ) {
    $inlineStyles .= 'white-space:' . $block['attrs']['mobile_white_space'] . ' !important;';
}
if ( ! empty( $block['attrs']['mobile_flex_grow'] ) ) {
    $inlineStyles .= 'flex-grow:' . $block['attrs']['mobile_flex_grow'] . ' !important;';
}
if ( ! empty( $block['attrs']['mobile_transition'] ) ) {
    $inlineStyles .= 'transition: all ' . $block['attrs']['mobile_transition'] . 's !important;';
}
if ( ! empty( $block['attrs']['mobile_textAlign'] ) ) {
    $inlineStyles .= 'text-align:' . $block['attrs']['mobile_textAlign'] . ';';
}
 if ( ! empty( $block['attrs']['mobile_justify'] ) ) {
    $inlineStyles .= 'justify-content:' . $block['attrs']['mobile_justify'] . ';';
}        
//      mobile_display: { type: "string", default: "" },
//      mobile_textShadow: { type: "string", default: "" },
//      mobile_flexWrap: { type: "string", default: "" },

        $inlineStyles .= '} }';

        if ( ! empty( $block['attrs']['custom_css'] ) ) {
            $inlineStyles .= str_replace( 'this_block', '#block_' . $block['attrs']['customId'], $block['attrs']['custom_css'] );
        }

        return $inlineStyles;
    }
}

function stepfox_block_scripts() {
    wp_reset_postdata();
    global $_wp_current_template_content;
    $page_content = get_the_content();
    $full_content = $_wp_current_template_content . $page_content;

    if ( has_blocks( $full_content ) ) {
        $blocks = parse_blocks( $full_content );
        $all_blocks = search( $blocks, 'blockName' );
        // Get template parts content.
        foreach ( $all_blocks as $block ) {
            $full_content .= get_template_parts_as_content( $block );
        }
        $blocks = parse_blocks( $full_content );
        $all_blocks = search( $blocks, 'blockName' );
        $inline_style = '';
        wp_register_style( 'intellibridge-custom-style', false );
        wp_enqueue_style( 'intellibridge-custom-style' );

        foreach ( $all_blocks as $block ) {
            if ( ( $block['blockName'] === 'core/block' && ! empty( $block['attrs']['ref'] ) ) ||
                ( $block['blockName'] === 'core/navigation' && ! empty( $block['attrs']['ref'] ) ) ) {
                $content = get_post_field( 'post_content', $block['attrs']['ref'] );
                $reusable_blocks = parse_blocks( $content );
                $all_reusable_blocks = search( $reusable_blocks, 'blockName' );
                foreach ( $all_reusable_blocks as $reusable_block ) {
                    $inline_style .= inline_scripts_for_blocks( $reusable_block );
                }
            }

            if ( $block['blockName'] == 'stepfox/intellibridge-query-block' && $block['attrs']['className'] == 'is-style-custom-template-part' ) {
                if ( is_numeric( $block['attrs']['template_part'] ) ) {
                    $template_part = get_block_template( 'intellibridge//' . basename( get_permalink( $block['attrs']['template_part'] ) ), 'wp_template_part' );
                } else {
                    $template_part = get_block_template( 'intellibridge//' . $block['attrs']['template_part'], 'wp_template_part' );
                }
                $inline_style .= inline_scripts_for_blocks( $block );
                $template_part_content = $template_part->content;
                if ( $block['attrs']["post_type"] == 'wp_template_part' && $block['attrs']['source'] == 'manual_selection' ) {
                    $manual_selection = $block['attrs']['manual_selection'];
                    foreach ( $manual_selection as $item ) {
                        if ( is_numeric( $item['toplistitem'] ) ) {
                            $template_part_inner = get_block_template( 'intellibridge//' . basename( get_permalink( $item['toplistitem'] ) ), 'wp_template_part' );
                        } else {
                            $template_part_inner = get_block_template( 'intellibridge//' . $item['toplistitem'], 'wp_template_part' );
                        }
                        $template_part_content .= $template_part_inner->content;
                    }
                }
                $template_blocks = parse_blocks( $template_part_content );
                $all_template_blocks = search( $template_blocks, 'blockName' );
                foreach ( $all_template_blocks as $template_block ) {
                    $inline_style .= inline_scripts_for_blocks( $template_block );
                }
            } elseif ( $block['blockName'] == 'stepfox/intellibridge-query-block' && $block['attrs']['className'] != 'is-style-custom-template-part' ) {
                $inline_style .= inline_scripts_for_blocks( $block );
            } elseif ( $block['blockName'] == 'stepfox/casino-query-block' && $block['attrs']['className'] != 'is-style-custom-template-part' ) {
                $card = str_replace( 'is-style-', '', $block['attrs']['className'] );
                wp_enqueue_style( 'casino-query-block-' . $card, get_template_directory_uri() . '/blocks/casino_query_block/cards/css/card-' . $card . '.css' );
                $inline_style .= inline_scripts_for_blocks( $block );
            } else {
                $inline_style .= inline_scripts_for_blocks( $block );
            }
        }

        wp_register_script( 'myprefix-dummy-js-header', '',);
        wp_enqueue_script( 'myprefix-dummy-js-header' );
        wp_add_inline_script( 'myprefix-dummy-js-header', $inline_style);
    }
}

add_action( 'wp_head', 'stepfox_block_scripts' );

function inline_scripts_for_blocks($block) {
    if(!empty($block['attrs']['custom_js'])) {
        return str_replace('this_block', '#block_' . $block['attrs']['customId'], $block['attrs']['custom_js']);
    }
}
