"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressionWizard = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const CheckBox_1 = require("../../components/CheckBox");
const ExpressionEditor_1 = require("../../components/ExpressionEditor");
const Input_1 = tslib_1.__importDefault(require("../../components/Input"));
const StringExtensions_1 = require("../../Utilities/Extensions/StringExtensions");
const ModuleConstants_1 = require("../../Utilities/Constants/ModuleConstants");
const react_1 = require("react");
class ExpressionWizard extends React.Component {
    constructor(props) {
        super(props);
        this.handleCustomExpressionChange = (expression) => {
            this.setState({
                expression,
            }, () => this.props.updateGoBackState());
        };
        this.state = {
            // this is realy horrible but its only way to use the Expression Wizard for both Report (that has Query) and Format Column (that has Rule)
            // once we move to the new Wizard we can remove this monstrosity
            expression: this.props.callingModule && this.props.callingModule == 'Export'
                ? this.props.data.Query?.BooleanExpression
                : this.props.data.Rule?.BooleanExpression,
            saveToNamedQueries: false,
            newNamedQuery: '',
        };
    }
    render() {
        const initialData = (0, react_1.useMemo)(() => this.props.api.internalApi.getQueryPreviewData(), []);
        return (React.createElement(React.Fragment, null,
            React.createElement(ExpressionEditor_1.ExpressionEditor, { type: 'boolean', module: ModuleConstants_1.NamedQueryModuleId, value: this.state.expression, onChange: this.handleCustomExpressionChange, initialData: initialData, columns: this.props.api.columnApi.getQueryableColumns(), fields: this.props.api.expressionApi.internalApi.getAvailableFields(), namedQueries: this.props.api.namedQueryApi.getNamedQueries(), api: this.props.api }),
            ' ',
            React.createElement(rebass_1.Flex, { flexDirection: "row", padding: 1, marginBottom: 2, marginLeft: 1, alignItems: "center", "data-name": "expression-wizard-save-option" },
                React.createElement(CheckBox_1.CheckBox, { marginLeft: 2, disabled: !this.isValidExpression(), marginBottom: 2, checked: this.state.saveToNamedQueries, onChange: (checked) => this.setState({
                        saveToNamedQueries: checked,
                    }, () => this.props.updateGoBackState()) }, "Save as new Named Query"),
                this.state.saveToNamedQueries && (React.createElement(Input_1.default, { marginLeft: 2, style: { minWidth: '20rem' }, value: this.state.newNamedQuery, onChange: (e) => this.setState({
                        newNamedQuery: e.target.value,
                    }, () => this.props.updateGoBackState()) })))));
    }
    isValidExpression() {
        return (StringExtensions_1.StringExtensions.IsNotNullOrEmpty(this.state.expression) &&
            this.props.api.internalApi
                .getQueryLanguageService()
                .validateBoolean(this.state.expression, ModuleConstants_1.NamedQueryModuleId).isValid);
    }
    canNext() {
        if (this.isValidExpression() == false) {
            return false;
        }
        if (this.state.saveToNamedQueries && StringExtensions_1.StringExtensions.IsNullOrEmpty(this.state.newNamedQuery)) {
            return false;
        }
        return true;
    }
    canBack() {
        return true;
    }
    next() {
        this.props.callingModule == 'Export'
            ? (this.props.data.Query = {
                BooleanExpression: this.state.expression,
            })
            : (this.props.data.Rule = {
                BooleanExpression: this.state.expression,
            });
        if (this.state.saveToNamedQueries) {
            this.props.onSetNewNamedQuery(this.state.newNamedQuery);
        }
    }
    back() { }
    getIndexStepIncrement() {
        return 1;
    }
    getIndexStepDecrement() {
        return 1;
    }
}
exports.ExpressionWizard = ExpressionWizard;
