const UPDATE_RANGE = 'UPDATE_RANGE';
export const updateRange = (value) => ({
  type: UPDATE_RANGE,
  value,
});

const UPDATE_RADIO = 'UPDATE_RADIO';
export const updateRadio = () => ({
  type: UPDATE_RADIO,
});

const UPDATE_RADIO_GROUP = 'UPDATE_RADIO_GROUP';
export const updateRadioGroup = name => ({
  type: UPDATE_RADIO_GROUP,
  name,
});

const INITIAL_STATE = {
  formRange: {
    value: 10,
  },
  formRadio: {
    checked: undefined,
  },
  formRadioGroup: {
    male: undefined,
    female: undefined,
  },
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case UPDATE_RANGE: {
      return {
        ...state,
        formRange: {
          value: action.value,
        },
      }
    }

    case UPDATE_RADIO: {
      return {
        ...state,
        formRadio: {
          checked: !state.formRadio.checked,
        },
      }
    }

    case UPDATE_RADIO_GROUP: {
      return {
        ...state,
        formRadioGroup: {
          male: false,
          female: false,
          [action.name]: !state.formRadioGroup[action.name],
        },
      }
    }

    default:
      return state;
  }
};
