<?php

namespace intec\template\controllers;

use intec\core\helpers\{Encoding, EncodingBase};
use intec\core\web\Response;

/**
 * Контроллер получения постраничной навигации
 * @author Intec
 * @version 1.0.0
 */
class NavigationController extends BaseController
{
    /**
     * Действие получения навигации
     * @param null|string $component Название компонента
     * @param null|string $template Название шаблона
     * @param null|array $parameters Список параметров вызова компонента
     * @return Response
     */
    public function actionGet(
        ?string $component = null,
        ?string $template = null,
        ?array $parameters = null
    ): Response
    {
        global $APPLICATION;

        $responseData = [
            'data' => [],
            'format' => Response::FORMAT_JSON
        ];

        if (
            $component === null ||
            $component === '' ||
            !self::isAllowedComponent($component)
        ) {
            $responseData['data']['code'] = 'incorrectComponent';
            $responseData['data']['message'] = 'Selected incorrect component';
            $responseData['data']['status'] = 'error';

            return new Response($responseData);
        }

        if (empty($template))
            $template = '.default';

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

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

        $this->startOutputCapture();
        $APPLICATION->IncludeComponent(
            componentName: $component,
            componentTemplate: $template,
            arParams: $parameters,
            arFunctionParams: ['HIDE_ICONS' => 'Y']
        );
        $content = $this->endOutputCapture();

        list(, $items) = explode(separator: '<!--items-->', string: $content);
        list(, $navigation) = explode(separator: '<!--navigation-->', string: $content);

        if (empty($items)) {
            $responseData['data']['code'] = 'emptyContent';
            $responseData['data']['data'] = $content;
            $responseData['data']['message'] = 'Items or navigation not found';
            $responseData['data']['status'] = 'error';

            return new Response($responseData);
        }

        $responseData['data']['code'] = 'ok';
        $responseData['data']['data']['items'] = Encoding::convert(
            object: $items,
            encodingTo: EncodingBase::UTF8
        );
        $responseData['data']['data']['navigation'] = Encoding::convert(
            object: $navigation ?: '',
            encodingTo: EncodingBase::UTF8
        );
        $responseData['data']['status'] = 'success';

        return new Response($responseData);
    }

    /**
     * @inheritdoc
     */
    public function bindActionParameters($action, $parameters): array
    {
        return [
            'component' => $_POST['component'] ?? null,
            'template' => $_POST['template'] ?? null,
            'parameters' => $_POST['parameters'] ?? null
        ];
    }
}
