/**
 *
 * @param {Array} questionnaireItems
 * @returns {void}
 */
export default function (questionnaireItems) {
  questionnaireItems.forEach((questionnaireItem) => {
    let product = 1;
    let count = 0;
    let min = Number.POSITIVE_INFINITY;
    let max = Number.NEGATIVE_INFINITY;
    for (const quotation of questionnaireItem.quotations) {
      for (const quote of quotation.cleanedQuotes) {
        const price = parseFloat(quote.price);
        quote.price = price;
        if (price > 0) {
          product *= price;
          count++;
          min = Math.min(min, price);
          max = Math.max(max, price);
        }
      }
    }
    const geomean = count > 0 ? Math.pow(product, 1 / count) : 0;

    const stdev = Math.sqrt(
      questionnaireItem.quotations.reduce((sum, quotation) => {
        for (const quote of quotation.cleanedQuotes) {
          const price = parseFloat(quote.price);
          if (price > 0) sum += Math.pow(price - geomean, 2);
        }
        return sum;
      }, 0) / count
    );
    const variation = stdev / geomean;

    if (geomean > 0) {
      questionnaireItem.geomean = geomean.toFixed(2);
      questionnaireItem.stdev = stdev.toFixed(2);
      questionnaireItem.variation = variation.toFixed(2);
      questionnaireItem.min = min.toFixed(2);
      questionnaireItem.max = max.toFixed(2);
      questionnaireItem.product = product.toFixed(2);
      questionnaireItem.count = count.toFixed(2);
    }
  });
}

export function recalculateRow(rowData) {
  // console.log("Row date is: ", rowData);
  let product = 1;
  let count = 0;
  let min = Number.POSITIVE_INFINITY;
  let max = Number.NEGATIVE_INFINITY;
  for (const quotation of Object.values(rowData.quotes)) {
    for (const key of Object.keys(quotation)) {
      const price = parseFloat(quotation[key]);
      if (isOnlyQuote(key) && price > 0) {
        product *= price;
        count++;
        min = Math.min(min, parseFloat(price));
        max = Math.max(max, price);
      }
    }
  }
  const geomean = count > 0 ? Math.pow(product, 1 / count) : 0;

  const stdev = Math.sqrt(
    Object.values(rowData.quotes).reduce((sum, quotation) => {
      for (const key of Object.keys(quotation)) {
        const price = parseFloat(quotation[key]);
        if (isOnlyQuote(key) && price > 0) sum += Math.pow(price - geomean, 2);
      }
      return sum;
    }, 0) /
      (count - 1)
  );
  const variation = stdev / geomean;

  if (geomean > 0) {
    rowData.geomean = isNaN(geomean) ? "0" : geomean.toFixed(2);
    rowData.deviation = isNaN(stdev) ? "0" : stdev.toFixed(2);
    rowData.variation = isNaN(variation) ? "0" : variation.toFixed(2);
    rowData.minQ = isNaN(min) ? "0" : min.toFixed(2);
    rowData.maxQ = isNaN(max) ? "0" : max.toFixed(2);
    rowData.product = isNaN(product) ? "0" : product.toFixed(2);
    rowData.count = isNaN(count) ? "0" : count.toFixed(2);
    rowData.ratio = isNaN(max / min) ? "0" : (max / min).toFixed(2);
  }
  // console.log("Recalculated data: ", geomean, stdev, variation, rowData);
}

const isOnlyQuote = (str) => {
  return /^((?!_)[A-Za-z0-9])+$/.test(str);
};
Based on the above function which calculates variation, geomean and standard deviation on quotaions of questionnaireItems fetched from the backend. The following react codes are not displaying the correct calculations and editing one of the values  is giving the wrong calculation const mutateRows = (newItem, p) => {
    let pos;
    const data = p.data;
    const quote = p.colDef.field;
    const marketplaceCode = p.column.originalParent.colGroupDef.field;
    const newValue = parseFloat(p.newValue);

    rowImmutableStore = rowImmutableStore.map((oldItem, index) => {
      if (oldItem.id === newItem.id) {
        pos = index;
        return newItem;
      }
      return oldItem;
    });
    // console.log('onCellEditRequest, updating ' + quote + ' to ' + newValue, pos);

    // console.log("rowImmutableStore: ", pos, rowImmutableStore);
    if (pos !== "undefined") recalculateRow(rowImmutableStore[pos]);
    setRowData(rowImmutableStore);
    // console.log("CHanges:  ", event);
    setChanges((state) => [
      ...state,
      {
        quote,
        marketplace: marketplaceCode.toString(),
        oldValue:
          typeof p.oldValue === "number" ? parseFloat(p.oldValue) : undefined,
        newValue,
        itemCode: data.code.toString(),
        row: p.rowIndex,
        col: p.column.getInstanceId(),
        geomean: data.geomean,
        variation: data.variation,
        deviation: data.deviation,
        count: data.count,
        maxQ: data.maxQ,
        minQ: data.minQ,
        ratio: data.ratio,
      },
    ]);
  }; const onCellEditRequest = useCallback(
    (event) => {
      console.log("edit event: ", event);

      //
      const marketplaceCode = event.column.originalParent.colGroupDef.field;
      const quote = event.colDef.field;
      const data = event.data;
      const newValue = event?.newValue?.trim();
      const newItem = { ...data };

      // console.log('type: ', typeof(newValue));
      // console.log(`editing: ${newItem['quotes'][marketplaceCode][quote]}   to   ${newValue}`);

      // if(newItem["quotes"][marketplaceCode][`${quote}_Id`] === undefined){
      //   showErrorModal('Alert', 'You cannot add a new price.');
      //   return;
      // }
      //
      if (newValue !== "" && isNaN(Number(newValue))) {
        showErrorModal("Alert", "Please enter a valid number.");
        return;
      }

      if (newValue !== "" && !isNaN(Number(newValue))) {
        //!changeU
        // newItem["quotes"][marketplaceCode][quote] = parseFloat(newValue);

        console.log("newValue: ", newValue);
        console.log("marketplaceCode: ", marketplaceCode);
        console.log(
          'newItem["quotes"][marketplaceCode]: ',
          newItem["quotes"][marketplaceCode]
        );
        if (newItem["quotes"][marketplaceCode]) {
          newItem["quotes"][marketplaceCode][quote] = parseFloat(newValue);
        } else {
          newItem["quotes"][marketplaceCode] = {};
          newItem["quotes"][marketplaceCode][quote] = parseFloat(newValue);
        }

        //!
      }

      if (newValue === "") {
        newItem["quotes"][marketplaceCode][quote] = null;
      }

      mutateRows(newItem, event);
      console.log(`newItem: ${newItem["quotes"][marketplaceCode][quote]}`);
    },
    [rowImmutableStore]
  );