"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CellSummaryViewPanelControl = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const EnumExtensions_1 = require("../../Utilities/Extensions/EnumExtensions");
const Enums_1 = require("../../AdaptableState/Common/Enums");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const CellSummaryPopover_1 = require("./CellSummaryPopover");
const GeneralConstants = tslib_1.__importStar(require("../../Utilities/Constants/GeneralConstants"));
const AdaptablePopover_1 = require("../AdaptablePopover");
const InternalRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/InternalRedux"));
const react_redux_1 = require("react-redux");
const Select_1 = require("../../components/Select");
const Flex_1 = require("../../components/Flex");
const clsx_1 = tslib_1.__importDefault(require("clsx"));
class CellSummaryViewPanelComponent extends React.Component {
    cleanupEvent;
    constructor(props) {
        super(props);
    }
    componentDidMount() {
        let adaptable = this.props.api.internalApi.getAdaptableInstance();
        if (adaptable) {
            this.cleanupEvent = adaptable._on('CellsSelected', () => {
                this.checkSelectedCells();
            });
        }
        this.checkSelectedCells();
    }
    componentWillUnmount() {
        this.cleanupEvent?.();
    }
    render() {
        let operationMenuItems = EnumExtensions_1.EnumExtensions.getNames(Enums_1.SummaryOperation).map((summaryOperation, index) => {
            return {
                label: summaryOperation,
                value: summaryOperation,
            };
        });
        const cellSummaryOperationDefinitions = this.props.api.cellSummaryApi.getCustomCellSummaryOperations();
        const operationDefinitions = ArrayExtensions_1.default.IsNullOrEmpty(cellSummaryOperationDefinitions)
            ? []
            : cellSummaryOperationDefinitions.map((operationDefinition) => {
                return {
                    value: operationDefinition.operationName,
                    label: operationDefinition.operationName,
                };
            });
        let cellSummaryPopover = React.createElement(CellSummaryPopover_1.CellSummaryPopover, { CellSummary: this.props.CellSummary });
        let shouldDisable = this.props.accessLevel == 'ReadOnly' || this.props.CellSummary == null;
        const isToolbar = this.props.viewType === 'Toolbar';
        const elementType = isToolbar ? 'DashboardToolbar' : 'ToolPanel';
        const operationValue = this.getOperationValue() ?? 'N/A';
        return (React.createElement(Flex_1.Flex, { className: (0, clsx_1.default)(shouldDisable ? GeneralConstants.READ_ONLY_STYLE : '', `ab-${elementType}__CellSummary__wrap twa:gap-2 twa:flex-row`, {
                'twa:min-w-[215px] twa:max-w-[215px] twa:w-[215px] twa:flex-nowrap': isToolbar,
                'twa:flex-1 twa:flex-wrap': !isToolbar,
            }) },
            React.createElement(Flex_1.Flex, { className: "twa:flex-1" },
                React.createElement(Select_1.Select, { "aria-label": "Cell Summary Operation Selector", className: `ab-${elementType}__CellSummary__select twa:w-full`, disabled: shouldDisable, options: [...operationMenuItems, ...operationDefinitions], onChange: (x) => this.props.onCellSummaryOperationChange(x), value: this.props.CellSummaryOperation })),
            React.createElement(Flex_1.Flex, { className: "twa:items-center twa:gap-1" }, React.createElement(React.Fragment, null,
                React.createElement(Flex_1.Flex, { className: (0, clsx_1.default)(`ab-${elementType}__CellSummary__value twa:min-w-[50px]`, {
                        'twa:rounded-standard twa:text-color-text-on-info twa:bg-color-info twa:text-2': !isToolbar,
                        'twa:flex-1 twa:text-color-text-on-primary twa:justify-center': isToolbar,
                    }) }, operationValue),
                React.createElement(AdaptablePopover_1.AdaptablePopover, { popoverMaxWidth: 360, className: "ab-ToolPanel__CellSummary__info", bodyText: [cellSummaryPopover], useButton: true, showEvent: 'focus', hideEvent: "blur", tooltipText: 'Show Cell Summaries', disabled: !this.props.CellSummary?.Count })))));
    }
    checkSelectedCells() {
        this.props.onCreateCellSummary();
    }
    getOperationValue() {
        return this.props.api.cellSummaryApi.getCellSummaryOperationValue(this.props.CellSummaryOperation);
    }
}
function mapStateToProps(state, ownProps) {
    return {
        SelectedCellInfo: state.Internal.SelectedCellInfo,
        CellSummaryOperation: state.Internal.CellSummary.CellSummaryOperation,
        CellSummary: state.Internal.CellSummary.CellSummaryInfo,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onCellSummaryOperationChange: (summaryOperation) => dispatch(InternalRedux.CellSummaryChangeOperation(summaryOperation)),
        onCreateCellSummary: () => dispatch(InternalRedux.CreateCellSummaryInfo()),
    };
}
exports.CellSummaryViewPanelControl = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(CellSummaryViewPanelComponent);
