<?php

use ModernFields\FieldGroups\Entities\FieldGroup;

defined('ABSPATH') || exit;

# Global get_field() and the_field() functions with conflict detection
if (!function_exists('get_field') && !defined('ACF_VERSION') && get_option('modern_fields_shorthand_functions', false)) {
  /**
   * Get a Modern Fields field value
   * 
   * This function retrieves a field value in WordPress templates.
   * It auto-detects the context (post/user/attachment) but allows explicit override.
   * 
   * @since 1.0.0
   * @param string $field_name The name of the field to retrieve
   * @param mixed $context The context of the field to retrieve (id, taxonomy, option)
   * @return mixed The field value or null if not found
   */
  function get_field(string $field_name, mixed $context = null): mixed
  {
    return ModernFields()->fields->getField($field_name, $context);
  }
}

if (!function_exists('the_field') && !defined('ACF_VERSION') && get_option('modern_fields_shorthand_functions', false)) {
  /**
   * Display a Modern Fields field value
   * 
   * This function displays a field value in WordPress templates.
   * It auto-detects the context (post/user/attachment) but allows explicit override.
   * 
   * @since 1.0.0
   * @param string $field_name The name of the field to display
   * @param mixed $context The context of the field to display (id, taxonomy, option)
   * @return void
   */
  function the_field(string $field_name, mixed $context = null): void
  {
    ModernFields()->fields->theField($field_name, $context);
  }
}

if (!function_exists('get_field_group') && get_option('modern_fields_shorthand_functions', false)) {
  /**
   * Get a Modern Fields field group
   * 
   * This function retrieves a field group in WordPress templates.
   * 
   * @since 1.0.0
   * @param string|int $id The ID of the field group to retrieve
   * @return FieldGroup|null The field group object if found, null otherwise.
   */
  function get_field_group(string|int $id): ?FieldGroup
  {
    return ModernFields()->fieldGroups->getFieldGroup((int) $id);
  }
}
