import * as React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import join from '../../components/utils/join';
import * as ExportRedux from '../../Redux/ActionsReducers/ExportRedux';
import * as PopupRedux from '../../Redux/ActionsReducers/PopupRedux';
import * as GeneralConstants from '../../Utilities/Constants/GeneralConstants';
import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants';
import AdaptableHelper from '../../Utilities/Helpers/AdaptableHelper';
import ObjectFactory from '../../Utilities/ObjectFactory';
import { ButtonDelete } from '../Components/Buttons/ButtonDelete';
import { ButtonEdit } from '../Components/Buttons/ButtonEdit';
import { ButtonNew } from '../Components/Buttons/ButtonNew';
import { ButtonSchedule } from '../Components/Buttons/ButtonSchedule';
import { ReportNameSelector } from './ReportNameSelector';
import { ReportFormatSelector } from './ReportFormatSelector';
import { ExportDestinationPicker } from './ExportDestinationPicker';
import { Flex } from '../../components/Flex';
import clsx from 'clsx';
export const ExportViewPanel = (props) => {
    const dispatch = useDispatch();
    const exportApi = props.api.exportApi;
    const userReports = useSelector((state) => state.Export.Reports);
    const currentReportName = useSelector((state) => state.Export.CurrentReport);
    const currentFormat = useSelector((state) => state.Export.CurrentFormat);
    const currentReport = exportApi.getReportByName(currentReportName);
    const accessLevel = AdaptableHelper.getAccessLevelForObject(currentReport, props.accessLevel);
    const deleteMessage = currentReport
        ? `Are you sure you want to delete '${currentReport.Name}'?`
        : 'Are you sure you want to delete this report?';
    const isToolbar = props.viewType === 'Toolbar';
    const elementType = isToolbar ? 'DashboardToolbar' : 'ToolPanel';
    return (React.createElement(Flex, { flexDirection: "row", className: clsx(`ab-${elementType}__CellSummary__wrap twa:gap-2 twa:flex-row`, {
            'twa:min-w-[486px] twa:flex-nowrap': isToolbar,
            'twa:flex-1 twa:flex-wrap': !isToolbar,
        }) },
        React.createElement(ReportNameSelector, { viewType: props.viewType, reportName: currentReportName, userReports: userReports, onReportNameSelected: (reportName) => exportApi.selectReport(reportName) }),
        React.createElement(ReportFormatSelector, { viewType: props.viewType, reportName: currentReportName, reportFormat: currentFormat, onReportFormatSelected: (reportFormat) => exportApi.selectFormat(reportFormat) }),
        React.createElement(Flex, { className: join(accessLevel == 'ReadOnly' ? GeneralConstants.READ_ONLY_STYLE : '', `ab-${elementType}__Export__controls twa:w-full`) },
            React.createElement(Flex, { className: "twa:min-w-[44px]" },
                React.createElement(ExportDestinationPicker, { viewType: props.viewType, reportName: currentReportName, reportFormat: currentFormat })),
            React.createElement(Flex, null,
                React.createElement(ButtonEdit, { onClick: () => dispatch(PopupRedux.PopupShowScreen(ModuleConstants.ExportModuleId, props.moduleInfo.Popup, {
                        action: 'Edit',
                        source: 'Toolbar',
                    })), tooltip: "Edit Report", className: `ab-${elementType}__Export__edit`, disabled: currentReport == null, accessLevel: accessLevel }),
                React.createElement(ButtonNew, { variant: "text", className: `ab-${elementType}__Export__new`, tone: "neutral", children: null, onClick: () => dispatch(PopupRedux.PopupShowScreen(ModuleConstants.ExportModuleId, props.moduleInfo.Popup, {
                        action: 'New',
                        source: 'Toolbar',
                    })), tooltip: "Create New Report", accessLevel: props.accessLevel }),
                React.createElement(ButtonDelete, { tooltip: "Delete Report", className: `ab-${elementType}__Export__delete`, disabled: currentReport == null, ConfirmAction: ExportRedux.ReportDelete(currentReport), ConfirmationMsg: deleteMessage, ConfirmationTitle: 'Delete Report', accessLevel: accessLevel }),
                props.api.entitlementApi.isModuleFullEntitlement('Schedule') && (React.createElement(ButtonSchedule, { className: `ab-${elementType}__Export__schedule`, onClick: () => {
                        const reportSchedule = ObjectFactory.CreateReportSchedule(currentReportName, currentFormat);
                        dispatch(PopupRedux.PopupShowScreen(ModuleConstants.ScheduleModuleId, 'SchedulePopup', {
                            action: 'New',
                            source: 'Toolbar',
                            value: reportSchedule,
                        }));
                    }, tooltip: "Schedule", disabled: currentReport == null, accessLevel: props.accessLevel }))))));
};
