import React, { useMemo, useState, useCallback } from 'react';
import i18n from 'ui-i18n';
import Button from '@mui/material/Button';
import Portal from '@mui/material/Portal';
import Snackbar from '@mui/material/Snackbar';
import GetAppIcon from '@mui/icons-material/GetApp';
import { SmallIconButtonWithTooltip } from '../../../SmallIconButton';
import { DropDownMenuButton } from '../../../DropDownMenuButton';
import { exportActivities } from '@reltio/mdm-sdk';
import { buildActivitiesFilterString } from '../utils/filters';
import { useMdmAction, useMdmExportPath, useMdmTenant } from '../../../contexts/MdmModuleContext';
import { showErrorMessage } from '../../../helpers/errors';
import { useStyles } from './styles';
export var ActivityExportButton = function (_a) {
    var filters = _a.filters, _b = _a.exportTypes, exportTypes = _b === void 0 ? [] : _b;
    var styles = useStyles();
    var openConsoleApp = useMdmAction('openConsoleApp');
    var exportPath = useMdmExportPath();
    var tenant = useMdmTenant();
    var _c = useState(false), showSnackbar = _c[0], setShowSnackbar = _c[1];
    var handleClickExport = useCallback(function (type) {
        var options = {
            fileFormat: type
        };
        exportActivities({ exportPath: exportPath, tenant: tenant, options: options, filter: buildActivitiesFilterString(filters) })
            .then(function () { return setShowSnackbar(true); })
            .catch(onError);
    }, [filters, tenant, exportPath]);
    var onError = function (error) {
        showErrorMessage(error, { title: i18n.text('Error'), defaultMessage: i18n.text("Can't export activities") });
    };
    var handleOpenAppClick = useCallback(function () {
        openConsoleApp('Export');
    }, [openConsoleApp]);
    var menuItems = useMemo(function () {
        return exportTypes.map(function (type) { return ({
            text: type.toUpperCase(),
            onClick: function () { return handleClickExport(type); }
        }); });
    }, [exportTypes, handleClickExport]);
    return (React.createElement(React.Fragment, null,
        menuItems.length ? (React.createElement(DropDownMenuButton, { buttonComponent: SmallIconButtonWithTooltip, buttonProps: {
                icon: GetAppIcon,
                tooltipTitle: i18n.text('Export'),
                size: 'L',
                className: styles.icon
            }, menuItems: menuItems, menuId: "export-menu" })) : (React.createElement(SmallIconButtonWithTooltip, { icon: GetAppIcon, size: "L", onClick: function () { return handleClickExport(); }, tooltipTitle: i18n.text('Export'), className: styles.icon })),
        React.createElement(Portal, { container: document.body },
            React.createElement(Snackbar, { open: showSnackbar, autoHideDuration: 5000, className: styles.snackbar, onClose: function () { return setShowSnackbar(false); }, anchorOrigin: { vertical: 'bottom', horizontal: 'center' }, message: i18n.text('Export operation is currently in progress. You will receive an email notification once completed. Go to Export Application to view the status.'), action: React.createElement(Button, { className: styles.exportButton, onClick: handleOpenAppClick }, i18n.text('Export application')) }))));
};
