'use strict';

var UserReports = require('../../../../types/UserReports.js');
var localsHelper = require('../../../../utils/localsHelper.js');

/**
 * Public api for building out the congfig for a current status component
 *
 * @param {RequestedReport} data
 * @param {RequestStatus} status
 * @param {Response} res
 * @return {*}
 */
const buildCurrentStatusView = (data, status, res) => {
    const { csrfToken } = localsHelper.getValues(res);
    const metaDetails = setMetaDetails(data);
    const requestDetails = setRequestDetails(data);
    const statusDetails = setStatusDetails(status, data);
    return {
        csrfToken,
        metaDetails,
        requestDetails,
        statusDetails,
    };
};
const setMetaDetails = (data) => {
    const { reportId, id, executionId, tableId, type } = data;
    return {
        reportId,
        id,
        executionId,
        tableId,
        type,
    };
};
const setRequestDetails = (data) => {
    const { reportName, name, timestamp, query } = data;
    return {
        reportName: reportName || '',
        name: name || '',
        requestedAt: timestamp.requested,
        ...(query && { querySummary: query?.summary }),
    };
};
const setStatusDetails = (status, data) => {
    const { url, type } = data;
    const reportUrl = url?.report?.fullUrl;
    const requestUrl = url?.request?.fullUrl;
    let descriptionText;
    let altText;
    let buttonText;
    let buttonHref;
    let errorMessage;
    switch (status) {
        case UserReports.RequestStatus.ABORTED:
            descriptionText = 'Your request has been cancelled';
            buttonText = 'Return to request page';
            buttonHref = requestUrl;
            break;
        case UserReports.RequestStatus.FAILED:
            buttonText = 'Retry';
            descriptionText = 'Your report is no longer available and needs to be refreshed';
            buttonHref = requestUrl;
            errorMessage = setErrorMessage(data);
            break;
        case UserReports.RequestStatus.EXPIRED:
            descriptionText = 'Your report is no longer available and needs to be refreshed';
            buttonText = 'Refresh report';
            buttonHref = requestUrl;
            break;
        case UserReports.RequestStatus.STARTED:
        case UserReports.RequestStatus.SUBMITTED:
        case UserReports.RequestStatus.PICKED:
            descriptionText = `We are generating your ${type}.`;
            altText = [
                `Your ${type} will be generated shortly.`,
                `You may navigate away from this page at anytime. Please visit the homepage to monitor your report status.`,
            ];
            break;
        case UserReports.RequestStatus.FINISHED:
        case UserReports.RequestStatus.READY:
            descriptionText = `Your ${type} has been generated`;
            altText = [
                `Please wait, you are being redirected...`,
                `If you are not redirected within 10 seconds, please copy and paste this url into your browser and press enter:`,
                `${requestUrl}`,
            ];
            buttonHref = reportUrl;
            break;
    }
    return {
        status,
        descriptionText,
        altText,
        buttonText,
        buttonHref,
        errorMessage,
    };
};
const setErrorMessage = (data) => {
    return data?.errorMessage ? JSON.parse(data?.errorMessage) : undefined;
};

exports.buildCurrentStatusView = buildCurrentStatusView;
//# sourceMappingURL=utils.js.map
