/**
 * Helper functions for working with ResponseFormat objects
 *
 * These utilities handle parsing, stringifying, and default values
 * for ResponseFormat in isolation (without full LimeQuery wrapper)
 */
/**
 * Parse JSON string to ResponseFormat
 *
 * @param json - JSON string to parse
 * @returns Parsed ResponseFormat object
 * @throws Error if JSON is invalid or not a ResponseFormat
 */
export function parseResponseFormat(json) {
    let parsed;
    try {
        parsed = JSON.parse(json);
    }
    catch (error) {
        throw new Error(`Invalid JSON: ${error.message}`);
    }
    if (parsed === null || typeof parsed !== 'object') {
        throw new Error('Response format must be an object');
    }
    // Validate structure
    const validKeys = new Set(['object', 'aggregates']);
    const keys = Object.keys(parsed);
    // Check for unexpected keys
    const unexpectedKeys = keys.filter((key) => !validKeys.has(key));
    if (unexpectedKeys.length > 0) {
        throw new Error(`Unexpected properties in response format: ${unexpectedKeys.join(', ')}. ` +
            `Only 'object' and 'aggregates' are allowed.`);
    }
    // Must have at least one of object or aggregates
    // Note: Empty objects {} are valid (starting point for visual mode)
    const hasProperties = keys.length > 0;
    if (hasProperties && !parsed.object && !parsed.aggregates) {
        throw new Error('Response format must contain at least one of: object, aggregates');
    }
    return parsed;
}
/**
 * Stringify ResponseFormat to JSON
 *
 * @param format - ResponseFormat object to stringify
 * @returns Pretty-printed JSON string
 */
export function stringifyResponseFormat(format) {
    return JSON.stringify(format, null, 2);
}
/**
 * Get default ResponseFormat (just _id)
 *
 * @returns Default ResponseFormat with only _id selected
 */
export function getDefaultResponseFormat() {
    return {
        object: {
            _id: null,
        },
    };
}
/**
 * Check if a parsed JSON object is a valid ResponseFormat
 *
 * This is a more lenient check than parseResponseFormat - it doesn't throw,
 * just returns a boolean with optional error message
 *
 * @param json - Parsed JSON object to check
 * @returns Object with `valid` boolean and optional `error` message
 */
export function isValidResponseFormatStructure(json) {
    if (json === null || typeof json !== 'object') {
        return { valid: false, error: 'Response format must be an object' };
    }
    const validKeys = new Set(['object', 'aggregates']);
    const keys = Object.keys(json);
    // Check for unexpected keys
    const unexpectedKeys = keys.filter((key) => !validKeys.has(key));
    if (unexpectedKeys.length > 0) {
        return {
            valid: false,
            error: `Unexpected properties: ${unexpectedKeys.join(', ')}. Only 'object' and 'aggregates' are allowed.`,
        };
    }
    // Must have at least one of object or aggregates
    // Note: Empty objects {} are valid (starting point for visual mode)
    const hasProperties = keys.length > 0;
    if (hasProperties && !json.object && !json.aggregates) {
        return {
            valid: false,
            error: 'Response format must contain at least one of: object, aggregates',
        };
    }
    return { valid: true };
}
