<?php

namespace WHMCS\Module\Addon\creatia_button\Client;

/**
 * Client Area Dispatch Handler
 */
class Dispatcher {

    /**
     * Actions reachable from a request.
     *
     * A whitelist rather than is_callable() against the controller: the action comes
     * straight from the query string, so anything callable would otherwise become a
     * public endpoint the moment somebody adds a helper method.
     */
    private const ACTIONS = ['index', 'setup'];

    /**
     * Dispatch request.
     *
     * @param string $action
     * @param array $parameters
     *
     * @return array
     */
    public function dispatch($action, $parameters)
    {
        if (!(new \WHMCS\Authentication\CurrentUser)->client()) {
            return [
                'pagetitle'    => 'Creatia Setup',
                'breadcrumb'   => [ 'index.php?m=creatia_button&action=index' => 'Creatia Setup' ],
                'templatefile' => 'required',
                'requirelogin' => true,
                'vars'         => [],
            ];
        }

        if (!is_string($action) || !in_array($action, self::ACTIONS, true)) {
            $action = 'index';
        }

        return (new Controller())->$action($parameters);
    }
}
