<?php

namespace intec\template\controllers;

class ComponentsController extends BaseController
{
    /**
     * @param string|null $component
     * @param string|null $template
     * @param array|null $parameters
     * @return void
     */
    public function actionGet(
        ?string $component = null,
        ?string $template = null,
        ?array $parameters = null
    ): void
    {
        global $APPLICATION;

        $data = [
            'component' => $component,
            'template' => $template,
            'parameters' => $parameters
        ];

        if (
            $data['component'] === null ||
            $data['component'] === '' ||
            !self::isAllowedComponent($data['component'])
        )
            return;

        if ($data['template'] === null || $data['template'] === '')
            $data['template'] = '.default';

        if (!is_array($data['parameters']))
            $data['parameters'] = [];

        foreach ($data['parameters'] as $key => $parameter)
            if (str_starts_with(haystack: $key, needle: '~'))
                unset($data['parameters'][$key]);

        $data['parameters'] = array_merge([
            'AJAX_MODE' => 'Y',
            'AJAX_OPTION_ADDITIONAL' => 'COMPONENT',
            'AJAX_OPTION_SHADOW' => 'N',
            'AJAX_OPTION_JUMP' => 'N',
            'AJAX_OPTION_STYLE' => 'Y'
        ], $data['parameters']);

        $APPLICATION->ShowAjaxHead();
        $APPLICATION->IncludeComponent(
            componentName: $data['component'],
            componentTemplate: $data['template'],
            arParams: $data['parameters'],
            arFunctionParams: ['HIDE_ICONS' => 'Y']
        );
    }
}
