<?php
/**
 * @package   Essentials YOOtheme Pro 2.3.4 build 0217.0955
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials\Layout\Listener;

use YOOtheme\Arr;
use YOOtheme\Event;
use ZOOlanders\YOOessentials\Layout\LayoutManager;
use ZOOlanders\YOOessentials\Util;

class ValidateLibraries
{
    public function load($values): array
    {
        $key = LayoutManager::LIBRARIES_CONFIG_KEY;
        $libraries = Arr::get($values, $key, []);

        $libraries = Util\Arr::removeDuplicates($libraries);

        if (count(Arr::get($values, $key, [])) !== count($libraries)) {
            Event::emit('yooessentials.error', [
                'addon' => 'source',
                'task' => 'load-config',
                'error' => 'Essentials config contains duplicated libraries.',
            ]);
        }

        Arr::set($values, $key, $libraries);

        return $values;
    }
}
