"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlusMinusSummary = exports.PlusMinusSummaryComponent = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const EditableConfigEntityState_1 = require("../Components/SharedProps/EditableConfigEntityState");
const react_redux_1 = require("react-redux");
const Helper_1 = require("../../Utilities/Helpers/Helper");
const PlusMinusRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/PlusMinusRedux"));
const PopupRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/PopupRedux"));
const ObjectFactory_1 = require("../../Utilities/ObjectFactory");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const ModuleHeader_1 = require("../Components/ModuleSummary/ModuleHeader");
const ModuleDetail_1 = require("../Components/ModuleSummary/ModuleDetail");
const TeamSharingRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/TeamSharingRedux"));
const UIHelper_1 = require("../UIHelper");
const PlusMinusWizard_1 = require("./Wizard/PlusMinusWizard");
class PlusMinusSummaryComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = UIHelper_1.UIHelper.getEmptyConfigState();
    }
    render() {
        let moduleSummaries = [];
        // title row
        let titleRow = (React.createElement(ModuleHeader_1.ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: Helper_1.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_1.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_1.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_1.ObjectFactory.CreateEmptyPlusMinusNudge();
        configEntity.Scope = {
            ColumnIds: [this.props.summarisedColumn.columnId],
        };
        this.setState({
            editedAdaptableObject: configEntity,
            wizardStartIndex: 1,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.New,
        });
    }
    onEdit(PlusMinus) {
        this.setState({
            editedAdaptableObject: Helper_1.Helper.cloneObject(PlusMinus),
            wizardStartIndex: 1,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.Edit,
        });
    }
    onCloseWizard() {
        this.setState({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.None,
        });
    }
    onFinishWizard() {
        let plusMinusNudge = this.state.editedAdaptableObject;
        if (this.state.wizardStatus == EditableConfigEntityState_1.WizardStatus.Edit) {
            this.props.onEditPlusMinusNudge(plusMinusNudge);
        }
        else {
            this.props.onAddPlusMinusNudge(plusMinusNudge);
        }
        this.setState({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.None,
        });
    }
    canFinishWizard() {
        let plusMinus = this.state.editedAdaptableObject;
        if (!plusMinus.NudgeValue) {
            return false;
        }
        return true;
    }
}
exports.PlusMinusSummaryComponent = PlusMinusSummaryComponent;
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)),
    };
}
exports.PlusMinusSummary = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(PlusMinusSummaryComponent);
