"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExportSelector = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const Select_1 = require("../../components/Select");
const SimpleButton_1 = tslib_1.__importDefault(require("../../components/SimpleButton"));
const StringExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/StringExtensions"));
const AdaptableContext_1 = require("../AdaptableContext");
const isCustomDestination = (destination) => {
    return !!destination.name;
};
const ExportSelector = () => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    // reports
    const handleReportChange = (report) => {
        adaptable.api.exportApi.selectReport(report);
    };
    const allReports = adaptable.api.exportApi.getReports();
    const allReportNames = allReports.map((report) => report.Name);
    const reportItems = allReportNames.map((report) => {
        return {
            label: report,
            value: report,
        };
    });
    const currentReport = adaptable.api.exportApi.getCurrentReport();
    const currentReportId = StringExtensions_1.default.IsNullOrEmpty(currentReport === null || currentReport === void 0 ? void 0 : currentReport.Name)
        ? currentReport
        : currentReport.Name;
    // destinations
    const handleDestinationChange = (destination) => {
        adaptable.api.exportApi.selectDestination(destination);
    };
    const destinationItems = [
        ...adaptable.api.exportApi.getAvailableExportDestinations().map((destination) => ({
            label: destination,
            value: destination,
            isDisabled: !adaptable.api.exportApi.internalApi.isDestinationEnabled(destination),
        })),
        ...adaptable.api.exportApi.getCustomDestinations().map((destination) => ({
            label: destination.name,
            value: destination.name,
        })),
    ];
    const currentDestination = adaptable.api.exportApi.getCurrentDestination();
    let currentDestinationId = undefined;
    if (typeof currentDestination === 'string') {
        currentDestinationId = currentDestination;
    }
    else if (currentDestination && 'name' in currentDestination) {
        currentDestinationId = currentDestination.name;
    }
    // export
    const handleExport = () => {
        if (isCustomDestination(currentDestination)) {
            adaptable.api.exportApi.runReport(currentReport.Name, currentDestination.name);
        }
        else {
            adaptable.api.exportApi.runReport(currentReport.Name, currentDestination);
        }
    };
    return (React.createElement(React.Fragment, null,
        React.createElement(Select_1.Select, { size: "small", isClearable: true, placeholder: 'Select Report', value: currentReportId, options: reportItems, onChange: handleReportChange }),
        React.createElement(Select_1.Select, { size: "small", isClearable: true, placeholder: 'Select Destination', value: currentDestinationId, options: destinationItems, onChange: (destination) => {
                if (destination) {
                    handleDestinationChange(destination);
                }
                else {
                    handleDestinationChange(null);
                }
            } }),
        React.createElement(SimpleButton_1.default, { disabled: !currentDestinationId || !currentReportId, iconSize: 15, icon: "export", variant: "text", onClick: handleExport })));
};
exports.ExportSelector = ExportSelector;
