import * as React from 'react';
import { connect } from 'react-redux';
import * as InternalRedux from '../../Redux/ActionsReducers/InternalRedux';
import { AdaptablePopover } from '../AdaptablePopover';
import { getAlertButtonStyle } from './Utilities/getAlertButtonStyle';
import { ActiveAlertsPanel } from './ActiveAlertsPanel';
import { Flex } from '../../components/Flex';
import { ButtonClear } from '../Components/Buttons/ButtonClear';
import clsx from 'clsx';
class AlertViewPanelComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            ShowMessage: false,
            Alerts: this.props.AdaptableAlerts,
        };
    }
    componentDidUpdate() {
        if (this.state.Alerts.length != this.props.AdaptableAlerts.length) {
            this.setState({ ShowMessage: true, Alerts: this.props.AdaptableAlerts });
        }
    }
    render() {
        const { color: buttonTextColor, background: buttonBackground } = getAlertButtonStyle(this.props.AdaptableAlerts);
        const collapsedText = this.props.AdaptableAlerts.length == 0
            ? '0 Alerts'
            : this.props.AdaptableAlerts.length == 1
                ? '1 Alert'
                : this.props.AdaptableAlerts.length + ' Alerts';
        const alertsPanel = React.createElement(ActiveAlertsPanel, null);
        const isToolbar = this.props.viewType === 'Toolbar';
        const elementType = isToolbar ? 'DashboardToolbar' : 'ToolPanel';
        return (React.createElement(Flex, { alignItems: "stretch", className: clsx(`ab-${elementType}__Alert__wrap twa:gap-1`, {
                'twa:min-w-[140px] twa:w-[140px]': isToolbar,
                [`twa:flex-1`]: !isToolbar,
            }) },
            React.createElement(Flex, { key: `${buttonTextColor}_${buttonBackground}_${collapsedText}`, className: `ab-${elementType}__Alert__text twa:flex-1 twa:rounded-standard twa:p-2 text-2 twa:items-center twa:justify-center twa:min-h-input`, style: { color: buttonTextColor, backgroundColor: buttonBackground } }, collapsedText),
            this.props.AdaptableAlerts.length > 0 && (React.createElement(Flex, { className: 'twa:gap-1' },
                React.createElement(Flex, { className: "twa:flex twa:box-border" },
                    React.createElement(ButtonClear, { "aria-label": 'Clear All Alerts', variant: 'outlined', className: `ab-${elementType}__Alert__clear`, onClick: () => this.props.onDeleteAllAlert(this.state.Alerts), tooltip: "Clear All Alerts", showText: this.props.viewType === 'ToolPanel' }, 'Clear Alerts')),
                React.createElement(AdaptablePopover, { className: `ab-${elementType}__Alert__info`, headerText: "Alerts Details", bodyText: [alertsPanel], MessageType: 'Info', useButton: true, showEvent: 'focus', hideEvent: "blur", popoverMinWidth: 400 })))));
    }
}
function mapStateToProps(state, ownProps) {
    return {
        AlertDefinitions: state.Alert.AlertDefinitions,
        AdaptableAlerts: state.Internal.AdaptableAlerts,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onDeleteAlert: (alert) => dispatch(InternalRedux.AdaptableAlertDelete(alert)),
        onDeleteAllAlert: (alerts) => dispatch(InternalRedux.AdaptableAlertDeleteAll(alerts)),
    };
}
export const AlertViewPanelControl = connect(mapStateToProps, mapDispatchToProps)(AlertViewPanelComponent);
