export const removeItem = props => (option) => {
  const {
     deleteItem,
     wrapperState: { itemsState },
     events: { onChange },
   } = props;
  const items = [...itemsState];
  const index = items.findIndex(item => item.key === option);
  const removedItem = (index > -1) ? items.splice(index, 1)[0] : null;
  if (removedItem) {
    const { kdFilterOptions: { model } } = removedItem;
    const callback =
    () => (
      onChange && onChange({
        data: { ...model.getFilterItemValue(), model },
      })
    );
    deleteItem({ items, removedItem }, callback);
  }
};

export const addChicklet = props => ({
   value,
   chickletLabel,
   option,
   inputVisible,
   remove,
   category,
   direction,
   errors,
   kdFilterModel,
   kdFilterOptions = {
     model: props.kdFilterModel,
     operator: 'Eq',
     itemHandler,
   },
}, callback) => {
  const {
    setErrors,
    clearErrors,
    setOption,
    chicklets,
    removeItem,
    wrapperState: { itemsState },
    addItem,
    addItemSingle,
    addItemAtPosition,
    addItemAtBeginning,
    setItems,
    isChickletNone,
    isAddChicklet,
    isAddChickletSingle,
    isAddChickletSingleRegardlessOfType,
    events,
  } = props;

  if (!value || !chickletLabel || errors) {
    if (errors) {
      setErrors({
        errors,
      });
    }
    return removeItem(option);
  }

  const newItem = {
    value,
    option,
    direction,
    label: chickletLabel,
    key: option,
    category,
    kdFilterOptions,
    chicklets,
    kdFilterModel,
    onRemove: (data) => {
      removeItem(option);
      remove && remove();
    },
  };

  let items = itemsState;

  if (isChickletNone()) {
    newItem.autocompleteVisible = inputVisible;
    newItem.isChickletNone = isChickletNone();
    newItem.hasMainSelector = props.mainSelector.visible;

    addItemSingle(newItem, callback);
  }
  if (isAddChickletSingle()) {
    const itemsMap = itemsState.reduce((map, item) => {
      map[item.kdFilterOptions.model.getEntityType()] = item;
      return map;
    }, {});

    itemsMap[newItem.kdFilterOptions.model.getEntityType()] = newItem;
    items = Object.keys(itemsMap).map(key => itemsMap[key]);
    setItems(items, callback);
  }
  if (isAddChickletSingleRegardlessOfType()) {
    addItemSingle(newItem, callback);
  } else if (isAddChicklet()) {
    const index = itemsState.findIndex(item => item.key === option);
    if (index > -1) {
      addItemAtPosition({
        index,
        newItem,
      }, callback);
    } else {
      chicklets.insertAtBeginning
        ? addItemAtBeginning(newItem, callback)
        : addItem(newItem, callback);
    }
  }

  setOption(option);
  clearErrors();
};


export const setChicklets = props => (chicklets, callback) => {
  const {
    clearErrors,
    addItemSingle,
    removeItem,
    setItems,
    isChickletNone,
    isAddChickletSingle,
    isAddChickletSingleRegardlessOfType,
    events,
  } = props;

  let items = chicklets.map(opt =>
    ({
      ...opt,
      label: opt.chickletLabel,
      key: opt.option,
      onRemove: () => {
        removeItem(opt.option);
        opt.remove && opt.remove();
      },
    }),
  );
  if (items && items.length) {
    const item = [...items].pop();
    if (isChickletNone() || isAddChickletSingleRegardlessOfType()) {
      addItemSingle(item);
    } else {
      if (isAddChickletSingle()) {
        const itemsMap = items.reduce((map, item) => {
          map[item.kdFilterOptions.model.getEntityType()] = item;
          return map;
        }, {});

        // itemsMap[newItem.kdFilterOptions.model.getEntityType()] = newItem;
        items = Object.keys(itemsMap).map(key => itemsMap[key]);
      }
      setItems(items, callback);
    }
    clearErrors();
  }
};

