"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataSetViewPanelControl = void 0;
const tslib_1 = require("tslib");
const InternalRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/InternalRedux"));
const React = tslib_1.__importStar(require("react"));
const StringExtensions_1 = require("../../Utilities/Extensions/StringExtensions");
const rebass_1 = require("rebass");
const react_redux_1 = require("react-redux");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const Select_1 = require("../../components/Select");
class DataSetViewPanelComponent extends React.Component {
    getCurrentDataSet() {
        return StringExtensions_1.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(rebass_1.Flex, { flexDirection: "row", className: `ab-${elementType}__DataSet__wrap` },
            React.createElement(Select_1.Select, { style: { width: '100%' }, placeholder: 'Select Data Set', disabled: ArrayExtensions_1.default.IsNullOrEmpty(availableDataSets), options: availableDataSets, value: currentDataSetName, className: `ab-${elementType}__DataSet__select`, onChange: (destination) => this.onSelectedDataSetChanged(destination) })));
    }
    onSelectedDataSetChanged(dataSetName) {
        if (StringExtensions_1.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));
        },
    };
}
exports.DataSetViewPanelControl = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(DataSetViewPanelComponent);
