import * as constants from '../../constants';

import { updateKdModel, updateKdItem, resetKdModel, removeItemKdModel } from './kdModelHelper';

export const behaviorHandler = {
  isChickletNone: props => () => (
    constants.CHICKLET_NONE === props.selectBehavior
  ),
  isAddChickletSingle: props => () => (
    constants.ADD_CHICKLET_SINGLE === props.selectBehavior
  ),
  isAddChickletSingleRegardlessOfType: props => () => (
    constants.ADD_CHICKLET_SINGLE_REGARDLESS_OF_TYPE === props.selectBehavior
  ),
  isAddChicklet: props => () => (
    constants.ADD_CHICKLET === props.selectBehavior
  ),
};

export const errorHandler = {
  setErrors: ({ setWrapperState, events }) =>
    error =>
      setWrapperState(
        state => ({ ...state, errorState: error }),
        () => events && events.onError && events.onError({ error }),
      ),
  clearErrors: ({ setWrapperState, events }) =>
    () => {
      setWrapperState(
        state => ({ ...state, errorState: null }),
        () => events && events.onError && events.onError({ error: null }),
      );
    },
};

export const optionHandler = {
  setOption:
  ({ setWrapperState, events }) =>
    option =>
      setWrapperState(
        state =>
          ({
            ...state,
            optionState: option,
          }),
        () => events && events.onOptionChange && events.onOptionChange({ option }),
      ),
};

export const addItemHandler = {
  addItem: ({ setWrapperState, events }) =>
  (newItem, callback) =>
    setWrapperState(
      state => ({
        ...state,
        itemsState: updateKdModel({ items: [...state.itemsState, newItem], events }),
      }),
      () => callback && callback(),
    ),
  addItemAtBeginning: ({ setWrapperState, events }) =>
    (newItem, callback) => setWrapperState(
      state => ({
        ...state,
        itemsState: updateKdModel({
          items: [newItem, ...state],
          events,
        }),
      }),
      () => callback && callback(),
    ),
  addItemSingle: ({ setWrapperState }) =>
    (newItem, callback) =>
      setWrapperState(
        state => ({
          ...state,
          itemsState: [updateKdItem(
            newItem,
            true,
          )],
        }),
        () => callback && callback(),
      ),
  addItemAtPosition: ({ setWrapperState, events }) =>
    ({ index, newItem }, callback) =>
    setWrapperState(
      (state) => {
        state.itemsState.splice(index, 1, newItem);
        return ({
          ...state,
          itemsState: updateKdModel({ items: [...state.itemsState], events }),
        });
      },
      () => callback && callback(),
    ),
  deleteItem: ({ setWrapperState, events }) =>
    ({ items, removedItem }, callback) =>
      setWrapperState(
        state => ({
          ...state,
          itemsState: removeItemKdModel({
            items,
            removedItem,
            events,
          }),
        }),
        () => callback && callback(),
      ),
  resetItems: ({ setWrapperState, events }) =>
    callback =>
      setWrapperState(
        state => ({
          ...state,
          itemsState: resetKdModel({ items: state.itemsState, events }),
        }),
        () => callback && callback(),
      ),
  setItems: ({ setWrapperState, events }) =>
    (items, callback) => setWrapperState(
      state => ({
        ...state,
        itemsState: updateKdModel({ items, events }),
      }),
      () => callback && callback(),
    ),

};

