"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmartEditPopup = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_redux_1 = require("react-redux");
const SmartEditRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/SmartEditRedux"));
const InternalRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/InternalRedux"));
const Enums_1 = require("../../AdaptableState/Common/Enums");
const PanelWithImage_1 = require("../Components/Panels/PanelWithImage");
const AdaptablePopover_1 = require("../AdaptablePopover");
const EnumExtensions_1 = require("../../Utilities/Extensions/EnumExtensions");
const PreviewResultsPanel_1 = require("../Components/PreviewResultsPanel");
const PreviewHelper_1 = require("../../Utilities/Helpers/PreviewHelper");
const StringExtensions_1 = require("../../Utilities/Extensions/StringExtensions");
const Input_1 = tslib_1.__importDefault(require("../../components/Input"));
const rebass_1 = require("rebass");
const DropdownButton_1 = tslib_1.__importDefault(require("../../components/DropdownButton"));
const SimpleButton_1 = tslib_1.__importDefault(require("../../components/SimpleButton"));
const preventDefault = (e) => e.preventDefault();
class SmartEditPopupComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = { isShowingError: false, errorText: '' };
    }
    componentDidMount() {
        this.props.onSmartEditCheckSelectedCells();
    }
    render() {
        let col;
        if (this.props.PreviewInfo) {
            col = this.props.PreviewInfo.column;
        }
        let globalValidationMessage = PreviewHelper_1.PreviewHelper.GetValidationMessage(this.props.PreviewInfo, `${this.props.SmartEditValue}`);
        let showPanel = this.props.PreviewInfo && StringExtensions_1.StringExtensions.IsNotNullOrEmpty(`${this.props.SmartEditValue}`);
        let previewPanel = showPanel ? (React.createElement(PreviewResultsPanel_1.PreviewResultsPanel, { style: { flex: '1 1 100%', overflow: 'initial', height: '100%' }, previewInfo: this.props.PreviewInfo, api: this.props.api, selectedColumn: col, showPanel: showPanel, showHeader: true })) : null;
        const operationMenuItems = EnumExtensions_1.EnumExtensions.getNames(Enums_1.MathOperation).map((mathOperation, index) => {
            return {
                label: mathOperation,
                onClick: () => {
                    this.props.onSmartEditOperationChange(mathOperation);
                },
            };
        });
        const customOperations = this.props.api.smartEditApi.getSmartEditCustomOperations();
        if (customOperations?.length) {
            operationMenuItems.push(...customOperations.map((operation) => {
                return {
                    onClick: () => this.props.onSmartEditOperationChange(operation),
                    label: operation.name,
                };
            }));
        }
        return (React.createElement(PanelWithImage_1.PanelWithImage, { flex: 1, style: { height: '100%' }, variant: "primary", bodyProps: {
                style: { display: 'flex', flexFlow: 'column' },
            }, bodyScroll: true, glyphicon: this.props.moduleInfo.Glyph, header: this.props.moduleInfo.FriendlyName, infoLink: this.props.moduleInfo.HelpPage, infoLinkDisabled: !this.props.api.internalApi.isDocumentationLinksDisplayed(), onKeyDown: (e) => {
                if (e.key === 'Enter') {
                    this.submit();
                }
            } },
            React.createElement(rebass_1.Flex, { flexDirection: "row", padding: 2, "data-name": "smart-edit-operation" },
                React.createElement(DropdownButton_1.default, { "data-name": "smart-edit-operation-dropdown", items: operationMenuItems, columns: ['label'], onMouseDown: preventDefault }, typeof this.props.SmartEditOperation === 'object'
                    ? this.props.SmartEditOperation.name
                    : this.props.SmartEditOperation),
                React.createElement(Input_1.default, { "data-name": "smart-edit-value", value: this.props.SmartEditValue.toString(), marginLeft: 2, marginRight: 2, type: "number", placeholder: "Enter a Number", onChange: (e) => this.onSmartEditValueChange(e) }),
                React.createElement(SimpleButton_1.default, { "data-name": "smart-edit-apply-button", tone: this.getButtonStyle(), variant: "raised", disabled: StringExtensions_1.StringExtensions.IsNullOrEmpty(`${this.props.SmartEditValue}`) ||
                        (this.props.PreviewInfo &&
                            this.props.PreviewInfo.previewValidationSummary.validationResult == 'All'), onClick: () => {
                        this.submit();
                    }, marginRight: 2 }, "Apply Smart Edit"),
                ' ',
                this.props.PreviewInfo &&
                    this.props.PreviewInfo.previewValidationSummary.validationResult != 'None' && (React.createElement(AdaptablePopover_1.AdaptablePopover, { headerText: 'Validation Error', bodyText: [globalValidationMessage], MessageType: 'Error' }))),
            previewPanel));
    }
    submit() {
        this.props.onApplySmartEdit();
    }
    onSmartEditValueChange(event) {
        const e = event.target;
        this.props.onSmartEditValueChange(Number(e.value));
    }
    getButtonStyle() {
        if (this.props.PreviewInfo) {
            if (this.props.PreviewInfo.previewValidationSummary.validationResult == 'All') {
                return 'neutral';
            }
            if (this.props.PreviewInfo.previewValidationSummary.validationResult == 'Some') {
                return 'error';
            }
        }
        return 'success';
    }
}
function mapStateToProps(state, ownProps) {
    return {
        SmartEditValue: state.Internal.SmartEdit.SmartEditValue,
        SmartEditOperation: state.Internal.SmartEdit.SmartEditOperation,
        PreviewInfo: state.Internal.SmartEdit.SmartEditPreviewInfo,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onSmartEditValueChange: (value) => dispatch(InternalRedux.SmartEditChangeValue(value)),
        onSmartEditOperationChange: (SmartEditOperation) => dispatch(InternalRedux.SmartEditChangeOperation(SmartEditOperation)),
        onSmartEditCheckSelectedCells: () => dispatch(InternalRedux.SmartEditCheckCellSelection()),
        onApplySmartEdit: () => dispatch(SmartEditRedux.SmartEditApply(false)),
    };
}
exports.SmartEditPopup = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(SmartEditPopupComponent);
