const parseExcel = () => {
    console.log("Marketplace codes: ", marketplaceCodes);
    console.log("itemCodes", itemCodes);
    const parsedInfo = {
      parsed: 0,
      total: 0,
      unfoundMarketplaces: [],
      unfoundItems: [],
    };
    console.log("--------------------------------------------");
    console.log("sheets: ", sheets);
    console.log("sheetNames: ", sheets[sheetNames[0]]);
    console.log("marketplaceRow: ", sheets[sheetNames[0]][MARKETPLACE_ROW]);
    const marketplaceRow = Object.keys(sheets[sheetNames[0]][MARKETPLACE_ROW]);
    const columnMpCodes = {};
    /** @type {{itemCode: string; variation: string; stdev: string; geomean: string; min: string; max: string; quantity: string;}} */
    const valueColumns = {
      itemCode: marketplaceRow[ITEM_CODE_COLUMN],
      variation: marketplaceRow[VARIATION_COLUMN],
      stdev: marketplaceRow[STDEV_COLUMN],
      geomean: marketplaceRow[GEOMEAN_COLUMN],
      min: marketplaceRow[MIN_COLUMN],
      max: marketplaceRow[MAX_COLUMN],
      quantity: marketplaceRow[MEASUREMENT_UNIT_COLUMN],
    };
    let lastMpCode;
    let used = 0;
    // TODO: limit until QUOTATION_END_COLUMN
    for (let i = QUOTATION_START_COLUMN; i < marketplaceRow.length; i++) {
      const cur = marketplaceRow[i];
      let mpCode = String(sheets[sheetNames[0]][MARKETPLACE_ROW][cur]).trim();
      if (mpCode[0] === "0" && mpCode.length !== 8) {
        mpCode = `${mpCode.slice(0, mpCode.length - 1)}0${
          mpCode[mpCode.length - 1]
        }`;
      }
      const num = parseInt(mpCode);
      if (
        // isNaN(num) &&
        // mpCode.length < 6 &&
        used < QUOTE_PER_MP - 1 &&
        lastMpCode
      ) {
        parsedInfo.total++;
        columnMpCodes[cur] = lastMpCode;
        used++;
      } else if (mpCode.length > 6) {
        if (!marketplaceCodes[num]) {
          errors.push(
            `Error at row ${
              i + 2
            }! Marketplace with the code ${num} doesn't exist on the database`
          );
          parsedInfo.unfoundMarketplaces.push(num);
        }
        parsedInfo.total++;
        columnMpCodes[cur] = num;
        lastMpCode = num;
        used = 0;
      } else {
        used = 0;
        lastMpCode = null;
        console.error(`Invalid marketplace code: "${mpCode}"`);
      }
    }
    console.log("Marketplace codes: ", columnMpCodes);
    console.log("Value columns: ", valueColumns);
    /** @type {{ questionnaireId: number; quotations: {[marketplaceCode_itemCode: string]: CreateQuotation}; geomeans: CreateGeomean[]}} */
    const preparedData = { questionnaireId, quotations: {}, geomeans: [] };
    // For each row
    sheetNames.forEach((sheetName) => {
      const items = sheets[sheetName];
      for (let i = QUOTATION_START_ROW; i < items.length; i++) {
        const row = items[i];
        // if (typeof row[valueColumns.quantity] !== "string") {
        //   errors.push(
        //     `Error at row ${
        //       i + 5
        //     }! Measurement unit expected to be a string but found ${typeof row[
        //       valueColumns.quantity
        //     ]}`
        //   );
        //   continue;
        // }
        const itemCode = parseInt(row[valueColumns.itemCode]);
        // const quantity = parseInt(row[valueColumns.quantity]);
        // const measurementCode = (
        //   isNaN(quantity)
        //     ? row[valueColumns.quantity]
        //     : row[valueColumns.quantity].split(quantity)[1]
        // ).trim();
        if (isNaN(itemCode) || itemCode < 100_000_000) {
          errors.push(
            `Error at row ${i + 2}! Invalid item code found: "${
              row[valueColumns.itemCode]
            }".`
          );
          continue;
        }
        // if (!measurementCode || measurementCode === "") {
        //   errors.push(
        //     `Error at row ${i + 2}! Invalid Measurement unit found: ${
        //       row[valueColumns.quantity]
        //     }`
        //   );
        //   continue;
        // }
        if (!itemCodes[itemCode]) {
          errors.push(
            `Error at row ${
              1 + 5
            }! Item with the code ${itemCode} doesn't exist on the database`
          );
          parsedInfo.unfoundItems.push(itemCode);
          continue;
        }
        // const measurmentIds = measurements.data.filter((measurement) => {
        //   return (
        //     measurementCode.toLowerCase() == measurement.UoMName.toLowerCase() ||
        //     measurementCode.toLowerCase() == measurement.SmU.toLowerCase()
        //   );
        // });
        // if (!measurmentIds || measurmentIds.length < 1) {
        //   errors.push(
        //     `Error at row ${rowIndex + 5} column ${
        //       Object.keys(row).indexOf(`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`) + 1
        //     }! Measurement unit not found on the database ${
        //       row[`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`]
        //     }.\nPlease create the standard unit of measure first`
        //   );
        //   return;
        // }
        const variation = "-" ? null : parseFloat(row[valueColumns.variation]);
        console.log("variation", variation);
        const stdev = "-" ? null : parseFloat(row[valueColumns.stdev]);
        const geomean = "-" ? null : parseFloat(row[valueColumns.geomean]);
        const min = "-" ? null : parseFloat(row[valueColumns.min]);
        const max = "-" ? null : parseFloat(row[valueColumns.max]);
        if (
          isNaN(variation) ||
          isNaN(stdev) ||
          isNaN(geomean) ||
          isNaN(min) ||
          isNaN(max)
        ) {
          errors.push(
            `Error at row ${
              i + 2
            }! Invalid geomean data found: ${JSON.stringify({
              variation,
              stdev,
              geomean,
              min,
              max,
            })}`
          );
          continue;
        }
        preparedData.geomeans.push({
          itemCode,
          variation,
          stdev,
          geomean,
          min,
          max,
          questionnaireId,
        });
        for (const columnName in columnMpCodes) {
          const price = parseFloat(row[columnName]);
          const key = `${columnMpCodes[columnName]}_${itemCode}`;
          if (!isNaN(price)) {
            parsedInfo.parsed++;
            if (!preparedData.quotations[key]) {
              console.log(
                `Create------------------ "${key}": ${price}`,
                preparedData[key]
              );
              preparedData.quotations[key] = {
                itemCode,
                questionnaireId,
                marketplaceCode: columnMpCodes[columnName],
                quotes: [
                  {
                    price,
                    quantity: itemCodes[itemCode].measurementQuantity,
                    measurement: itemCodes[itemCode].measurement,
                    shopContactName: "imported",
                    shopContactPhone: "imported",
                  },
                ],
              };
            } else {
              console.log("Exists ------------------", key, price);
              preparedData.quotations[key].quotes.push({
                price,
                quantity: itemCodes[itemCode].measurementQuantity,
                measurement: itemCodes[itemCode].measurement,
                shopContactName: "imported",
                shopContactPhone: "imported",
              });
            }
          } else {
            console.error(
              `Invalid price: "${price}", row: ${i + 2}, key: ${key} `
            );
          }
        }
      }
    });
    parsedInfo.total *=
      (sheets[sheetNames[0]].length - QUOTATION_START_ROW) * sheetNames.length;
    console.log("Prepared data: ", preparedData);
    // items.slice(QUOTATION_START_ROW).forEach((row, rowIndex) => {
    //   if (typeof row[`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`] !== "string") {
    //     errors.push(
    //       `Error at row ${rowIndex + 5} column ${
    //         Object.keys(row).indexOf(`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`) + 1
    //       }! Measurement unit expected to be a string but found ${typeof row[
    //         `__EMPTY_${MEASUREMENT_UNIT_COLUMN}`
    //       ]}`
    //     );
    //     return;
    //   }
    //   const itemCode = parseInt(row.__EMPTY);
    //   const quantity = parseInt(row[`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`]);
    //   const measurementCode = (
    //     isNaN(quantity)
    //       ? row[`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`]
    //       : row[`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`].split(quantity)[1]
    //   ).replace(" ", "");
    //   if (isNaN(itemCode) || itemCode < 100_000_000) {
    //     errors.push(
    //       `Error at row ${rowIndex + 5} colum ${
    //         Object.keys(row).indexOf("__EMPTY") + 1
    //       }! Item code not found`
    //     );
    //     return;
    //   }
    //   if (!measurementCode || measurementCode == "") {
    //     errors.push(
    //       `Error at row ${rowIndex + 5} column ${
    //         Object.keys(row).indexOf(`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`) + 1
    //       }! Measurement unit not recognized ${
    //         row[`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`]
    //       }`
    //     );
    //     return;
    //   }
    //   if (itemsData.data.filter((el) => el.code == itemCode).length == 0) {
    //     errors.push(
    //       `Error at row ${rowIndex + 5} column ${
    //         Object.keys(row).indexOf(`__EMPTY`) + 1
    //       }! Item with the code ${itemCode} doesn't exist on the database`
    //     );
    //     return;
    //   }
    //   const measurmentIds = measurements.data.filter((measurement) => {
    //     return (
    //       measurementCode.toLowerCase() == measurement.UoMName.toLowerCase() ||
    //       measurementCode.toLowerCase() == measurement.SmU.toLowerCase()
    //     );
    //   });
    //   if (!measurmentIds || measurmentIds.length < 1) {
    //     errors.push(
    //       `Error at row ${rowIndex + 5} column ${
    //         Object.keys(row).indexOf(`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`) + 1
    //       }! Measurement unit not found on the database ${
    //         row[`__EMPTY_${MEASUREMENT_UNIT_COLUMN}`]
    //       }.\nPlease create the standard unit of measure first`
    //     );
    //     return;
    //   }
    //   const quotesRow = Object.keys(row).slice(QUOTATION_START_COLUMN);
    //   quotesRow.forEach((quoteKey, index) => {
    //     if (
    //       marketplacesData.data.filter(
    //         (el) => el.code == parseInt(columnMpCodes[quoteKey])
    //       ).length == 0
    //     ) {
    //       errors.push(
    //         `Error at row ${rowIndex + 5} column ${
    //           Object.keys(row).indexOf(quoteKey) + 1
    //         }! Marketplace with the code ${
    //           columnMpCodes[quoteKey]
    //         } doesn't exist on the database`
    //       );
    //       return;
    //     }
    //     if (index % 2 === 0) {
    //       const price1 = parseFloat(row[quoteKey]);
    //       const price2 = parseFloat(row[quotesRow[index + 1]]);
    //       const measurmentId = measurmentIds[0].id;
    //       const quotes = [];
    //       if (!isNaN(price1))
    //         quotes.push({
    //           price: price1,
    //           quantity: isNaN(quantity) ? 1 : quantity,
    //           measurmentId,
    //           shopContactName: "Imported",
    //           shopContactPhone: "Imported",
    //         });
    //       if (!isNaN(price2))
    //         quotes.push({
    //           price: price2,
    //           quantity: isNaN(quantity) ? 1 : quantity,
    //           measurmentId,
    //           shopContactName: "Imported",
    //           shopContactPhone: "Imported",
    //         });
    //       if (quotes.length > 0)
    //         quotatios.push({
    //           itemCode,
    //           questionnaireId,
    //           marketplaceCode: columnMpCodes[quoteKey],
    //           quotes,
    //         });
    //     }
    //   });
    // });
    console.log(`
     ---------------------------------------------
    Parsed ${parsedInfo.parsed} quotations, Got ${errors.length} errors,\nexpected ${parsedInfo.total} quotations
    ----------------------------------------------`);
    console.log(errors);
    showErrorModal("Error", errors.join("\n"));
    setParseLog(parsedInfo);
    exportFiles(
      errors,
      Object.values({
        quotations: Object.values(preparedData.quotations),
        geomeans: preparedData.geomeans,
      })
    );
    setErrors(errors);
  };