"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BulkUpdateViewPanelControl = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const Enums_1 = require("../../AdaptableState/Common/Enums");
const PreviewResultsPanel_1 = require("../Components/PreviewResultsPanel");
const rebass_1 = require("rebass");
const join_1 = tslib_1.__importDefault(require("../../components/utils/join"));
const GeneralConstants = tslib_1.__importStar(require("../../Utilities/Constants/GeneralConstants"));
const BulkUpdateValueSelector_1 = require("../Components/Selectors/BulkUpdateValueSelector");
const StringExtensions_1 = require("../../Utilities/Extensions/StringExtensions");
const ButtonApply_1 = require("../Components/Buttons/ButtonApply");
const AdaptablePopover_1 = require("../AdaptablePopover");
const UIHelper_1 = require("../UIHelper");
const InternalRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/InternalRedux"));
const BulkUpdateRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/BulkUpdateRedux"));
const react_redux_1 = require("react-redux");
class BulkUpdateViewPanelComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            Disabled: true,
        };
    }
    componentDidMount() {
        if (this.props.api) {
            let 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 = this.props.BulkUpdateValidationResult.Column;
        let previewPanel = (React.createElement(PreviewResultsPanel_1.PreviewResultsPanel, { previewInfo: this.props.PreviewInfo, api: this.props.api, selectedColumn: selectedColumn, showPanel: true, showHeader: false }));
        let shouldDisable = this.props.accessLevel == 'ReadOnly' ||
            !this.props.BulkUpdateValidationResult.IsValid ||
            this.props.api.layoutApi.isCurrentLayoutPivot() == true;
        const applyStyle = {
            color: statusColour,
            fill: 'currentColor',
        };
        const elementType = this.props.viewType === 'Toolbar' ? 'DashboardToolbar' : 'ToolPanel';
        return (React.createElement(rebass_1.Flex, { minWidth: 150, flexDirection: "row", className: (0, join_1.default)(shouldDisable ? GeneralConstants.READ_ONLY_STYLE : '', `ab-${elementType}__BulkUpdate__wrap`), flexWrap: this.props.viewType === 'ToolPanel' ? 'wrap' : 'nowrap' },
            React.createElement(rebass_1.Flex, null,
                React.createElement(BulkUpdateValueSelector_1.BulkUpdateValueSelector, { selectedGridCells: this.props.SelectedGridCells, newLabel: "New", existingLabel: "Existing", dropdownButtonProps: {
                        listMinWidth: 160,
                    }, className: `ab-${elementType}__BulkUpdate__select`, disabled: shouldDisable, selectedColumnValue: this.props.BulkUpdateValue, selectedColumn: selectedColumn, api: this.props.api, onColumnValueChange: (columns) => this.onColumnValueSelectedChanged(columns) })),
            React.createElement(rebass_1.Flex, null,
                !shouldDisable && StringExtensions_1.StringExtensions.IsNotNullOrEmpty(this.props.BulkUpdateValue) && (React.createElement(ButtonApply_1.ButtonApply, { marginLeft: 2, className: `ab-${elementType}__BulkUpdate__apply`, onClick: () => this.onApplyClick(), style: applyStyle, tooltip: "Apply Bulk Update", disabled: StringExtensions_1.StringExtensions.IsNullOrEmpty(this.props.BulkUpdateValue) ||
                        (this.props.PreviewInfo != null &&
                            this.props.PreviewInfo.previewValidationSummary.validationResult == 'All'), accessLevel: this.props.accessLevel }, this.props.viewType === 'ToolPanel' && 'Update')),
                !shouldDisable && StringExtensions_1.StringExtensions.IsNotNullOrEmpty(this.props.BulkUpdateValue) && (React.createElement(AdaptablePopover_1.AdaptablePopover, { popoverMinWidth: 360, className: `ab-${elementType}__BulkUpdate__info`, headerText: "Preview Results", bodyText: [previewPanel], MessageType: UIHelper_1.UIHelper.getMessageTypeByStatusColour(statusColour), useButton: true, showEvent: 'focus', hideEvent: "blur" })))));
    }
    onColumnValueSelectedChanged(selectedColumnValue) {
        this.props.onBulkUpdateValueChange(selectedColumnValue);
    }
    checkSelectedCells() {
        this.props.onBulkUpdateCheckSelectedCells();
        if (StringExtensions_1.StringExtensions.IsNotNullOrEmpty(this.props.BulkUpdateValue)) {
            this.props.onBulkUpdateValueChange('');
        }
    }
    getStatusColour() {
        if (StringExtensions_1.StringExtensions.IsNotNullOrEmpty(this.props.BulkUpdateValue) && 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.props.onApplyBulkUpdate();
        this.props.onBulkUpdateValueChange(undefined);
    }
}
function mapStateToProps(state, ownProps) {
    return {
        SelectedGridCells: state.Internal.SelectedCellInfo?.gridCells,
        BulkUpdateValue: state.Internal.BulkUpdate.BulkUpdateValue,
        BulkUpdateValidationResult: state.Internal.BulkUpdate.BulkUpdateValidationResult,
        PreviewInfo: state.Internal.BulkUpdate.BulkUpdatePreviewInfo,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onBulkUpdateValueChange: (value) => dispatch(InternalRedux.BulkUpdateChangeValue(value)),
        onBulkUpdateCheckSelectedCells: () => dispatch(InternalRedux.BulkUpdateCheckCellSelection()),
        onApplyBulkUpdate: () => dispatch(BulkUpdateRedux.BulkUpdateApply(false)),
    };
}
exports.BulkUpdateViewPanelControl = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(BulkUpdateViewPanelComponent);
