
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="ebf6509d-f540-52ad-bd5b-b615c94d9916")}catch(e){}}();
import toJsonSchema from 'to-json-schema';
/**
 * Function to recursively remove empty arrays from an object
 */
export function removeEmptyArrays(obj) {
    if (Array.isArray(obj)) {
        // If the item is an array, recursively call removeEmptyArrays on each element.
        return obj.map((item) => removeEmptyArrays(item));
    }
    if (typeof obj !== 'object' || obj === null) {
        // Return primitives and null values as is.
        return obj;
    }
    // Use reduce to build a new object, excluding keys with empty arrays.
    return Object.entries(obj).reduce((acc, [key, value]) => {
        const processedValue = removeEmptyArrays(value);
        // Exclude the key if the processed value is an empty array.
        if (Array.isArray(processedValue) && processedValue.length === 0) {
            return acc;
        }
        acc[key] = processedValue;
        return acc;
    }, {});
}
// TODO: write unit tests for this.
/**
 * Generates a JSON schema from dataset items with configurable options
 *
 * @param datasetItems - Array of dataset items to generate schema from
 * @param options - Configuration options for schema generation
 * @returns JSON schema object or null if generation fails
 */
export function generateSchemaFromItems(datasetItems, options = {}) {
    const { limit = 5, clean = true, arrayMode = 'all', } = options;
    // Limit the number of items used for schema generation
    const itemsToUse = datasetItems.slice(0, limit);
    if (itemsToUse.length === 0) {
        return null;
    }
    // Clean the dataset items by removing empty arrays if requested
    const processedItems = clean
        ? itemsToUse.map((item) => removeEmptyArrays(item))
        : itemsToUse;
    // Try to generate schema with full options first
    try {
        return toJsonSchema(processedItems, {
            arrays: { mode: arrayMode },
        });
    }
    catch { /* ignore */ }
    try {
        return toJsonSchema(processedItems, {
            arrays: { mode: 'first' },
        });
    }
    catch { /* ignore */ }
    // If all attempts fail, return null
    return null;
}
//# sourceMappingURL=schema_generation.js.map
//# debugId=ebf6509d-f540-52ad-bd5b-b615c94d9916
