<?php
declare(strict_types=1);

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

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

use Automattic\WooCommerce\Api\Mutations\Products\CreateProduct as CreateProductCommand;
use Automattic\WooCommerce\Api\Infrastructure\QueryInfoExtractor;
use Automattic\WooCommerce\Api\Infrastructure\ResolverHelpers;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Interfaces\Product as ProductInterface;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Input\CreateProduct as CreateProductInput;
use Automattic\WooCommerce\Api\Infrastructure\Schema\ResolveInfo;
use Automattic\WooCommerce\Api\Infrastructure\Schema\Type;

class CreateProduct {
	public static function get_field_definition(): array {
		return array(
			'type'          => Type::nonNull( ProductInterface::get() ),
			'description'   => __( 'Create a new product.', 'woocommerce' ),
			'authorization' => array(
				array(
					'attribute' => 'RequiredCapability',
					'args'      => array(
						0 => 'edit_products',
					),
				),
			),
			'args'          => array(
				'input' => array(
					'type'        => Type::nonNull( CreateProductInput::get() ),
					'description' => __( 'Data for the new product.', 'woocommerce' ),
				),
			),
			'resolve'       => array( self::class, 'resolve' ),
		);
	}

	public static function resolve( mixed $root, array $args, mixed $context, ResolveInfo $info ): mixed {
		// Standalone authorization gate: no authorize() method on the command,
		// so the autodiscovered authorization attributes are the sole guard.
		if ( ! self::compute_preauthorized( $context['principal'] ) ) {
			throw ResolverHelpers::build_authorization_error( $context['principal'] );
		}

		// Publish the root query's metadata so downstream field-level
		// authorization gates can read it via `$_metadata['query']`.
		// $context is an ArrayObject (see GraphQLController::process_request())
		// so the mutation propagates to nested resolvers.
		$context['_query_metadata'] = array();

		$command = \Automattic\WooCommerce\Api\Infrastructure\ClassResolver::resolve_class( CreateProductCommand::class );

		$execute_args = array();
		if ( array_key_exists( 'input', $args ) ) {
			$execute_args['input'] = self::convert_create_product_input( $args['input'] );
		}

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

		return $result;
	}

	/**
	 * Compute the value `_preauthorized` would carry for a given principal —
	 * the AND of the autodiscovered authorization attributes' authorize()
	 * outcomes on this command. Single source of truth for both the resolver's
	 * own gates and external (code-API) callers asking about authorization
	 * without going through GraphQL execution.
	 *
	 * Returns true vacuously when the command has no authorization attributes
	 * (in that case authorize() on the command is the sole guard, and that
	 * method should be consulted instead).
	 */
	public static function compute_preauthorized( \Automattic\WooCommerce\Api\Infrastructure\Principal $principal ): bool {
		return ( new \Automattic\WooCommerce\Api\Attributes\RequiredCapability( 'edit_products' ) )->authorize( $principal );
	}

	private static function convert_dimensions_input( array $data ): \Automattic\WooCommerce\Api\InputTypes\Products\DimensionsInput {
		$input = new \Automattic\WooCommerce\Api\InputTypes\Products\DimensionsInput();

		if ( array_key_exists( 'length', $data ) ) {
			$input->mark_provided( 'length' );
				$input->length = $data['length'];
		}
		if ( array_key_exists( 'width', $data ) ) {
			$input->mark_provided( 'width' );
					$input->width = $data['width'];
		}
		if ( array_key_exists( 'height', $data ) ) {
			$input->mark_provided( 'height' );
					$input->height = $data['height'];
		}
		if ( array_key_exists( 'weight', $data ) ) {
			$input->mark_provided( 'weight' );
					$input->weight = $data['weight'];
		}

		return $input;
	}

	private static function convert_create_product_input( array $data ): \Automattic\WooCommerce\Api\InputTypes\Products\CreateProductInput {
		$input = new \Automattic\WooCommerce\Api\InputTypes\Products\CreateProductInput();

		if ( array_key_exists( 'name', $data ) ) {
			$input->mark_provided( 'name' );
				$input->name = $data['name'];
		}
		if ( array_key_exists( 'slug', $data ) ) {
			$input->mark_provided( 'slug' );
					$input->slug = $data['slug'];
		}
		if ( array_key_exists( 'sku', $data ) ) {
			$input->mark_provided( 'sku' );
					$input->sku = $data['sku'];
		}
		if ( array_key_exists( 'description', $data ) ) {
			$input->mark_provided( 'description' );
					$input->description = $data['description'];
		}
		if ( array_key_exists( 'short_description', $data ) ) {
			$input->mark_provided( 'short_description' );
					$input->short_description = $data['short_description'];
		}
		if ( array_key_exists( 'status', $data ) ) {
			$input->mark_provided( 'status' );
					$input->status = $data['status'];
		}
		if ( array_key_exists( 'product_type', $data ) ) {
			$input->mark_provided( 'product_type' );
					$input->product_type = $data['product_type'];
		}
		if ( array_key_exists( 'regular_price', $data ) ) {
			$input->mark_provided( 'regular_price' );
					$input->regular_price = $data['regular_price'];
		}
		if ( array_key_exists( 'sale_price', $data ) ) {
			$input->mark_provided( 'sale_price' );
					$input->sale_price = $data['sale_price'];
		}
		if ( array_key_exists( 'manage_stock', $data ) ) {
			$input->mark_provided( 'manage_stock' );
					$input->manage_stock = $data['manage_stock'];
		}
		if ( array_key_exists( 'stock_quantity', $data ) ) {
			$input->mark_provided( 'stock_quantity' );
					$input->stock_quantity = $data['stock_quantity'];
		}
		if ( array_key_exists( 'dimensions', $data ) ) {
			$input->mark_provided( 'dimensions' );
					$input->dimensions = null !== $data['dimensions'] ? self::convert_dimensions_input( $data['dimensions'] ) : null;
		}

		return $input;
	}
}
