<?php

namespace KupShop\AutomationConfiguratorBundle\Admin;

use KupShop\AutomationConfiguratorBundle\Util\ConfigurationsRunner;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class AutomationConfigurationTestConfig extends \Window
{
    public function get_vars()
    {
        $vars = parent::get_vars();

        $expressionsRunner = ServiceContainer::getService(ConfigurationsRunner::class);

        $id = getVal('id');

        try {
            $config = json_decode_strict(getVal('config'), true);
        } catch (\JsonException $e) {
            $config = null;
        }

        $rawRestrict = getVal('restrict');

        if ($rawRestrict) {
            $restrict = explode(',', $rawRestrict);
        }

        if ($config !== null) {
            $vars['body']['data']['result'] = $expressionsRunner->runTest($id, $restrict ?? null, $config);
        }

        return $vars;
    }

    public function hasRights($name = null)
    {
        if ($name == self::RIGHT_SAVE) {
            return false;
        }

        return parent::hasRights($name);
    }
}

return AutomationConfigurationTestConfig::class;
