<?php

/**
 * Smarty plugin.
 */

/**
 * Smarty {insert_llm_button} plugin.
 *
 * Purpose: Insert llm button
 *
 * @param array                    $params parameters
 * @param Smarty_Internal_Template $smarty template object
 *
 * @return string
 */
function smarty_function_insert_llm_button($params, $smarty)
{
    if (!findModule(Modules::LLM)) {
        return;
    }

    $textObjectUtil = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService(\KupShop\LLMBundle\Util\TextObjectUtil::class);

    $params['actions'] = [];

    foreach ($textObjectUtil->getObjectLabelPrompts($params['type']) as $id => $prompt) {
        $json = [
            'entityType' => $params['entityType'] ?? substr(getVal('s'), 0, -4),
            'entityId' => $params['entityId'] ?? getVal('ID'),
            'target' => $params['target'],
            'promptId' => $id,
            'objectLabel' => $params['type'] ?? '',
            'title' => $prompt->getTitle(),
        ];

        $params['actions'][] = [
            'id' => $id,
            'title' => $prompt->getTitle(),
            'json' => $json,
        ];
    }

    echo $smarty->_subTemplateRender('llm/text-dropdown.tpl', $smarty->cache_id, $smarty->compile_id, 0, null, $params, 0, false);
}
