<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\AdminBundle\Export;

/**
 * Builds the export-menu descriptors a list view passes to the
 * {@see \Ferienpass\AdminBundle\Components\SearchableQueryableList} component.
 *
 * Each view declares its exports explicitly — the component does not derive
 * routes. A descriptor is a plain array so it survives LiveProp (de)hydration:
 * `['label' => …, 'route' => …, 'params' => …, 'icon' => …]`. The component
 * generates the final URL, merging the current filter query.
 */
final class ExportLink
{
    /**
     * A generic, format-based export (xlsx, csv, …) pointing at the view's
     * export route. The format fills the `{format}` route placeholder; the
     * label is the upper-cased format and doubles as a translation key.
     *
     * @return array{label: string, route: string, params: array<string, string>, icon: string}
     */
    public static function format(string $format, string $route): array
    {
        return [
            'label' => strtoupper($format),
            'route' => $route,
            'params' => ['format' => $format],
            'icon' => $format,
        ];
    }

    /**
     * Map every available format to a {@see self::format()} descriptor.
     *
     * @param iterable<string> $formats
     *
     * @return list<array{label: string, route: string, params: array<string, string>, icon: string}>
     */
    public static function formats(iterable $formats, string $route): array
    {
        $links = [];
        foreach ($formats as $format) {
            $links[] = self::format($format, $route);
        }

        return $links;
    }

    /**
     * A bespoke export pointing at its own route (e.g. the payments
     * "Kassenbericht"). The label is a translation key.
     *
     * @param array<string, scalar> $params
     *
     * @return array{label: string, route: string, params: array<string, scalar>, icon: string}
     */
    public static function to(string $label, string $route, string $icon = 'pdf', array $params = []): array
    {
        return [
            'label' => $label,
            'route' => $route,
            'params' => $params,
            'icon' => $icon,
        ];
    }
}
