import * as XLSX from 'xlsx';
export const handleExcelFileUpload = (fileObject, fileProperties) => {
    return new Promise((resolve, reject) => {
        if (fileObject) {
            const reader = new FileReader();
            reader.onload = function (e) {
                var _a;
                const data = new Uint8Array(e.target.result);
                const workbook = XLSX.read(data, { type: 'array' });
                const sheetName = workbook.SheetNames[((_a = fileProperties === null || fileProperties === void 0 ? void 0 : fileProperties.worksheetNumber) === null || _a === void 0 ? void 0 : _a.value) - 1];
                const worksheet = workbook.Sheets[sheetName];
                // Get the headers (first row) and first data line
                const json = XLSX.utils.sheet_to_json(worksheet, {
                    header: 1,
                });
                if (json.length > 1) {
                    const headers = json[0]; // First row as headers
                    const firstDataLine = json[1]; // Second row as the first data line
                    resolve({ headers: headers, sampleData: firstDataLine });
                }
                else {
                    reject("Could not get file headers");
                }
            };
            reader.onerror = function () {
                reject('Error reading the file.');
            };
            reader.readAsArrayBuffer(fileObject); // Read the file as an ArrayBuffer
        }
        else {
            reject('No file provided');
        }
    });
};
export const handleTextFileUpload = (fileObject, fileProperties) => {
    return new Promise((resolve, reject) => {
        if (fileObject) {
            const chunkSize = 1024; // Read 1024 bytes at a time
            let offset = 0; // Start reading at the beginning of the file
            let accumulatedData = '';
            const reader = new FileReader();
            reader.onload = function (e) {
                var _a, _b;
                accumulatedData += e.target.result;
                const lines = accumulatedData.split('\n');
                if (lines.length > 2) {
                    const headers = lines[0].replace(/\r/g, '').split((_a = fileProperties === null || fileProperties === void 0 ? void 0 : fileProperties.delimiter) === null || _a === void 0 ? void 0 : _a.value);
                    const firstDataLine = lines[1].replace(/\r/g, '').split((_b = fileProperties === null || fileProperties === void 0 ? void 0 : fileProperties.delimiter) === null || _b === void 0 ? void 0 : _b.value);
                    resolve({ headers: headers, sampleData: firstDataLine });
                }
                else {
                    // If the header is not found and there's more data to read
                    if (offset < fileObject.size) {
                        readNextChunk();
                    }
                }
            };
            reader.onerror = function () {
                reject('Error reading the file.');
            };
            function readNextChunk() {
                // Read the next 1024 bytes
                const blob = fileObject.slice(offset, offset + chunkSize);
                reader.readAsText(blob);
                offset += chunkSize;
            }
            readNextChunk();
        }
        else {
            reject('No file provided');
        }
    });
};
export const handleJsonFileUpload = (fileObject, fileProperties) => {
    return new Promise((resolve, reject) => {
        if (fileObject) {
            const reader = new FileReader();
            reader.onload = function (e) {
                try {
                    const jsonData = JSON.parse(e.target.result);
                    const dataAtPath = (fileProperties === null || fileProperties === void 0 ? void 0 : fileProperties.jsonPath) ? getValueByPath(jsonData, fileProperties === null || fileProperties === void 0 ? void 0 : fileProperties.jsonPath) : jsonData;
                    if (Array.isArray(dataAtPath) && dataAtPath.length > 0) {
                        const keys = Object.keys(dataAtPath[0]);
                        resolve({ headers: keys, sampleData: dataAtPath[0] });
                    }
                    else {
                        reject('The JSON file does not contain an array or is empty.');
                    }
                }
                catch (err) {
                    reject('Error parsing JSON file:', err);
                }
            };
            reader.onerror = function () {
                reject('Error reading the file.');
            };
            reader.readAsText(fileObject);
        }
        else {
            reject('No file provided');
        }
    });
};
const getValueByPath = (obj, path) => {
    return path.split('.').reduce((acc, key) => acc && acc[key], obj);
};
//# sourceMappingURL=fileUtils.js.map