<?php

namespace Signteer\Core\Action\Setting;

use Signteer\Core\Action\ActionResult;
// use Signteer\Core\Event\EventBus;
// use Signteer\Core\Event\Setting\SettingsUpdated;
use Signteer\Core\Repo\SettingRepo;

final class UpdateSettings
{
    public $repo = SettingRepo::class;
    // public $eventBus = EventBus::class;

    public function __invoke(array $values): ActionResult
    {
        $ret = new ActionResult('__Update settings__');

        $newValues = $oldValues = [];
        foreach ($values as $k => $v) {
            $v0 = $this->repo->get($k);
            if ($v != $v0) {
                $newValues[$k] = $v;
                $oldValues[$k] = $v0;
            }
        }

        if ($newValues) {
            foreach ($newValues as $k => $v) {
                $this->repo->set($k, $v);
            }
            // $this->eventBus->emit(new SettingsUpdated($oldValues, $newValues));
        }

        return $ret;
    }
}
