import * as React from 'react';
import { WizardStatus, } from '../Components/SharedProps/EditableConfigEntityState';
import { connect } from 'react-redux';
import { Helper } from '../../Utilities/Helpers/Helper';
import * as CustomSortRedux from '../../Redux/ActionsReducers/CustomSortRedux';
import { ObjectFactory } from '../../Utilities/ObjectFactory';
import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants';
import { ModuleHeader } from '../Components/ModuleSummary/ModuleHeader';
import { ModuleDetail } from '../Components/ModuleSummary/ModuleDetail';
import { ModuleProfile } from '../Components/ModuleProfile';
import * as TeamSharingRedux from '../../Redux/ActionsReducers/TeamSharingRedux';
import { UIHelper } from '../UIHelper';
import { StringExtensions } from '../../Utilities/Extensions/StringExtensions';
import { ArrayExtensions } from '../../Utilities/Extensions/ArrayExtensions';
import { CustomSortWizard } from './Wizard/CustomSortWizard';
export class CustomSortSummaryComponent extends React.Component {
    constructor(props) {
        super(props);
        this.onFinishWizard = (customSort) => {
            if (this.props.CustomSorts.find((x) => x.ColumnId == customSort.ColumnId)) {
                this.props.onEditCustomSort(customSort);
            }
            else {
                this.props.onAddCustomSort(customSort);
            }
            this.setState({
                editedAdaptableObject: null,
                wizardStartIndex: 0,
                wizardStatus: WizardStatus.None,
            });
        };
        this.state = UIHelper.getEmptyConfigState();
    }
    render() {
        const customSort = this.props.CustomSorts.find((c) => c.ColumnId == this.props.summarisedColumn.columnId);
        const columnSortComparer = this.props.api.customSortApi.internalApi.getCustomSortComparer(this.props.summarisedColumn.columnId);
        let noCustomSort = customSort == null && columnSortComparer == null;
        let customSortRow;
        if (!this.props.summarisedColumn.sortable) {
            customSortRow = (React.createElement(ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: 'Column is not sortable', newButtonDisabled: true, onNew: () => this.onNew(), newButtonTooltip: this.props.moduleInfo.FriendlyName, accessLevel: this.props.accessLevel }));
        }
        else if (noCustomSort) {
            // title row
            customSortRow = (React.createElement(ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: 'No Custom Sort Set', onNew: () => this.onNew(), accessLevel: this.props.accessLevel, newButtonTooltip: this.props.moduleInfo.FriendlyName }));
        }
        else if (columnSortComparer != null) {
            customSortRow = (React.createElement(ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: 'Using a Custom Sort Comparer Function', onNew: () => this.onNew(), accessLevel: this.props.accessLevel, newButtonTooltip: this.props.moduleInfo.FriendlyName }));
        }
        else {
            customSortRow = (React.createElement(ModuleDetail, { key: this.props.moduleInfo.FriendlyName, item1: React.createElement(ModuleProfile, { moduleInfo: this.props.moduleInfo }), item2: this.getCustomSortedValues(customSort), configEnity: customSort, moduleInfo: this.props.moduleInfo, onEdit: () => this.onEdit(customSort), showEdit: customSort != undefined, onShare: (config) => this.props.onShare(customSort, config), showShare: this.props.teamSharingActivated, onDelete: CustomSortRedux.CustomSortDelete(customSort), showBold: true, accessLevel: this.props.accessLevel }));
        }
        return (React.createElement(React.Fragment, null,
            customSortRow,
            this.state.editedAdaptableObject && (React.createElement(CustomSortWizard, { isNew: this.state.wizardStatus === WizardStatus.New, moduleInfo: this.props.moduleInfo, data: this.state.editedAdaptableObject, configEntities: this.props.CustomSorts, onCloseWizard: () => this.onCloseWizard(), onFinishWizard: this.onFinishWizard }))));
    }
    onNew() {
        let configEntity = ObjectFactory.CreateEmptyCustomSort();
        configEntity.ColumnId = this.props.summarisedColumn.columnId;
        this.setState({
            editedAdaptableObject: configEntity,
            wizardStartIndex: 1,
            wizardStatus: WizardStatus.New,
        });
    }
    onEdit(customSort) {
        this.setState({
            editedAdaptableObject: Helper.cloneObject(customSort),
            wizardStartIndex: 1,
            wizardStatus: WizardStatus.Edit,
        });
    }
    onCloseWizard() {
        this.setState({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: WizardStatus.None,
        });
    }
    canFinishWizard() {
        let customSort = this.state.editedAdaptableObject;
        return (StringExtensions.IsNotNullOrEmpty(customSort.ColumnId) &&
            ArrayExtensions.IsNotNullOrEmpty(customSort.SortedValues));
    }
    getCustomSortedValues(customSort) {
        if (ArrayExtensions.IsNotNullOrEmpty(customSort.SortedValues)) {
            return customSort.SortedValues.join(', ');
        }
        else {
            return '[Has bespoke Custom Sort implementing using bespoke function]';
        }
    }
}
function mapStateToProps(state, ownProps) {
    return {
        CustomSorts: state.CustomSort.CustomSorts,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onAddCustomSort: (customSort) => dispatch(CustomSortRedux.CustomSortAdd(customSort)),
        onEditCustomSort: (customSort) => dispatch(CustomSortRedux.CustomSortEdit(customSort)),
        onShare: (entity, config) => dispatch(TeamSharingRedux.TeamSharingShare(entity, ModuleConstants.CustomSortModuleId, config)),
    };
}
export let CustomSortSummary = connect(mapStateToProps, mapDispatchToProps)(CustomSortSummaryComponent);
