import { LoadType, ReportType } from '../../../types/UserReports.js';
import ShowMoreUtils from '../../show-more/utils.js';
import { setInitialHref, createListItemProductMin, createListActions } from '../../../utils/reportListsHelper.js';
import localsHelper from '../../../utils/localsHelper.js';

const getReportsList = async (res, services) => {
    const { definitions, csrfToken, bookmarkingEnabled, dprUser } = localsHelper.getValues(res);
    // Sort report Definitions by product name
    const sortedDefinitions = definitions.sort((a, b) => {
        if (a.name < b.name)
            return -1;
        if (a.name > b.name)
            return 1;
        return 0;
    });
    // Sort by variant/dashboard name
    const sortedVariants = sortedDefinitions.flatMap((def) => {
        const { id: reportId, name: reportName, description: reportDescription, variants, dashboards, authorised } = def;
        const productBase = {
            reportName,
            reportId,
            authorised,
            reportDescription: reportDescription || '',
        };
        let variantsArray = [];
        if (variants) {
            variantsArray = variants.map((variant) => {
                const { id, name, description, isMissing, loadType } = variant;
                return {
                    ...productBase,
                    type: ReportType.REPORT,
                    loadType: loadType || LoadType.ASYNC,
                    id,
                    name,
                    description: description || '',
                    isMissing,
                };
            });
        }
        let dashboardsArray = [];
        if (dashboards) {
            dashboardsArray = dashboards.map((dashboard) => {
                const { id, name, description, loadType } = dashboard;
                return {
                    ...productBase,
                    type: ReportType.DASHBOARD,
                    loadType: loadType || LoadType.ASYNC,
                    id,
                    name,
                    description: description || '',
                    isMissing: false,
                };
            });
        }
        const mergedArray = [...dashboardsArray, ...variantsArray];
        mergedArray.sort((a, b) => {
            if (a.name < b.name)
                return -1;
            if (a.name > b.name)
                return 1;
            return 0;
        });
        return mergedArray;
    });
    const userConfig = await services.bookmarkService.getState(dprUser.id);
    const rows = await Promise.all(sortedVariants.map(async (v) => {
        const { id, name, description, reportName, reportId, reportDescription, type, loadType, authorised, isMissing } = v;
        const desc = description || reportDescription || '';
        const href = setInitialHref(loadType, type, reportId, id, res, isMissing);
        const { bookmarkActionEndpoint } = localsHelper.getRouteLocals(res);
        let bookmarkHtml;
        if (bookmarkingEnabled) {
            bookmarkHtml = await services.bookmarkService.createBookMarkButtonHtml({
                userConfig,
                reportId,
                id,
                csrfToken,
                ctxId: 'reports-list',
                reportType: type,
                isMissing: Boolean(isMissing),
                endpoint: bookmarkActionEndpoint,
            });
        }
        return [
            { html: `<p class="govuk-body-s">${reportName}</p>` },
            { html: createListItemProductMin(name, type) },
            { html: ShowMoreUtils.createShowMoreHtml(desc) },
            { html: createListActions(href, type, loadType, bookmarkHtml, authorised, isMissing) },
        ];
    }));
    const head = [
        { text: 'Product', classes: 'dpr-product-head' },
        { text: 'Name', classes: 'dpr-name-head' },
        { text: 'Description', classes: 'dpr-description-head' },
        { text: 'Actions', classes: 'dpr-bookmark-head' },
    ];
    return {
        head,
        rows,
        id: 'dpr-reports-catalogue',
    };
};
var CatalogueListUtils = {
    getReportsList,
};

export { CatalogueListUtils as default, getReportsList };
//# sourceMappingURL=utils.js.map
