"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReportRowsWizardSection = exports.renderReportRowsSummary = exports.isValidReportRowsQuery = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const rebass_1 = require("rebass");
const ExpressionEditor_1 = require("../../../components/ExpressionEditor");
const ExpressionPreview_1 = require("../../../components/ExpressionEditor/ExpressionPreview");
const Radio_1 = tslib_1.__importDefault(require("../../../components/Radio"));
const Tabs_1 = require("../../../components/Tabs");
const Tag_1 = require("../../../components/Tag");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const isValidReportRowsQuery = (report) => {
    if (report.ReportRowScope === 'ExpressionRows' && !report.Query?.BooleanExpression) {
        return 'Rows query cannot be empty';
    }
    return true;
};
exports.isValidReportRowsQuery = isValidReportRowsQuery;
const renderReportRowsSummary = (report) => {
    const { api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (React.createElement(rebass_1.Box, { fontSize: 2 },
        report.ReportRowScope === 'AllRows' ? (React.createElement(React.Fragment, null,
            "Include ",
            React.createElement("b", null, "all Rows"),
            " in the Report")) : null,
        report.ReportRowScope === 'VisibleRows' ? (React.createElement(React.Fragment, null,
            "Include ",
            React.createElement("b", null, "only visible Rows"),
            " in the Report")) : null,
        report.ReportRowScope === 'ExpressionRows' ? (React.createElement(React.Fragment, null,
            "Include Rows matching the query:",
            ' ',
            React.createElement(Tag_1.Tag, null,
                React.createElement(ExpressionPreview_1.ExpressionPreview, { query: report.Query })))) : null));
};
exports.renderReportRowsSummary = renderReportRowsSummary;
const ReportRowsWizardSection = (props) => {
    const { api, data, moduleInfo } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const initialData = (0, react_1.useMemo)(() => api.internalApi.getQueryPreviewData(), []);
    return (React.createElement(React.Fragment, null,
        React.createElement(Tabs_1.Tabs, { m: 2, value: data.ReportRowScope, onValueChange: (ReportRowScope) => {
                const report = {
                    ...data,
                    ReportRowScope,
                };
                if (report.ReportRowScope !== 'ExpressionRows') {
                    delete report.Query;
                }
                else {
                    if (!report.Query) {
                        report.Query = {
                            BooleanExpression: '',
                        };
                    }
                }
                props.onChange(report);
            } },
            React.createElement(Tabs_1.Tabs.Tab, { value: "AllRows" },
                React.createElement(Radio_1.default, { margin: 0, checked: data.ReportRowScope == 'AllRows', tabIndex: -1 }, "All Rows")),
            React.createElement(Tabs_1.Tabs.Tab, { value: "VisibleRows" },
                React.createElement(Radio_1.default, { margin: 0, checked: data.ReportRowScope == 'VisibleRows', tabIndex: -1 }, "Visible Rows")),
            React.createElement(Tabs_1.Tabs.Tab, { value: "ExpressionRows" },
                React.createElement(Radio_1.default, { margin: 0, checked: data.ReportRowScope == 'ExpressionRows', tabIndex: -1 }, "By Query")),
            React.createElement(Tabs_1.Tabs.Content, { value: "AllRows" },
                React.createElement(rebass_1.Text, { fontSize: 2, padding: 2 }, "All Rows in the datasource will be included in the report, whether visible or not at time of export.")),
            React.createElement(Tabs_1.Tabs.Content, { value: "VisibleRows" },
                React.createElement(rebass_1.Text, { fontSize: 2, padding: 2 }, "Only Rows that are visible at the time the Report is exported will be included in the Export")),
            React.createElement(Tabs_1.Tabs.Content, { value: "ExpressionRows" },
                React.createElement(rebass_1.Text, { fontSize: 2, padding: 2, pl: 0 }, "Only the Rows which match the Query will be exported - whether visible or not."),
                React.createElement(ExpressionEditor_1.ExpressionEditor, { allowSaveNamedQuery: true, type: 'boolean', module: moduleInfo.ModuleName, style: { paddingLeft: 0 }, value: data.Query?.BooleanExpression ?? '', onChange: (BooleanExpression) => {
                        props.onChange({
                            ...data,
                            Query: {
                                BooleanExpression,
                            },
                        });
                    }, initialData: initialData, columns: api.columnApi.internalApi.getQueryableColumnsForUIEditor(), fields: api.expressionApi.internalApi.getAvailableFields(), namedQueries: api.namedQueryApi.getNamedQueries(), api: api })))));
};
exports.ReportRowsWizardSection = ReportRowsWizardSection;
