<?php

namespace ModernFields\FieldGroups\Locations\Hooks;

use ModernFields\FieldGroups\Entities\FieldGroup;
use ModernFields\FieldGroups\Locations\AbstractPostLocation;
use ModernFields\WordPress\Interfaces\Hooks;

defined('ABSPATH') || exit;

/**
 * PostLocation Class
 *
 * Handles Modern Fields field groups on post types that use the block editor.
 * Meta is read from and written to the core/editor store on the JavaScript side
 * and persisted through Gutenberg's REST save flow, so this strategy only needs
 * to render metaboxes and enqueue the editor assets — there is no save_post
 * handler (see ClassicPostLocation for the classic-editor counterpart).
 *
 * @since 1.0.0
 */
class PostLocation extends AbstractPostLocation implements Hooks
{
  /**
   * Only handle post types whose edit screen is the block editor.
   *
   * @since 1.1.0
   * @param string $post_type The post type being edited.
   * @return bool
   */
  protected function shouldHandlePostType(string $post_type): bool
  {
    return use_block_editor_for_post_type($post_type);
  }

  /**
   * Register only metabox-positioned groups; inspector groups are mounted in
   * the editor sidebar through registerPlugin on the JavaScript side.
   *
   * @since 1.1.0
   * @param FieldGroup $field_group The field group to place.
   * @return string|null
   */
  protected function getMetaboxContext(FieldGroup $field_group): ?string
  {
    return $field_group->getPosition() === 'metabox' ? 'normal' : null;
  }

  /**
   * The block-editor post fields bundle.
   *
   * @since 1.1.0
   * @return string
   */
  protected function getBundleName(): string
  {
    return 'post-fields';
  }
}
