import { connect } from 'react-redux';

import { requestUpdateFeatureToggleVariants } from '../../../store/feature-actions';
import UpdateFeatureToggleComponent from './update-variant-component';
import { updateWeight } from '../../common/util';

const mapStateToProps = (state, ownProps) => ({
    variants: ownProps.featureToggle.variants || [],
    hasPermission: ownProps.hasPermission,
});

const mapDispatchToProps = (dispatch, ownProps) => ({
    addVariant: variant => {
        const { featureToggle } = ownProps;
        const currentVariants = featureToggle.variants || [];
        const variants = [...currentVariants, variant];
        updateWeight(variants, 1000);
        return requestUpdateFeatureToggleVariants(featureToggle, variants)(dispatch);
    },
    removeVariant: index => {
        const { featureToggle } = ownProps;
        const currentVariants = featureToggle.variants || [];
        const variants = currentVariants.filter((v, i) => i !== index);
        if (variants.length > 0) {
            updateWeight(variants, 1000);
        }
        requestUpdateFeatureToggleVariants(featureToggle, variants)(dispatch);
    },
    updateVariant: (index, variant) => {
        const { featureToggle } = ownProps;
        const currentVariants = featureToggle.variants || [];
        const variants = currentVariants.map((v, i) => (i === index ? variant : v));
        updateWeight(variants, 1000);
        requestUpdateFeatureToggleVariants(featureToggle, variants)(dispatch);
    },
});

export default connect(mapStateToProps, mapDispatchToProps)(UpdateFeatureToggleComponent);
