import { AdaptableModuleBase } from './AdaptableModuleBase';
import * as ModuleConstants from '../Utilities/Constants/ModuleConstants';
import * as ExportRedux from '../Redux/ActionsReducers/ExportRedux';
import { NewReportWizard } from '../View/Export/Wizard/NewReportWizard';
import { getExportColumnsViewItems } from './Utilities/Export/getExportColumnsViewItems';
import { getExportRowsViewItems } from './Utilities/Export/getExportRowsViewItems';
import { getObjectTagsViewItems } from '../Utilities/getObjectTagsViewItems';
import { ExportStatusBar } from '../View/Export/ExportStatusBar';
import { ReportListItem } from '../View/Export/ReportListItem';
export class ExportModule extends AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.ExportModuleId, ModuleConstants.ExportFriendlyName, 'export-data', 'ExportPopup', 'Export data from the Grid to numerous locations in numerous formatso', api);
    }
    getModuleAdaptableObjects() {
        return this.api.exportApi.getAllReports();
    }
    getExplicitlyReferencedColumnIds(report) {
        if (report.ReportColumnScope === 'ScopeColumns' &&
            this.api.columnScopeApi.scopeHasColumns(report.Scope)) {
            return this.api.columnScopeApi
                .getColumnsInScope(report.Scope)
                .map((adaptableColumn) => adaptableColumn.columnId);
        }
        return [];
    }
    getReferencedNamedQueryNames(report) {
        if (!report.Query) {
            return [];
        }
        return this.api.namedQueryApi.internalApi.getReferencedNamedQueryNames(report.Query.BooleanExpression);
    }
    createContextMenuItems(menuContext) {
        const canExport = !menuContext.isRowGroupColumn && this.isModuleAvailable();
        if (!canExport) {
            return;
        }
        let returnMenuItems = [];
        const allReportNames = this.api.exportApi.internalApi.getAllAvailableReportNames();
        const allReportFormats = this.api.exportApi.getAvailableSystemFormats();
        allReportNames.forEach((reportName) => {
            const formatItems = allReportFormats.map((reportFormat) => {
                const supportedDestinations = this.api.exportApi.getSupportedExportDestinations(reportFormat);
                const exportDestinationItems = supportedDestinations.map((exportDestination) => this.createMenuItemClickFunction(this.getMenuItemName(reportName, reportFormat, exportDestination), exportDestination, this.getDestinationMenuItemIcon(exportDestination), () => this.api.exportApi.exportReport(reportName, reportFormat, exportDestination)));
                const formatMenuItemGroup = {
                    name: this.getMenuItemName(reportName, reportFormat),
                    label: reportFormat,
                    isVisible: true,
                    category: 'Export',
                    icon: {
                        name: this.getFormatMenuItemIcon(reportFormat),
                    },
                    subItems: exportDestinationItems,
                };
                return formatMenuItemGroup;
            });
            const reportMenuItemGroup = {
                name: this.getMenuItemName(reportName),
                label: reportName,
                isVisible: true,
                category: 'Export',
                icon: {
                    name: 'export-data',
                },
                subItems: formatItems,
            };
            returnMenuItems.push(reportMenuItemGroup);
        });
        return returnMenuItems;
    }
    getMenuItemName(reportName, reportFormat, exportDestination) {
        // convert to lowercase and replace empty spaces with hyphens
        const adjustName = (name = '') => {
            const result = name.replace(/ /g, '-');
            return result.toLowerCase();
        };
        let name = `export-${adjustName(reportName)}`;
        if (reportFormat) {
            name += `-${adjustName(reportFormat)}`;
        }
        if (exportDestination) {
            name += `-${adjustName(exportDestination)}`;
        }
        return name;
    }
    getDestinationMenuItemIcon(exportDestination) {
        switch (exportDestination) {
            case 'Download':
                return 'downloaded';
            case 'Clipboard':
                return 'clipboard';
            default:
                return 'export-data';
        }
    }
    getFormatMenuItemIcon(reportFormat) {
        switch (reportFormat) {
            case 'Excel':
                return 'excel';
            case 'VisualExcel':
                return 'color-palette';
            case 'CSV':
                return 'csv';
            case 'JSON':
                return 'json';
            default:
                return 'export';
        }
    }
    getTeamSharingAction() {
        return {
            ModuleEntities: this.api.exportApi.getAllReports(),
            AddAction: ExportRedux.ReportAdd,
            EditAction: ExportRedux.ReportEdit,
        };
    }
    toView(report) {
        const isSystemReport = this.api.exportApi.internalApi.isSystemReport(report.Name);
        if (isSystemReport) {
            return {
                items: [
                    {
                        name: 'Report',
                        values: [report.Name],
                    },
                    getObjectTagsViewItems(report, this.api),
                ],
                abObject: report,
            };
        }
        return {
            items: [
                {
                    name: 'Report',
                    values: [report.Name],
                },
                getExportColumnsViewItems(report, this.api),
                getExportRowsViewItems(report, this.api),
                getObjectTagsViewItems(report, this.api),
            ],
            abObject: report,
        };
    }
    toViewAll() {
        return this.getModuleAdaptableObjects().map((report) => this.toView(report));
    }
    getViewProperties() {
        return {
            newTooltipText: 'Create New Report',
            actions: [ReportListItem],
            getDeleteAction: ExportRedux.ReportDelete,
            getEditWizard() {
                return NewReportWizard;
            },
            getStatusBarPanelProps: () => {
                return {
                    content: ExportStatusBar,
                    triggerActionOnWrapperClick: false,
                };
            },
        };
    }
}
