<?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\Internal\Api\QueryInfoExtractor;
use Automattic\WooCommerce\Internal\Api\Utils;
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\Internal\Api\Schema\ResolveInfo;
use Automattic\WooCommerce\Internal\Api\Schema\Type;

class UpdateCoupon {
	public static function get_field_definition(): array {
		return array(
			'type'        => Type::nonNull( CouponType::get() ),
			'description' => __( 'Update an existing coupon.', '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 {
		Utils::check_current_user_can( 'manage_woocommerce' );

		$command = \Automattic\WooCommerce\Api\Container::get( UpdateCouponCommand::class );

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

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

		return $result;
	}

	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;
	}
}
