"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.systemFileHandlers = exports.parseCSV = void 0;
const FileDroppable_1 = require("../../components/FileDroppable");
const jsonHandler = {
    fileExtension: '.json',
    handleFile: (file) => (0, FileDroppable_1.readJSONFile)(file),
};
const parseCSV = (content) => {
    const lines = content.split('\n');
    const headers = lines[0].split(',');
    const data = [];
    if (lines.length <= 1) {
        return null;
    }
    for (let i = 1; i < lines.length; i++) {
        const values = lines[i].split(',');
        if (values.length === headers.length) {
            const item = {};
            for (let j = 0; j < headers.length; j++) {
                const value = values[j];
                item[headers[j]] = value === '' || isNaN(Number(value)) ? value : Number(value); // '0012345'
            }
            data.push(item);
        }
    }
    return data;
};
exports.parseCSV = parseCSV;
const csvFileHandler = {
    fileExtension: '.csv',
    handleFile: (file) => {
        return new Promise((resolve, reject) => {
            const reader = new FileReader();
            reader.onload = (event) => {
                const content = event.target?.result;
                resolve((0, exports.parseCSV)(content));
            };
            reader.readAsText(file);
        });
    },
};
exports.systemFileHandlers = [jsonHandler, csvFileHandler];
