<?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\ListProducts as ListProductsCommand;
use Automattic\WooCommerce\Internal\Api\QueryInfoExtractor;
use Automattic\WooCommerce\Internal\Api\Utils;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Pagination\ProductConnection as ProductConnectionType;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Enums\ProductStatus as ProductStatusType;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Enums\StockStatus as StockStatusType;
use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Enums\ProductType as ProductTypeType;
use Automattic\WooCommerce\Internal\Api\Schema\ResolveInfo;
use Automattic\WooCommerce\Internal\Api\Schema\Type;

class ListProducts {
	public static function get_field_definition(): array {
		return array(
			'type'        => Type::nonNull( ProductConnectionType::get() ),
			'description' => __( 'List products with cursor-based pagination and optional filtering.', 'woocommerce' ),
			'args'        => array(
				'first'        => array(
					'type'         => Type::int(),
					'description'  => __( 'Return the first N results. Must be between 0 and 100.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'last'         => array(
					'type'         => Type::int(),
					'description'  => __( 'Return the last N results. Must be between 0 and 100.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'after'        => array(
					'type'         => Type::string(),
					'description'  => __( 'Return results after this cursor.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'before'       => array(
					'type'         => Type::string(),
					'description'  => __( 'Return results before this cursor.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'status'       => array(
					'type'         => ProductStatusType::get(),
					'description'  => __( 'Filter by product status.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'stock_status' => array(
					'type'         => StockStatusType::get(),
					'description'  => __( 'Filter by stock status.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'search'       => array(
					'type'         => Type::string(),
					'description'  => __( 'Search products by keyword.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'product_type' => array(
					'type'         => ProductTypeType::get(),
					'description'  => __( 'Filter by product type.', 'woocommerce' ),
					'defaultValue' => null,
				),
			),
			'complexity'  => Utils::complexity_from_pagination( ... ),
			'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' );
		Utils::check_current_user_can( 'edit_products' );

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

		$execute_args               = array();
		$execute_args['pagination'] = Utils::create_pagination_params( $args );
		$execute_args['filters']    = Utils::create_input(
			fn() => new \Automattic\WooCommerce\Api\InputTypes\Products\ProductFilterInput(
				status: $args['status'],
				stock_status: $args['stock_status'],
				search: $args['search'] ?? null,
			)
		);
		if ( array_key_exists( 'product_type', $args ) ) {
			$execute_args['product_type'] = $args['product_type'];
		}
		$execute_args['_query_info'] = QueryInfoExtractor::extract_from_info( $info, $args );

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

		return $result;
	}
}
