{"version":3,"file":"fileUtils.mjs","names":["XLSX","handleExcelFileUpload","fileObject","fileProperties","Promise","resolve","reject","reader","FileReader","onload","e","data","Uint8Array","target","result","workbook","read","type","sheetName","SheetNames","worksheetNumber","value","worksheet","Sheets","json","utils","sheet_to_json","header","length","headers","firstDataLine","sampleData","onerror","readAsArrayBuffer","handleTextFileUpload","chunkSize","offset","accumulatedData","lines","split","replace","delimiter","size","readNextChunk","blob","slice","readAsText","handleJsonFileUpload","jsonData","JSON","parse","dataAtPath","jsonPath","getValueByPath","Array","isArray","keys","Object","err","obj","path","reduce","acc","key"],"sources":["../../../src/udp/fileImportMapping/fileUtils.js"],"sourcesContent":["import * as XLSX from 'xlsx';\n\nexport const handleExcelFileUpload = (fileObject, fileProperties) => {\n  return new Promise((resolve, reject) => {\n    if (fileObject) {\n      const reader = new FileReader();\n\n      reader.onload = function (e) {\n        const data = new Uint8Array(e.target.result);\n        const workbook = XLSX.read(data, { type: 'array' });\n\n        const sheetName = workbook.SheetNames[fileProperties?.worksheetNumber?.value - 1];\n        const worksheet = workbook.Sheets[sheetName];\n\n        // Get the headers (first row) and first data line\n        const json = XLSX.utils.sheet_to_json(worksheet, {\n          header: 1,\n        });\n        if (json.length > 1) {\n          const headers = json[0]; // First row as headers\n          const firstDataLine = json[1]; // Second row as the first data line\n          resolve({ headers: headers, sampleData: firstDataLine });\n        }\n        else{\n            reject(\"Could not get file headers\")\n        }\n      };\n      reader.onerror = function () {\n        reject('Error reading the file.');\n      };\n\n      reader.readAsArrayBuffer(fileObject); // Read the file as an ArrayBuffer\n    } else {\n      reject('No file provided');\n    }\n  });\n};\n\nexport const handleTextFileUpload = (fileObject, fileProperties) => {\n  return new Promise((resolve, reject) => {\n    if (fileObject) {\n      const chunkSize = 1024; // Read 1024 bytes at a time\n      let offset = 0; // Start reading at the beginning of the file\n      let accumulatedData = '';\n\n      const reader = new FileReader();\n\n      reader.onload = function (e) {\n        accumulatedData += e.target.result;\n\n        const lines = accumulatedData.split('\\n');\n        if (lines.length > 2) {\n          const headers = lines[0].replace(/\\r/g, '').split(fileProperties?.delimiter?.value);\n          const firstDataLine = lines[1].replace(/\\r/g, '').split(fileProperties?.delimiter?.value);\n          resolve({ headers: headers, sampleData: firstDataLine });\n        } else {\n          // If the header is not found and there's more data to read\n          if (offset < fileObject.size) {\n            readNextChunk();\n          }\n        }\n      };\n      reader.onerror = function () {\n        reject('Error reading the file.');\n      };\n\n      function readNextChunk() {\n        // Read the next 1024 bytes\n        const blob = fileObject.slice(offset, offset + chunkSize);\n        reader.readAsText(blob);\n        offset += chunkSize;\n      }\n      readNextChunk();\n    } else {\n      reject('No file provided');\n    }\n  });\n};\n\nexport const handleJsonFileUpload = (fileObject, fileProperties) => {\n  return new Promise((resolve, reject) => {\n    if (fileObject) {\n      const reader = new FileReader();\n\n      reader.onload = function (e) {\n        try {\n          const jsonData = JSON.parse(e.target.result);\n          const dataAtPath = fileProperties?.jsonPath ? getValueByPath(jsonData, fileProperties?.jsonPath) : jsonData;\n\n          if (Array.isArray(dataAtPath) && dataAtPath.length > 0) {\n            const keys = Object.keys(dataAtPath[0]);\n            resolve({ headers: keys, sampleData: dataAtPath[0] });\n          } else {\n            reject('The JSON file does not contain an array or is empty.');\n          }\n        } catch (err) {\n          reject('Error parsing JSON file:', err);\n        }\n      };\n      reader.onerror = function () {\n        reject('Error reading the file.');\n      };\n\n      reader.readAsText(fileObject);\n    } else {\n      reject('No file provided');\n    }\n  });\n};\n\nconst getValueByPath = (obj, path) => {\n    return path.split('.').reduce((acc, key) => acc && acc[key], obj);\n  }\n"],"mappings":";;AAEA,MAAaC,yBAAyBC,YAAYC,mBAAmB;AACnE,QAAO,IAAIC,SAASC,SAASC,WAAW;AACtC,MAAIJ,YAAY;GACd,MAAMK,SAAS,IAAIC,YAAY;AAE/BD,UAAOE,SAAS,SAAUC,GAAG;IAC3B,MAAMC,OAAO,IAAIC,WAAWF,EAAEG,OAAOC,OAAO;IAC5C,MAAMC,WAAWf,KAAKgB,KA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