<?php  

namespace TSNV_SEO;

add_action('after_setup_theme', function(){
  if(function_exists('pll_register_string')){
    pll_register_string('breadcrumbs-home', 'Home', 'TSNV SEO Plugin');
  }
});

add_action('template_redirect', function () {
  $delete_microdata = get_option('options_'.TSNV_PREFIX.'delete_microdata');

  if ($delete_microdata === '0') {
    return;
  }

  ob_start(__NAMESPACE__ .'\\remove_microdata_attributes');
});

function remove_microdata_attributes($html) {
  // Remove <meta itemprop="..."> entirely
  $html = preg_replace('/<meta[^>]+itemprop\s*=\s*["\'][^"\']+["\'][^>]*\/?>/i', '', $html);

  // Remove itemscope, itemtype, itemprop attributes from all other tags
  $html = preg_replace('/\s+(itemscope|itemtype|itemprop)(\s*=\s*("|\')[^"\']*\3)?/i', '', $html);

  // Remove all <script type="application/ld+json"> without data-tsnv
  $html = preg_replace_callback(
    '#<script[^>]*type=["\']application/ld\+json["\'][^>]*>.*?</script>#is',
    function ($matches) {
      return (stripos($matches[0], 'data-tsnv') !== false) ? $matches[0] : '';
    },
    $html
  );

  return $html;
}

function render_game_schema(): void {
  $group = get_field(TSNV_PREFIX.'game_schema', 'option');

  if(!empty($group) && $group['show_schema'] === false) {
  	return;
  }

  if (is_array($group)) {
    (new GameSchema($group))->render();
  }
}

function render_breadcrumb_schema(): void {
	$group = get_field(TSNV_PREFIX.'breadcrumb_schema', 'option');

	if(!empty($group) && $group['show_schema'] === false) {
    return;
  }

  $current_lang = function_exists('pll_current_language') ? pll_current_language() : 'en';
  $breadcrumbs = [
    [
      'name' => function_exists('pll__') ? pll__('Home') : 'Home',
      'url'  => function_exists('pll_home_url') ? pll_home_url($current_lang) : get_home_url(),
      'position' => 1
    ],
    [
      'name' => get_the_title(),
      'url'  => get_permalink(),
      'position' => 2
    ]
  ];
  (new BreadcrumbSchema($breadcrumbs))->render();
}

function render_faq_schema(): void {
  $group = get_field(TSNV_PREFIX.'faq_schema', 'option');

  if(!empty($group) && $group['show_schema'] === false) {
    return;
  }

  $fag_block_names = ['block_faq', 'faq-items', 'faq_section'];

  foreach ($fag_block_names as $block_name) {
    $block = get_field($block_name);

    if (
      isset($block['items'][0]['title']) 
      && isset($block['items'][0]['text'])
    ) {
      $faq_items = array_map(fn($item) => [
        'question' => $item['title'],
        'answer' => $item['text']
      ], $block['items']);

      (new FAQSchema($faq_items))->render();

      break;
    }else if(
      isset($block[0]['faq-question']) 
      && isset($block[0]['faq-answer'])
    ){
      $faq_items = array_map(fn($item) => [
        'question' => $item['faq-question'],
        'answer' => $item['faq-answer']
      ], $block);

      (new FAQSchema($faq_items))->render();

      break;
    }else if(
      isset($block[0]['question']) 
      && isset($block[0]['answer'])
    ){
      $faq_items = array_map(fn($item) => [
        'question' => $item['question'],
        'answer' => $item['answer']
      ], $block);

      (new FAQSchema($faq_items))->render();

      break;
    }else if(
      isset($block['faq'][0]['title']) 
      && isset($block['faq'][0]['text'])
    ){
      $faq_items = array_map(fn($item) => [
        'question' => $item['title'],
        'answer' => $item['text']
      ], $block['faq']);

      (new FAQSchema($faq_items))->render();

      break;
    }
  }
}

function render_review_schema(): void {
  $group = get_field(TSNV_PREFIX.'review_schema', 'option');

  if(empty($group) || !$group['show_schema']){
    return;
  }

  $review_block_names = ['reviews'];

  foreach ($review_block_names as $block_name) {
    $block = get_field($block_name);

    if(isset($block[0]['text'])){
      $data = array(
        'item_name' => $group['name']
      );

      $data['reviews'] = array_map(fn($item) => [
        'author' => $item['name'],
        'review_body' => $item['text'],
        'rating_value' => $item['rating']
      ], $block);

      (new ReviewSchema($data))->render();

      break;
    }
  }
}