<?php

namespace RebelCode\Spotlight\Instagram\Pro\Engine\Aggregation;

use RebelCode\Iris\Aggregation\ItemFeed;
use RebelCode\Iris\Aggregation\ItemProcessor;
use RebelCode\Iris\Item;
use RebelCode\Spotlight\Instagram\Engine\MediaItem;

/**
 * Filters media according to the feed's selected media type.
 *
 * @since 0.5
 */
class MediaTypeProcessor implements ItemProcessor
{
    /**
     * @inheritDoc
     *
     * @since 0.5
     */
    public function process(array &$mediaList, ItemFeed $feed)
    {
        // Get the media type option from the feed's options
        $typeOption = $feed->getOption('mediaType');

        // Do nothing if the option is set to "all"
        if ($typeOption === 'all') {
            return;
        }

        $mediaList = array_filter($mediaList, function (Item $item) use ($feed, $typeOption) {
            $isVideo = $item->data[MediaItem::MEDIA_TYPE] === 'VIDEO';

            return (
                ($typeOption === 'photos' && !$isVideo) ||
                ($typeOption === 'videos' && $isVideo)
            );
        });
    }
}
