import * as constants from '../../constants';
import { updateKdModel, updateKdItems, 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 }),
      ),
};

const limitHandler = ({ valuesLimit, items }) => {
  return valuesLimit ? items.slice(-valuesLimit) : items;
};

export const addItemHandler = {
  addNewItem: ({ setWrapperState, valuesLimit, events, chicklets }) =>
    ({ newItem }, callback) =>
    setWrapperState(
      (state) => {
        const index = state.itemsState.findIndex(item => item.key === newItem.option);
        if (index > -1) {
          state.itemsState.splice(index, 1, newItem);
          return ({
            ...state,
            itemsState: updateKdModel({
              items: limitHandler({
                items: [...state.itemsState],
                valuesLimit,
              }),
              events,
            }),
          });
        } else {
          const items = [...state.itemsState, newItem]; // add at the end
          return ({
            ...state,
            itemsState: updateKdModel({
              items: limitHandler({ items, valuesLimit }),
              events,
            }),
          });
        }
      },
      () => callback && callback(),
    ),
  addItemSingle: ({ setWrapperState }) =>
    (newItem, callback) =>
      setWrapperState(
        state => ({
          ...state,
          itemsState: [updateKdItem(
            newItem,
            true,
          )],
        }),
        () => 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, valuesLimit }) =>
    ({ items, model, callback }) => setWrapperState(
      state => ({
        ...state,
        itemsState: updateKdItems(
          limitHandler({ items, valuesLimit }),
          model,
        ),
      }),
      () => callback && callback(),
    ),
};

