import { Item } from '@isa/catalogue/data-access';

/**
 * Helper function to extract the assortment string from an Item object.
 * The assortment is constructed by concatenating the value and the last character of the key
 * for each feature in the item's features array.
 * @param {Item} item - The item object from which to extract the assortment
 * @returns {string} The constructed assortment string
 */
export const getAssortmentFromItem = (item: Item): string => {
  if (!item.features || item.features.length === 0) {
    return '';
  }

  return item.features.reduce((acc, feature) => {
    const value = feature.value ?? '';
    const key = feature.key ?? '';
    const lastChar = key.slice(-1); // gibt '' zurück, wenn key leer ist
    return acc + `${value}|${lastChar}`;
  }, '');
};
