<?php

/**
 * Model for rendering a tool in the admin sync page.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync\Models;

use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstract_Shared_Base;
defined('ABSPATH') || exit;
/**
 * Class Admin_Sync_Custom_Tool_Model
 */
readonly class Admin_Sync_Tool_Model
{
    /**
     * Key for the ajax action.
     *
     * @var string
     */
    private string $ajax_key;
    /**
     * Constructor.
     *
     * @param string $id The ID of the tool.
     * @param string $title The title of the tool.
     * @param string $button_text The text for the button.
     * @param array  $tool_function Callable for when the button is clicked.
     * @param string $description The description of the tool.
     */
    public function __construct(private string $id, private string $title, private string $button_text, private array $tool_function, private string $description = '')
    {
        $this->ajax_key = 'admin_sync_tool_' . $this->id;
    }
    /**
     * Renders the tool.
     *
     * @return void
     */
    public function render(): void
    {
        Abstract_Shared_Base::base_instance()->template_loader->get_admin_template_part('sync/tool', array('id' => $this->id, 'title' => $this->title, 'description' => $this->description, 'button_text' => $this->button_text), \false);
    }
    /**
     * Adds the AJAX action for the tool.
     *
     * @return void
     */
    public function add_ajax_action(): void
    {
        Abstract_Shared_Base::base_instance()->ajax->add_action($this->ajax_key, $this->tool_function);
    }
    /**
     * Gets the ID of the tool, for usage in JS.
     *
     * @return string
     */
    public function get_id_for_js(): string
    {
        return esc_attr($this->id);
        // Escaped to ensure same as in HTML.
    }
    /**
     * Gets the data for JavaScript.
     *
     * @return array
     */
    public function get_data_for_js(): array
    {
        return array_merge(array('title' => esc_attr($this->title)), Abstract_Shared_Base::base_instance()->ajax->get_action($this->ajax_key)?->get_data_for_js());
    }
}
