import * as InternalRedux from '../../Redux/ActionsReducers/InternalRedux';
import * as React from 'react';
import { StringExtensions } from '../../Utilities/Extensions/StringExtensions';
import { connect } from 'react-redux';
import ArrayExtensions from '../../Utilities/Extensions/ArrayExtensions';
import { Select } from '../../components/Select';
import { Flex } from '../../components/Flex';
class DataSetViewPanelComponent extends React.Component {
    getCurrentDataSet() {
        return StringExtensions.IsNullOrEmpty(this.props.CurrentDataSetName)
            ? null
            : this.getDataSets().find((ds) => ds.name == this.props.CurrentDataSetName);
    }
    getDataSets() {
        return this.props.api.dataSetApi.getDataSets();
    }
    render() {
        const selectDataSetString = 'Select Data Set';
        let currentDataSetName = this.getCurrentDataSet()?.name ?? selectDataSetString;
        let availableDataSets = this.getDataSets().map((dataSet) => {
            return {
                value: dataSet.name,
                label: dataSet.name,
                onClick: () => this.onSelectedDataSetChanged(dataSet.name),
            };
        });
        const elementType = this.props.viewType === 'Toolbar' ? 'DashboardToolbar' : 'ToolPanel';
        return (React.createElement(Flex, { flexDirection: "row", className: `ab-${elementType}__DataSet__wrap` },
            React.createElement(Select, { "aria-label": 'Select Data Set', placeholder: 'Select Data Set', disabled: ArrayExtensions.IsNullOrEmpty(availableDataSets), options: availableDataSets, value: currentDataSetName, className: `ab-${elementType}__DataSet__select twa:w-full`, onChange: (destination) => this.onSelectedDataSetChanged(destination) })));
    }
    onSelectedDataSetChanged(dataSetName) {
        if (StringExtensions.IsNullOrEmpty(dataSetName)) {
            this.setState({ CurrentDataSet: null });
        }
        else {
            let newDataSet = this.getDataSets().find((ds) => ds.name == dataSetName);
            this.setState({ CurrentDataSet: newDataSet });
            this.props.onSelectDataSet(newDataSet);
        }
    }
}
function mapStateToProps(state) {
    return {
        CurrentDataSetName: state.Internal.CurrentDataSet,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onSelectDataSet: (dataSet) => {
            return dispatch(InternalRedux.DataSetSelect(dataSet));
        },
    };
}
export let DataSetViewPanelControl = connect(mapStateToProps, mapDispatchToProps)(DataSetViewPanelComponent);
