'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var actionsTemplate = require('./actionsTemplate.js');

const getActions = ({ refresh, print, share, copy, download }) => {
    const actions = [];
    if (refresh && refresh.url && refresh.executionId) {
        actions.push(setRefreshAction(actionsTemplate.actionTemplates.refresh, refresh));
    }
    if (print) {
        actions.push(setPrintAction(actionsTemplate.actionTemplates.printable, print));
    }
    if (share) {
        actions.push(setShareAction(actionsTemplate.actionTemplates.sharable, share));
    }
    if (copy) {
        actions.push(setCopyAction(actionsTemplate.actionTemplates.copy, copy));
    }
    if (download) {
        actions.push(setDownloadAction(actionsTemplate.actionTemplates.downloadable, download));
    }
    return actions;
};
const setRefreshAction = (template, data) => {
    const { url: href } = data;
    return {
        ...template,
        href,
    };
};
const setShareAction = (template, data) => {
    const { reportName, name, url } = data;
    const href = `mailto:?subject=${reportName}-${name}&body=${encodeURIComponent(url)}`;
    return {
        ...template,
        href,
    };
};
const setCopyAction = (template, data) => {
    const { url: href } = data;
    return {
        ...template,
        href,
    };
};
const setDownloadAction = (template, data) => {
    const { canDownload, enabled } = data;
    const { text, ariaLabelText } = template;
    const ariaLabel = canDownload ? ariaLabelText : 'Enable download';
    return {
        ...template,
        text: canDownload ? text : 'Enable download',
        disabled: !enabled,
        attributes: {
            ...data,
        },
        ariaLabelText: !enabled ? `${ariaLabel}, disabled` : ariaLabel,
    };
};
const setPrintAction = (template, data) => {
    const { enabled } = data;
    const { ariaLabelText } = template;
    return {
        ...template,
        disabled: !enabled,
        href: '#',
        ariaLabelText: !enabled ? `${ariaLabelText}, disabled` : ariaLabelText,
    };
};
const setActions = (definitionData, downloadConfig, requestData) => {
    const { reportName, name, printable } = definitionData;
    let shareConfig;
    let copyConfig;
    if (requestData?.requestUrl?.fullUrl) {
        shareConfig = {
            reportName,
            name,
            url: requestData.requestUrl.fullUrl,
        };
        copyConfig = {
            url: requestData.requestUrl.fullUrl,
        };
    }
    let refreshConfig;
    if (requestData?.executionId && requestData?.requestUrl?.fullUrl) {
        refreshConfig = {
            url: requestData.requestUrl.fullUrl,
            executionId: requestData.executionId,
        };
    }
    return getActions({
        ...(downloadConfig && { download: downloadConfig }),
        ...(shareConfig && { share: shareConfig }),
        ...(refreshConfig && { refresh: refreshConfig }),
        ...(copyConfig && { copy: copyConfig }),
        print: { enabled: printable },
    });
};
var ReportActionsUtils = {
    getActions,
    setActions,
};

exports.default = ReportActionsUtils;
exports.getActions = getActions;
//# sourceMappingURL=utils.js.map
