'use strict';

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

/**
 * Builds the row
 *
 * @param {StoredReportData} data
 * @param {RequestStatus} status
 * @param {Request} req
 * @param {Response} res
 * @param {ListType} listType
 * @return {*}
 */
const buildMyReportListRow = (data, status, req, res, listType) => {
    return {
        title: buildTitleCell(data),
        filters: buildFiltersCell(data),
        status,
        actions: buildActionsCell(data, res, req, listType),
        meta: buildMeta(data, res, listType),
    };
};
/**
 * Builds the titls cell data
 *
 * @param {StoredReportData} data
 * @return {*}  {DprMyReportTitle}
 */
const buildTitleCell = (data) => {
    return {
        productName: data.reportName,
        reportName: data.name,
        reportType: data.type,
        timestamp: buildTimestamp(data),
    };
};
/**
 * Builds the timestamp to display
 *
 * @param {StoredReportData} data
 * @return {*}
 */
const buildTimestamp = (data) => {
    const { status, timestamp } = data;
    const now = dateHelper.todayAsUiDateTime();
    switch (status) {
        case UserReports.RequestStatus.FAILED: {
            const { failed } = timestamp;
            const ts = failed ? dateHelper.apiTimestampToUiDateTime(failed) : now;
            return `Failed at ${ts}`;
        }
        case UserReports.RequestStatus.ABORTED: {
            const { aborted } = timestamp;
            const ts = aborted ? dateHelper.apiTimestampToUiDateTime(aborted) : now;
            return `Aborted at ${ts}`;
        }
        case UserReports.RequestStatus.FINISHED: {
            const { completed } = timestamp;
            const ts = completed ? dateHelper.apiTimestampToUiDateTime(completed) : now;
            return `Ready at ${ts}`;
        }
        case UserReports.RequestStatus.EXPIRED: {
            const { expired } = timestamp;
            const ts = expired ? dateHelper.apiTimestampToUiDateTime(expired) : now;
            return `Expired at ${ts}`;
        }
        case UserReports.RequestStatus.READY: {
            const { lastViewed } = timestamp;
            const ts = lastViewed ? dateHelper.apiTimestampToUiDateTime(lastViewed) : now;
            return `Last viewed at ${ts}`;
        }
        case UserReports.RequestStatus.SUBMITTED:
        case UserReports.RequestStatus.STARTED:
        case UserReports.RequestStatus.PICKED: {
            const { requested } = timestamp;
            const ts = requested ? dateHelper.apiTimestampToUiDateTime(requested) : now;
            return `Requested at ${ts}`;
        }
        default: {
            const { lastViewed } = timestamp;
            const ts = lastViewed ? dateHelper.apiTimestampToUiDateTime(lastViewed) : now;
            return `Last viewed at ${ts}`;
        }
    }
};
/**
 * Builds the filters cell data
 *
 * @param {StoredReportData} data
 * @return {*}  {DprMyReportFilters}
 */
const buildFiltersCell = (data) => {
    const prerequest = data.query?.summary || [];
    const interactive = data.interactiveQuery?.summary || [];
    return {
        ...(prerequest && { prerequest }),
        ...(interactive && { interactive }),
    };
};
/**
 * Builds the action cell data
 *
 * @param {StoredReportData} data
 * @param {Response} res
 * @return {*}  {DprMyReportActions}
 */
const buildActionsCell = (data, res, req, listType) => {
    const { status, type, url } = data;
    let retry;
    let refresh;
    let polling;
    let view;
    let remove;
    const pollingPageUrl = url?.polling?.fullUrl || '';
    const requestPageUrl = url?.request?.fullUrl || '';
    const reportPageUrl = url?.report?.fullUrl || '';
    switch (status) {
        case UserReports.RequestStatus.FAILED:
            retry = { href: pollingPageUrl };
            remove = buildRemoveAction(data, res, req, listType);
            break;
        case UserReports.RequestStatus.EXPIRED:
            refresh = { href: requestPageUrl };
            remove = buildRemoveAction(data, res, req, listType);
            break;
        case UserReports.RequestStatus.ABORTED:
            retry = { href: requestPageUrl };
            remove = buildRemoveAction(data, res, req, listType);
            break;
        case UserReports.RequestStatus.READY:
        case UserReports.RequestStatus.FINISHED:
            view = {
                href: reportPageUrl,
                reportType: type,
            };
            remove = buildRemoveAction(data, res, req, listType);
            break;
        case UserReports.RequestStatus.PICKED:
        case UserReports.RequestStatus.SUBMITTED:
        case UserReports.RequestStatus.STARTED:
            polling = { href: pollingPageUrl };
            break;
    }
    return {
        ...(retry && { retry }),
        ...(refresh && { refresh }),
        ...(remove && { remove }),
        ...(view && { view }),
        ...(polling && { polling }),
    };
};
/**
 * Builds the remove action
 *
 * @param {StoredReportData} data
 * @param {Response} res
 * @return {*}
 */
const buildRemoveAction = (data, res, req, listType) => {
    const { reportId, id, executionId, tableId } = data;
    const { requestedListPath, recentlyViewedListPath } = localsHelper.getRouteLocals(res);
    let action = '';
    if (listType === types.ListType.REQUESTED) {
        // ASYNC report - only executionId is needed
        action = `${requestedListPath}/remove-item/${executionId}`;
    }
    else if (tableId && executionId) {
        // ASYNC report - tableId and executionId needed
        action = `${recentlyViewedListPath}/remove-item/${executionId}/table-id/${tableId}`;
    }
    else {
        // SYNC report - reportId and Id needed
        action = `${recentlyViewedListPath}/remove-item/report-id/${reportId}/id/${id}`;
    }
    return {
        action,
        csrfToken: res.locals.csrfToken,
        returnTo: req.originalUrl,
    };
};
/**
 * Builds the meta data needed by the polling class
 *
 * @param {StoredReportData} data
 * @param {Response} res
 * @return {*}
 */
const buildMeta = (data, res, listType) => {
    const { userReportsList } = localsHelper.getRouteLocals(res);
    return {
        id: data.id,
        reportId: data.reportId,
        tableId: data.tableId,
        executionId: data.executionId,
        path: userReportsList,
        listType,
    };
};

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