<?php

namespace Lucent\Hook;

class HookService
{
    protected array $hooks = [];

    public function execute(string $name, mixed $params): mixed
    {
        if (empty($this->hooks[$name])) {
            return $params;
        }

        return array_reduce($this->hooks[$name], fn($result, $hook) => $hook($result), $params);
    }

    public function register(string $name, callable $callback, int $priority = 0): void
    {
        while (isset($this->hooks[$name][$priority])) {
            $priority = $priority + 1;
        }
        $this->hooks[$name][$priority] = $callback;
        ksort($this->hooks[$name]);
    }
}