import React, { useEffect, useMemo } from 'react';
import Alert from '@material-ui/lab/Alert';
import { Report } from 'powerbi-report-component';
import { useTranslation } from 'react-i18next';
import { useIsUnityAuthenticated } from '../utilities/auth/useIsUnityAuthenticated';
import { useAxiosGet } from '../utilities/useAxiosGet';
import LoadingOverlay from '../UI/loading/LoadingOverlay';
import { useHistory } from 'react-router-dom';
import { useSetLocationState } from '../hooks/useSetLocationState';
import { ConfigService } from '../configService';
export const PowerBIReport = (props) => {
    const userIsAuthenticated = useIsUnityAuthenticated();
    const { id, reportParameters } = props;
    const i18n = useTranslation().t;
    const history = useHistory();
    const setLocationState = useSetLocationState();
    const reportStyle = {
        height: 'calc(100vh - 51px)'
    };
    const [{ data: reportData, loading, error }] = useAxiosGet(ConfigService.productV1ApiUrl, `UdpPBIReport/Access/${id}`, {}, !!!userIsAuthenticated || !!!id);
    const urlWithParameters = useMemo(() => {
        if (reportData === null || reportData === void 0 ? void 0 : reportData.embedUrl) {
            if (reportParameters) {
                let paramsString = '';
                Object.keys(reportParameters).forEach((key) => {
                    const paramString = `&rp:${key}=${reportParameters[key]}`;
                    paramsString += paramString;
                });
                return reportData.embedUrl + paramsString;
            }
            else {
                return reportData.embedUrl;
            }
        }
        else
            return '';
    }, [reportData === null || reportData === void 0 ? void 0 : reportData.embedUrl, reportParameters]);
    // Setting location state label so that if this page was saved to favorites
    // we can determine the bread crumb
    useEffect(() => {
        var _a;
        if (!((_a = history === null || history === void 0 ? void 0 : history.location) === null || _a === void 0 ? void 0 : _a.state)) {
            setLocationState({ label: i18n('Report') });
        }
    }, [setLocationState, i18n, history]);
    if (loading)
        return React.createElement(LoadingOverlay, null);
    if (error) {
        return (React.createElement(Alert, { severity: "error", onClose: () => { } }, i18n('shared.messages.error')));
    }
    if (!userIsAuthenticated)
        return null;
    if (!reportData ||
        !reportData.embedToken ||
        !urlWithParameters ||
        !reportData.reportId) {
        //If no report is found, should we display something?
        //Right now it will display a blank screen
        return React.createElement("div", null);
    }
    return (React.createElement(Report, { key: reportData.reportId, embedType: "report", tokenType: "Embed", accessToken: reportData.embedToken, embedUrl: urlWithParameters, embedId: reportData.reportId, permissions: "All", style: reportStyle }));
};
//# sourceMappingURL=PowerBIReport.js.map