<?php
//add_filter(
//        'pre_render_block',
//        function( $pre_render, $parsed_block ) {
//                var_dump( 'pre_render_block', $pre_render, $parsed_block );
//        },
//        10,
//        2
//);

//add_filter(
//        'render_block_data',
//        function( $parsed_block, $source_block ) {
////var_dump($source_block);
//                if ( $source_block['blockName'] == 'stepfox/supermenu') {
//                var_dump( 'render_block_data', $parsed_block, $source_block );
//
//                }
//
//        },
//        10,
//        2
//);

//add_filter(
//        'render_block_context',
//        function( $context, $parsed_block ) {
//                var_dump( 'render_block_context', $context, $parsed_block );
//        },
//        10,
//        2
//);
add_action( 'wp_loaded', function() {

//WP_Block_Supports::get_instance()->register(
//	'align',
//	array(
//		'register_attribute' => 'wp_register_alignment_support',
//		'apply'              => 'wp_apply_alignment_support',
//	)
//);
                $registered_blocks =  WP_Block_Supports::get_instance();
                //var_dump( $registered_blocks );


}, 100);


add_action( 'wp_loaded', function() {

	$registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();

	foreach( $registered_blocks as $name => $block ) {
if ($block->name === 'core/navigation'){
   //var_dump($block);
// $block->context['showSubmenuIcon'] = true;

}
        if ($block->name == 'stepfox/intellibridge-query-block' || $block->name == 'stepfox/metafield-block' || $block->name == 'stepfox/supermenu') {

                $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" => ""];
                $block->attributes['desktop_margin'] = ["type" => "object", "default" => ""];
                $block->attributes['desktop_border'] = ["type" => "object", "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" => ""];
                $block->attributes['tablet_margin'] = ["type" => "object", "default" => ""];
                $block->attributes['tablet_border'] = ["type" => "object", "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" => ""];
                $block->attributes['mobile_margin'] = ["type" => "object", "default" => ""];
                $block->attributes['mobile_border'] = ["type" => "object", "default" => ""];
                $block->attributes['id'] = ["type" => "string", "default" => "stepfox-not-set-id"];
                $block->attributes['custom_css'] = ["type" => "string", "default" => ""];

        }
    }

}, 100);




function wrap_group_and_columns($block_content, $block)
{


    if ($block['blockName'] == 'core/navigation-submenu'){
       $block_content = preg_replace('/<svg[^>]*>.*?<\/svg>/i', '<svg width="8" height="14" viewBox="0 0 8 14" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1 13L7 7L1 1" stroke="#1f1f24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
', $block_content);
    }


if(!empty($block['attrs']['id'])){
    $content = preg_replace('(>+)', ' id="block_'.$block['attrs']['id'].'" > ', $block_content, 1);
    $content = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $content, 1);
    return $content;
}

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

 }



  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;
// var_dump(esc_attr($_wp_current_template_content));
    $page_content = get_the_content();

    $full_content = $_wp_current_template_content.$page_content;

//var_dump($full_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' );
          }
          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;
}
