"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseCSV = parseCSV;
exports.loadDataFromFile = loadDataFromFile;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/**
 * Parse CSV content into TestDataSet array
 */
function parseCSV(content) {
    const lines = content.trim().split('\n');
    if (lines.length < 2)
        return []; // Need header + at least one data row
    const headers = lines[0].split(',').map(h => h.trim());
    const dataSets = [];
    for (let i = 1; i < lines.length; i++) {
        const line = lines[i].trim();
        if (!line)
            continue;
        // Simple CSV parsing (handles basic quoted values)
        const values = [];
        let current = '';
        let inQuotes = false;
        for (const char of line) {
            if (char === '"') {
                inQuotes = !inQuotes;
            }
            else if (char === ',' && !inQuotes) {
                values.push(current.trim());
                current = '';
            }
            else {
                current += char;
            }
        }
        values.push(current.trim());
        const row = {};
        headers.forEach((header, idx) => {
            const val = values[idx] ?? '';
            // Try to parse as number or boolean
            if (val === 'true') {
                row[header] = true;
            }
            else if (val === 'false') {
                row[header] = false;
            }
            else if (val !== '' && !isNaN(Number(val))) {
                row[header] = Number(val);
            }
            else {
                row[header] = val;
            }
        });
        dataSets.push({ name: `Row ${i}`, values: row });
    }
    return dataSets;
}
/**
 * Load data from a file (CSV or JSON)
 */
function loadDataFromFile(filePath, baseDir) {
    const resolvedPath = baseDir && !path.isAbsolute(filePath)
        ? path.join(baseDir, filePath)
        : filePath;
    if (!fs.existsSync(resolvedPath)) {
        throw new Error(`Data file not found: ${resolvedPath}`);
    }
    const content = fs.readFileSync(resolvedPath, 'utf-8');
    const ext = path.extname(resolvedPath).toLowerCase();
    if (ext === '.csv') {
        return parseCSV(content);
    }
    else if (ext === '.json') {
        const jsonData = JSON.parse(content);
        if (Array.isArray(jsonData)) {
            return jsonData.map((item, idx) => ({
                name: item.name || `Item ${idx + 1}`,
                values: item.values || item,
            }));
        }
        throw new Error('JSON data file must contain an array');
    }
    throw new Error(`Unsupported data file format: ${ext}`);
}
