<?php

/*
 * This file is part of the Fancy Bricks package.
 *
 * (c) Joshua Gugun Siagian <suabahasa@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare (strict_types=1);
namespace Fancy\Animations\Api\Admin;

use Fancy\Animations\Api\AbstractApi;
use Fancy\Animations\Api\ApiInterface;
use _FancyAnimations\FANCY_ANIMATIONS;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
/**
 * @since 1.0.0
 */
class Dashboard extends AbstractApi implements ApiInterface
{
    public function __construct()
    {
    }
    public function get_prefix() : string
    {
        return 'admin/dashboard';
    }
    public function register_custom_endpoints() : void
    {
        \register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/index', ['methods' => WP_REST_Server::READABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->index($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]);
        \register_rest_route(self::API_NAMESPACE, $this->get_prefix() . '/store', ['methods' => WP_REST_Server::CREATABLE, 'callback' => fn(\WP_REST_Request $wprestRequest): \WP_REST_Response => $this->store($wprestRequest), 'permission_callback' => fn(\WP_REST_Request $wprestRequest): bool => $this->permission_callback($wprestRequest)]);
    }
    public function index(WP_REST_Request $wprestRequest) : WP_REST_Response
    {
        $options = \json_decode(\get_option(FANCY_ANIMATIONS::WP_OPTION . '_options', \json_encode((object) FANCY_ANIMATIONS::DEFAULT_SETTINGS, \JSON_THROW_ON_ERROR)), null, 512, \JSON_THROW_ON_ERROR);
        $options = \apply_filters('f!fancy/animations/api/admin/dashboard:index', $options);
        return new WP_REST_Response(['options' => $options]);
    }
    public function store(WP_REST_Request $wprestRequest) : WP_REST_Response
    {
        $payload = $wprestRequest->get_json_params();
        $options = $payload['options'];
        if (empty($options)) {
            $options = (object) $options;
        }
        $options = \apply_filters('f!fancy/animations/api/admin/dashboard:store.before', $options);
        \update_option(FANCY_ANIMATIONS::WP_OPTION . '_options', \json_encode($options, \JSON_THROW_ON_ERROR));
        \do_action('a!fancy/animations/api/admin/dashboard:store.after', $options);
        return $this->index($wprestRequest);
    }
    private function permission_callback(WP_REST_Request $wprestRequest) : bool
    {
        return \wp_verify_nonce($wprestRequest->get_header('X-WP-Nonce'), 'wp_rest') && \current_user_can('manage_options');
    }
}
