<?php
declare(strict_types=1);

namespace WP_AUX\Services;

class RestService
{
  /**
   * Remove custom taxonomies used in block editor from post meta sidebar
   */
  public static function removeTaxonomiesFromPostSidebar(
    \WP_REST_Response $response,
    \WP_Taxonomy $taxonomy,
    \WP_REST_Request $request
  ): \WP_REST_Response
  {
    $context = ! empty( $request['context'] ) ? $request['context'] : 'view';

    if( $context === 'edit' && $taxonomy->meta_box_cb === false ){
      $data_response = $response->get_data();
      $data_response['visibility']['show_ui'] = false;
      $response->set_data( $data_response );
    }

    return $response;
  }

  /**
   * Prepare REST actions addressing universal WP REST issues
   */
  public static function prepareHooks(): void
  {
    add_filter( 'rest_prepare_taxonomy', [ self::class, 'removeTaxonomiesFromPostSidebar' ], 10, 3);
  }
}
