import * as React from 'react';
import { WizardStatus, } from '../Components/SharedProps/EditableConfigEntityState';
import { connect } from 'react-redux';
import { Helper } from '../../Utilities/Helpers/Helper';
import * as PlusMinusRedux from '../../Redux/ActionsReducers/PlusMinusRedux';
import * as PopupRedux from '../../Redux/ActionsReducers/PopupRedux';
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 * as TeamSharingRedux from '../../Redux/ActionsReducers/TeamSharingRedux';
import { UIHelper } from '../UIHelper';
import { PlusMinusWizard } from './Wizard/PlusMinusWizard';
export class PlusMinusSummaryComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = UIHelper.getEmptyConfigState();
    }
    render() {
        let moduleSummaries = [];
        // title row
        let titleRow = (React.createElement(ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: Helper.returnItemCount(this.props.PlusMinusNudges.filter((item) => this.props.api.columnScopeApi.isColumnInScopeColumns(this.props.summarisedColumn, item.Scope)), this.props.moduleInfo.FriendlyName), onNew: () => this.onNew(), newButtonTooltip: 'Plus / Minus Rule', accessLevel: this.props.accessLevel }));
        moduleSummaries.push(titleRow);
        // existing items
        this.props.PlusMinusNudges.map((item, index) => {
            if (this.props.api.columnScopeApi.isColumnInScopeColumns(this.props.summarisedColumn, item.Scope)) {
                let detailRow = (React.createElement(ModuleDetail, { key: 'PM' + index, item1: 'Nudge Value: ' + item.NudgeValue, item2: item.Rule?.BooleanExpression ? item.Rule.BooleanExpression : 'Always', configEnity: item, showShare: this.props.teamSharingActivated, moduleInfo: this.props.moduleInfo, onEdit: () => this.onEdit(item), onShare: (config) => this.props.onShare(item, config), onDelete: PlusMinusRedux.PlusMinusNudgeDelete(item), accessLevel: this.props.accessLevel }));
                moduleSummaries.push(detailRow);
            }
        });
        return (React.createElement("div", null,
            moduleSummaries,
            this.state.editedAdaptableObject && (React.createElement(PlusMinusWizard, { moduleInfo: this.props.moduleInfo, data: this.state.editedAdaptableObject, configEntities: null, wizardStartIndex: this.state.wizardStartIndex, onCloseWizard: () => this.onCloseWizard(), onFinishWizard: () => this.onFinishWizard() }))));
    }
    onNew() {
        let configEntity = ObjectFactory.CreateEmptyPlusMinusNudge();
        configEntity.Scope = {
            ColumnIds: [this.props.summarisedColumn.columnId],
        };
        this.setState({
            editedAdaptableObject: configEntity,
            wizardStartIndex: 1,
            wizardStatus: WizardStatus.New,
        });
    }
    onEdit(PlusMinus) {
        this.setState({
            editedAdaptableObject: Helper.cloneObject(PlusMinus),
            wizardStartIndex: 1,
            wizardStatus: WizardStatus.Edit,
        });
    }
    onCloseWizard() {
        this.setState({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: WizardStatus.None,
        });
    }
    onFinishWizard() {
        let plusMinusNudge = this.state.editedAdaptableObject;
        if (this.state.wizardStatus == WizardStatus.Edit) {
            this.props.onEditPlusMinusNudge(plusMinusNudge);
        }
        else {
            this.props.onAddPlusMinusNudge(plusMinusNudge);
        }
        this.setState({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: WizardStatus.None,
        });
    }
    canFinishWizard() {
        let plusMinus = this.state.editedAdaptableObject;
        if (!plusMinus.NudgeValue) {
            return false;
        }
        return true;
    }
}
function mapStateToProps(state, ownProps) {
    return {
        PlusMinusNudges: state.PlusMinus.PlusMinusNudges,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onAddPlusMinusNudge: (PlusMinusNudge) => dispatch(PlusMinusRedux.PlusMinusNudgeAdd(PlusMinusNudge)),
        onEditPlusMinusNudge: (PlusMinusNudge) => dispatch(PlusMinusRedux.PlusMinusNudgeEdit(PlusMinusNudge)),
        onClearPopupParams: () => dispatch(PopupRedux.PopupClearParam()),
        onShare: (entity, config) => dispatch(TeamSharingRedux.TeamSharingShare(entity, ModuleConstants.PlusMinusModuleId, config)),
    };
}
export let PlusMinusSummary = connect(mapStateToProps, mapDispatchToProps)(PlusMinusSummaryComponent);
