<?php

/**
 * Return the info popup entries of a project as grid data.
 *
 * @param string $project - project name
 * @return array{data: array<int, array<string, mixed>>, total: int}
 */

use QUI\InfoPopup\Handler;

QUI::getAjax()->registerFunction(
    'package_quiqqer_info-popup_ajax_list',
    function ($project) {
        $Project = QUI::getProject($project);
        $lang = QUI::getLocale()->getCurrent();

        $data = [];

        foreach (Handler::getList($Project) as $row) {
            $data[] = [
                'id' => (int)$row['id'],
                'active' => empty($row['active']) ? 0 : 1,
                // grid uses the admin UI language with fallback, so a row is
                // never blank (unlike the frontend delivery)
                'title' => Handler::decodeMultiLang($row['title'] ?? '', $lang),
                'languages' => Handler::parseLanguages($row['languages'] ?? null),
                'validFrom' => (string)($row['validFrom'] ?? ''),
                'validTo' => (string)($row['validTo'] ?? ''),
                'visibility' => (string)($row['visibility'] ?? 'always'),
                'priority' => (int)($row['priority'] ?? 0)
            ];
        }

        return [
            'data' => $data,
            'total' => count($data)
        ];
    },
    ['project'],
    'quiqqer.infoPopup.edit'
);
