<?php

namespace DaftPlug\Generatify\Features\HelpdeskAgent;

use DaftPlug\Generatify\Helpers\Utils;

if (!defined('ABSPATH')) {
  exit();
}

class UI
{
  public function __construct()
  {
    add_action('wp_enqueue_scripts', [$this, 'loadHelpdeskAgentAssets']);
  }

  public function loadHelpdeskAgentAssets()
  {
    if (Utils::getSetting('aiHelpdeskAgent') !== 'on') {
      return;
    }

    $dependencies = [];
    $dependencies[] = 'wp-i18n';
    $dependencies[] = 'jquery';

    // Load Helpdesk Agent script
    wp_enqueue_script('generatify-helpdesk-agent', plugins_url('assets/js/helpdeskAgent.js', GENERATIFY_FILE), $dependencies, GENERATIFY_VERSION, true);
    wp_set_script_translations('generatify-helpdesk-agent', 'generatify');
    wp_localize_script('generatify-helpdesk-agent', 'generatify_helpdesk_agent_js_vars', [
      'restUrl' => esc_url_raw(get_rest_url()),
      'restNonce' => wp_create_nonce('wp_rest'),
      'hasActivePro' => \DaftPlug\Generatify\generatify_pro()->can_use_premium_code(),
      'aiHelpdeskAgentSettings' => [
        'position' => Utils::getSetting('aiHelpdeskAgentPosition'),
        'primaryColor' => Utils::getSetting('aiHelpdeskAgentPrimaryColor'),
        'cover' => wp_get_attachment_url(Utils::getSetting('aiHelpdeskAgentCover')),
        'icon' => wp_get_attachment_url(Utils::getSetting('aiHelpdeskAgentIcon')),
        'homeTitle' => Utils::getSetting('aiHelpdeskAgentHomeTitle'),
        'homeTagline' => Utils::getSetting('aiHelpdeskAgentHomeTagline'),
        'homeButtonText' => Utils::getSetting('aiHelpdeskAgentHomeButtonText'),
        'homePopularTopics' => Utils::getSetting('aiHelpdeskAgentHomePopularTopics'),
        'messageMode' => Utils::getSetting('aiHelpdeskAgentMessageMode'),
        'helpArticles' => Utils::getSetting('aiHelpdeskAgentHelpArticles'),
      ],
    ]);
  }
}