Return All Tags from Search Result in Separate List

I am trying to create a list of tags on top of my search results page that returns all tags and tag count for all tags that are included in the search results.

For example, I search for ‘Foo’ and it returns 100 posts all with different tags eg pizza, bred, tomatoes. I would like all the tags with tag count (tag count related to search result) related to my search to show on top of my search results page eg pizza (50), bred (10), tomatoes (5).

I played around with two approaches but can not get this two work.

APPROACH 1 Return all Tags from search results

    function get_query_terms( $taxonomy = 'post_tag' )
{
    $list = array ();

    foreach ( $GLOBALS('wp_query')->posts as $id )
    {
        if ( $terms = get_the_terms( $id, $taxonomy ) )
        {
            foreach ( $terms as $term )
                $list( $term->term_taxonomy_id ) = $term;
        }
    }

    ksort( $list );

    return $list;
}

APPROACH 2

function list_search_tags() {
   $tagTmp = array();
   $tagLinks = '';
  
   // Start The Loop
      while (have_posts()) : the_post();
         $post_tags = get_the_tags();

         foreach ($post_tags as $tag) : $tagID = $tag->term_id;

            if (empty($tagTmp($tagID))) :
               $tagTmp($tagID) = 1;
               $tagURL = get_tag_link($tagID);
               $tagName = $tag->name;
               $tagLinks .= '<li><a class="tag" href="' . $tagURL . '">' . $tagName . '</a></li>';
            endif;
  
      endforeach;
   endwhile;

   echo $tagLinks;}

Thanks 🙂