<?php
declare(strict_types=1);

// THIS FILE IS AUTO-GENERATED. DO NOT EDIT MANUALLY.

namespace Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLQueries;

use Automattic\WooCommerce\Api\Queries\Products\GetProduct as GetProductCommand;
use Automattic\WooCommerce\Internal\Api\QueryInfoExtractor;
use Automattic\WooCommerce\Internal\Api\Utils;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Interfaces\Product as ProductInterface;
use Automattic\WooCommerce\Internal\Api\Schema\ResolveInfo;
use Automattic\WooCommerce\Internal\Api\Schema\Type;

class GetProduct {
	public static function get_field_definition(): array {
		return array(
			'type'        => ProductInterface::get(),
			'description' => __( 'Retrieve a single product by ID.', 'woocommerce' ),
			'args'        => array(
				'id' => array(
					'type'        => Type::nonNull( Type::int() ),
					'description' => __( 'The ID of the product to retrieve.', 'woocommerce' ),
				),
			),
			'resolve'     => array( self::class, 'resolve' ),
		);
	}

	public static function resolve( mixed $root, array $args, mixed $context, ResolveInfo $info ): mixed {
		$command = \Automattic\WooCommerce\Api\Container::get( GetProductCommand::class );

		$execute_args = array();
		if ( array_key_exists( 'id', $args ) ) {
			$execute_args['id'] = $args['id'];
		}
		$execute_args['_query_info'] = QueryInfoExtractor::extract_from_info( $info, $args );

		if ( ! Utils::authorize_command(
			$command,
			array(
				'id'             => $execute_args['id'],
				'_preauthorized' => current_user_can( 'read_product' ),
			)
		) ) {
			throw new \Automattic\WooCommerce\Internal\Api\Schema\Error(
				'You do not have permission to perform this action.',
				extensions: array( 'code' => 'UNAUTHORIZED' )
			);
		}

		$result = Utils::execute_command( $command, $execute_args );

		return $result;
	}
}
