<?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\Coupons\UpdateCoupon as UpdateCouponCommand;
use Automattic\WooCommerce\Api\Infrastructure\QueryInfoExtractor;
use Automattic\WooCommerce\Api\Infrastructure\ResolverHelpers;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Output\Coupon as CouponType;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Input\UpdateCoupon as UpdateCouponInput;
use Automattic\WooCommerce\Api\Infrastructure\Schema\ResolveInfo;
use Automattic\WooCommerce\Api\Infrastructure\Schema\Type;

class UpdateCoupon {
	public static function get_field_definition(): array {
		return array(
			'type'          => Type::nonNull( CouponType::get() ),
			'description'   => __( 'Update an existing coupon.', 'woocommerce' ),
			'authorization' => array(
				array(
					'attribute' => 'RequiredCapability',
					'args'      => array(
						0 => 'manage_woocommerce',
					),
				),
			),
			'args'          => array(
				'input' => array(
					'type'        => Type::nonNull( UpdateCouponInput::get() ),
					'description' => __( 'The fields to update.', '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( UpdateCouponCommand::class );

		$execute_args = array();
		if ( array_key_exists( 'input', $args ) ) {
			$execute_args['input'] = self::convert_update_coupon_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( 'manage_woocommerce' ) )->authorize( $principal );
	}

	private static function convert_update_coupon_input( array $data ): \Automattic\WooCommerce\Api\InputTypes\Coupons\UpdateCouponInput {
		$input = new \Automattic\WooCommerce\Api\InputTypes\Coupons\UpdateCouponInput();

		if ( array_key_exists( 'id', $data ) ) {
			$input->mark_provided( 'id' );
				$input->id = $data['id'];
		}
		if ( array_key_exists( 'code', $data ) ) {
			$input->mark_provided( 'code' );
					$input->code = $data['code'];
		}
		if ( array_key_exists( 'description', $data ) ) {
			$input->mark_provided( 'description' );
					$input->description = $data['description'];
		}
		if ( array_key_exists( 'discount_type', $data ) ) {
			$input->mark_provided( 'discount_type' );
					$input->discount_type = $data['discount_type'];
		}
		if ( array_key_exists( 'amount', $data ) ) {
			$input->mark_provided( 'amount' );
					$input->amount = $data['amount'];
		}
		if ( array_key_exists( 'status', $data ) ) {
			$input->mark_provided( 'status' );
					$input->status = $data['status'];
		}
		if ( array_key_exists( 'date_expires', $data ) ) {
			$input->mark_provided( 'date_expires' );
					$input->date_expires = $data['date_expires'];
		}
		if ( array_key_exists( 'individual_use', $data ) ) {
			$input->mark_provided( 'individual_use' );
					$input->individual_use = $data['individual_use'];
		}
		if ( array_key_exists( 'product_ids', $data ) ) {
			$input->mark_provided( 'product_ids' );
					$input->product_ids = $data['product_ids'];
		}
		if ( array_key_exists( 'excluded_product_ids', $data ) ) {
			$input->mark_provided( 'excluded_product_ids' );
					$input->excluded_product_ids = $data['excluded_product_ids'];
		}
		if ( array_key_exists( 'usage_limit', $data ) ) {
			$input->mark_provided( 'usage_limit' );
					$input->usage_limit = $data['usage_limit'];
		}
		if ( array_key_exists( 'usage_limit_per_user', $data ) ) {
			$input->mark_provided( 'usage_limit_per_user' );
					$input->usage_limit_per_user = $data['usage_limit_per_user'];
		}
		if ( array_key_exists( 'limit_usage_to_x_items', $data ) ) {
			$input->mark_provided( 'limit_usage_to_x_items' );
					$input->limit_usage_to_x_items = $data['limit_usage_to_x_items'];
		}
		if ( array_key_exists( 'free_shipping', $data ) ) {
			$input->mark_provided( 'free_shipping' );
					$input->free_shipping = $data['free_shipping'];
		}
		if ( array_key_exists( 'product_categories', $data ) ) {
			$input->mark_provided( 'product_categories' );
					$input->product_categories = $data['product_categories'];
		}
		if ( array_key_exists( 'excluded_product_categories', $data ) ) {
			$input->mark_provided( 'excluded_product_categories' );
					$input->excluded_product_categories = $data['excluded_product_categories'];
		}
		if ( array_key_exists( 'exclude_sale_items', $data ) ) {
			$input->mark_provided( 'exclude_sale_items' );
					$input->exclude_sale_items = $data['exclude_sale_items'];
		}
		if ( array_key_exists( 'minimum_amount', $data ) ) {
			$input->mark_provided( 'minimum_amount' );
					$input->minimum_amount = $data['minimum_amount'];
		}
		if ( array_key_exists( 'maximum_amount', $data ) ) {
			$input->mark_provided( 'maximum_amount' );
					$input->maximum_amount = $data['maximum_amount'];
		}
		if ( array_key_exists( 'email_restrictions', $data ) ) {
			$input->mark_provided( 'email_restrictions' );
					$input->email_restrictions = $data['email_restrictions'];
		}

		return $input;
	}
}
