"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmartEditViewPanelControl = void 0;
const tslib_1 = require("tslib");
const InternalRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/InternalRedux"));
const SmartEditRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/SmartEditRedux"));
const Enums_1 = require("../../AdaptableState/Common/Enums");
const React = tslib_1.__importStar(require("react"));
const StringExtensions_1 = require("../../Utilities/Extensions/StringExtensions");
const PreviewResultsPanel_1 = require("../Components/PreviewResultsPanel");
const EnumExtensions_1 = require("../../Utilities/Extensions/EnumExtensions");
const rebass_1 = require("rebass");
const Input_1 = tslib_1.__importDefault(require("../../components/Input"));
const ButtonApply_1 = require("../Components/Buttons/ButtonApply");
const AdaptablePopover_1 = require("../AdaptablePopover");
const UIHelper_1 = require("../UIHelper");
const react_redux_1 = require("react-redux");
const Select_1 = require("../../components/Select");
class SmartEditViewPanelComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            SelectedColumnId: '',
        };
    }
    componentDidMount() {
        if (this.props.api) {
            const adaptable = this.props.api.internalApi.getAdaptableInstance();
            if (adaptable) {
                this.cleanupEvent = adaptable._on('CellsSelected', () => {
                    this.checkSelectedCells();
                });
            }
        }
        this.checkSelectedCells();
    }
    componentWillUnmount() {
        this.cleanupEvent?.();
    }
    render() {
        let statusColour = this.getStatusColour();
        let selectedColumn = StringExtensions_1.StringExtensions.IsNotNullOrEmpty(this.state.SelectedColumnId)
            ? this.props.api.columnApi.getColumnWithColumnId(this.state.SelectedColumnId)
            : null;
        let previewPanel = (React.createElement(PreviewResultsPanel_1.PreviewResultsPanel, { previewInfo: this.props.PreviewInfo, api: this.props.api, selectedColumn: selectedColumn, showPanel: true, showHeader: false }));
        const operationMenuItems = EnumExtensions_1.EnumExtensions.getNames(Enums_1.MathOperation).map((mathOperation, index) => {
            return {
                onClick: () => this.props.onSmartEditOperationChange(mathOperation),
                value: mathOperation,
                label: 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,
                    value: operation,
                };
            }));
        }
        const applyButtonStyle = {
            color: statusColour,
            fill: 'currentColor',
        };
        let shouldDisable = this.props.accessLevel == 'ReadOnly' ||
            !this.props.IsValidSelection ||
            this.props.api.layoutApi.isCurrentLayoutPivot() == true;
        const elementType = this.props.viewType === 'Toolbar' ? 'DashboardToolbar' : 'ToolPanel';
        return (React.createElement(rebass_1.Flex, { flexDirection: "row", className: `ab-${elementType}__SmartEdit__wrap`, flexWrap: this.props.viewType === 'ToolPanel' ? 'wrap' : 'nowrap' },
            React.createElement(rebass_1.Flex, null,
                React.createElement(Select_1.Select, { "aria-label": 'Select Smart Edit Operation', value: typeof this.props.SmartEditOperation === 'object'
                        ? this.props.SmartEditOperation.name
                        : this.props.SmartEditOperation, options: operationMenuItems, onChange: (operation) => this.props.onSmartEditOperationChange(operation), style: { marginRight: 1 } }),
                React.createElement(Input_1.default, { style: {
                        width: '5rem',
                    }, className: `ab-${elementType}__SmartEdit__select-value`, value: this.props.SmartEditValue.toString(), type: "number", placeholder: "Enter a Number", step: "any", onChange: (e) => this.onSmartEditValueChange(e), disabled: shouldDisable })),
            React.createElement(rebass_1.Flex, null,
                !shouldDisable && (React.createElement(ButtonApply_1.ButtonApply, { onClick: () => this.onApplyClick(), style: applyButtonStyle, className: `ab-${elementType}__SmartEdit__apply`, tooltip: "Apply Smart Edit", disabled: StringExtensions_1.StringExtensions.IsNullOrEmpty(`${this.props.SmartEditValue}`) ||
                        (this.props.PreviewInfo != null &&
                            this.props.PreviewInfo.previewValidationSummary.validationResult == 'All'), accessLevel: this.props.accessLevel }, this.props.viewType === 'ToolPanel' && 'Apply Smart Edit')),
                !shouldDisable && (React.createElement(AdaptablePopover_1.AdaptablePopover, { headerText: "Preview Results", className: `ab-${elementType}__SmartEdit__info`, 
                    //  tooltipText="Preview Results"
                    bodyText: [previewPanel], MessageType: UIHelper_1.UIHelper.getMessageTypeByStatusColour(statusColour), useButton: true, showEvent: 'focus', hideEvent: "blur" })))));
    }
    checkSelectedCells() {
        this.props.onSmartEditCheckSelectedCells();
    }
    onSmartEditValueChange(event) {
        const e = event.target;
        this.props.onSmartEditValueChange(Number(e.value));
    }
    getStatusColour() {
        if (StringExtensions_1.StringExtensions.IsNotNullOrEmpty(`${this.props.SmartEditValue}`) &&
            this.props.PreviewInfo) {
            if (this.props.PreviewInfo.previewValidationSummary.validationResult == 'All') {
                return Enums_1.StatusColour.Error;
            }
            if (this.props.PreviewInfo.previewValidationSummary.validationResult == 'Some') {
                return Enums_1.StatusColour.Warn;
            }
        }
        return Enums_1.StatusColour.Success;
    }
    onApplyClick() {
        this.onApplySmartEdit();
    }
    onApplySmartEdit() {
        this.props.onApplySmartEdit();
    }
}
function mapStateToProps(state, ownProps) {
    return {
        SmartEditValue: state.Internal.SmartEdit.SmartEditValue,
        SmartEditOperation: state.Internal.SmartEdit.SmartEditOperation,
        IsValidSelection: state.Internal.SmartEdit.IsValidSmartEditSelection,
        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.SmartEditViewPanelControl = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(SmartEditViewPanelComponent);
