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

namespace ZOOlanders\YOOessentials\Config;

class ConfigUpdater
{
    protected string $version;

    protected array $updates = [];

    protected array $globals = [];

    /**
     * Constructor.
     */
    public function __construct(string $version)
    {
        $this->version = $version;
    }

    /**
     * Transform callback.
     */
    public function __invoke($config)
    {
        $version = empty($config['version']) ? '1.0.0' : $config['version'];

        // check config version
        if (version_compare($version, $this->version, '>=')) {
            return $config;
        }

        $sha = sha1(json_encode($config));

        // apply update callbacks
        foreach ($this->resolveUpdates($version) as $update) {
            $config = $update($config);
        }

        $config['version'] = $this->version;

        // give a hint to customizer there was an update
        $config['updated'] = $sha !== sha1(json_encode($config));

        return $config;
    }

    /**
     * Adds global updates.
     */
    public function addGlobals(array $updates): self
    {
        $this->globals[] = $updates['config'] ?? [];

        return $this;
    }

    /**
     * Resolves updates for a type and current version.
     */
    protected function resolveUpdates(string $version): array
    {
        if (isset($this->updates[$version])) {
            return $this->updates[$version];
        }

        $updates = $this->globals;

        $resolved = [];

        foreach ($updates as $update) {
            foreach ($update as $ver => $func) {
                if (version_compare($ver, $version, '>=') && is_callable($func)) {
                    $resolved[$ver][] = $func;
                }
            }
        }

        uksort($resolved, 'version_compare');

        return $this->updates[$version] = $resolved ? array_merge(...array_values($resolved)) : [];
    }
}
