import * as React from 'react';
import { StatusColour } from '../../AdaptableState/Common/Enums';
import { PreviewResultsPanel } from '../Components/PreviewResultsPanel';
import * as GeneralConstants from '../../Utilities/Constants/GeneralConstants';
import { BulkUpdateValueSelector } from '../Components/Selectors/BulkUpdateValueSelector';
import { StringExtensions } from '../../Utilities/Extensions/StringExtensions';
import { ButtonApply } from '../Components/Buttons/ButtonApply';
import { AdaptablePopover } from '../AdaptablePopover';
import { UIHelper } from '../UIHelper';
import * as InternalRedux from '../../Redux/ActionsReducers/InternalRedux';
import * as BulkUpdateRedux from '../../Redux/ActionsReducers/BulkUpdateRedux';
import { connect } from 'react-redux';
import { Flex } from '../../components/Flex';
import clsx from 'clsx';
class BulkUpdateViewPanelComponent extends React.Component {
    cleanupEvent;
    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, { previewInfo: this.props.PreviewInfo, api: this.props.api, selectedColumn: selectedColumn, showPanel: true, showHeader: false }));
        const valueSelectorDisabled = this.props.accessLevel == 'ReadOnly' ||
            !this.props.BulkUpdateValidationResult.IsValid ||
            this.props.api.layoutApi.isCurrentLayoutPivot();
        const valueOperationDisabled = valueSelectorDisabled ||
            StringExtensions.IsNullOrEmpty(this.props.BulkUpdateValue) ||
            (this.props.PreviewInfo != null &&
                this.props.PreviewInfo.previewValidationSummary.validationResult == 'All');
        const applyStyle = {
            color: statusColour,
            fill: 'currentColor',
        };
        const isToolbar = this.props.viewType === 'Toolbar';
        const elementType = isToolbar ? 'DashboardToolbar' : 'ToolPanel';
        const messageStyle = UIHelper.getMessageTypeByStatusColour(statusColour);
        const infoStyle = messageStyle === 'Success' ? 'Info' : messageStyle;
        return (React.createElement(Flex, { className: clsx(valueSelectorDisabled ? GeneralConstants.READ_ONLY_STYLE : '', `ab-${elementType}__BulkUpdate__wrap twa:gap-1 twa:flex-row`, {
                'twa:min-w-[300px] twa:max-w-[300px] twa:w-[300px] twa:flex-nowrap': isToolbar,
                'twa:flex-1 twa:flex-wrap': !isToolbar,
            }), flexWrap: isToolbar ? 'nowrap' : 'wrap' },
            React.createElement(Flex, { className: clsx('twa:flex-1', {
                    'twa:min-w-[100px]': !isToolbar,
                    'twa:min-w-0': isToolbar,
                }) },
                React.createElement(BulkUpdateValueSelector, { selectedGridCells: this.props.SelectedGridCells, newLabel: "New", existingLabel: "Existing", dropdownButtonProps: {
                        listMinWidth: 160,
                    }, className: `ab-${elementType}__BulkUpdate__select twa:w-full`, disabled: valueSelectorDisabled, selectedColumnValue: this.props.BulkUpdateValue, selectedColumn: selectedColumn, api: this.props.api, onColumnValueChange: (columns) => this.onColumnValueSelectedChanged(columns) })),
            React.createElement(Flex, { className: "twa:flex-shrink-0 twa:gap-1" },
                React.createElement(Flex, { className: "twa:flex twa:box-border twa:items-center" },
                    React.createElement(ButtonApply, { className: `ab-${elementType}__BulkUpdate__apply twa:h-full`, onClick: () => this.onApplyClick(), style: applyStyle, tooltip: "Apply Bulk Update", disabled: valueOperationDisabled, accessLevel: this.props.accessLevel }, 'Apply')),
                React.createElement(AdaptablePopover, { popoverMinWidth: 360, className: `ab-${elementType}__BulkUpdate__info`, headerText: "Preview Results", bodyText: [previewPanel], MessageType: infoStyle, useButton: true, showEvent: 'focus', hideEvent: "blur", disabled: valueSelectorDisabled || StringExtensions.IsNullOrEmpty(this.props.BulkUpdateValue) }))));
    }
    onColumnValueSelectedChanged(selectedColumnValue) {
        this.props.onBulkUpdateValueChange(selectedColumnValue);
    }
    checkSelectedCells() {
        this.props.onBulkUpdateCheckSelectedCells();
        if (StringExtensions.IsNotNullOrEmpty(this.props.BulkUpdateValue)) {
            this.props.onBulkUpdateValueChange('');
        }
    }
    getStatusColour() {
        if (StringExtensions.IsNotNullOrEmpty(this.props.BulkUpdateValue) && this.props.PreviewInfo) {
            if (this.props.PreviewInfo.previewValidationSummary.validationResult == 'All') {
                return StatusColour.Error;
            }
            if (this.props.PreviewInfo.previewValidationSummary.validationResult == 'Some') {
                return StatusColour.Warn;
            }
        }
        return 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)),
    };
}
export let BulkUpdateViewPanelControl = connect(mapStateToProps, mapDispatchToProps)(BulkUpdateViewPanelComponent);
