<?php
/**
 * @package DJ-Catalog2-Yootheme
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

namespace DJExtensions\DJCatalog2\YOOtheme\Builder\Source\Type\Item;
defined('_JEXEC') or die;
class ItemLabelsQueryType
{
	/**
	 * @return array
	 */
	public static function config()
	{
		return [

			'fields' => [
				'itemLabels' => [
					'type' => [
						'listOf' => 'ItemLabels',
					],
					'args' => [
						'offset' => [
							'type' => 'Int',
						],
						'limit' => [
							'type' => 'Int',
						],
					],
					'extensions' => [
						'call' => __CLASS__ . '::resolve',
					],
					'metadata' => [
						'label' => "Product's Labels",
						'group' => 'DJ-Catalog2',
						'fields' => [
							'_offset' => [
								'description' => 'Set the starting point and limit the number of labels.',
								'type' => 'grid',
								'width' => '1-2',
								'fields' => [
									'offset' => [
										'label' => 'Start',
										'type' => 'number',
										'default' => 0,
										'modifier' => 1,
										'attrs' => [
											'min' => 1,
											'required' => true,
										],
									],
									'limit' => [
										'label' => 'Quantity',
										'type' => 'limit',
										'attrs' => [
											'placeholder' => 'No limit',
											'min' => 0,
										],
									],
								],
							],
						],
					],
				],
			],
		];
	}

	public static function resolve($root, array $args)
	{
		$args += [
			'offset' => 0,
			'limit' => null,
		];

		if (isset($root['item']->_labels)) {
			$item = $root['item'];
			$labels = $item->_labels;

			if ($args['offset'] || $args['limit']) {
				$labels = array_slice($labels, (int)$args['offset'], (int)$args['limit'] ?: null);
			}

			return $labels;
		}
		return;
	}
}
