/**
 * Types for the FileTextbox block-based editor component.
 * A FileTextbox value is an array of blocks, where each block is either text or a file attachment.
 */
/** Normalize a raw value to FileTextboxValue (handles string backward compat) */
export function normalize_file_textbox_value(value) {
    if (!value)
        return [{ type: 'text', content: '' }];
    if (typeof value === 'string')
        return [{ type: 'text', content: value }];
    if (Array.isArray(value))
        return value;
    return [{ type: 'text', content: '' }];
}
let _entry_counter = 0;
function next_entry_id() {
    return `entry_${Date.now()}_${++_entry_counter}`;
}
/**
 * Parse flat blocks into grouped entries.
 * Each text block starts a new entry; subsequent file blocks belong to it.
 * Old data with no text blocks becomes a single entry with empty comment.
 */
export function blocks_to_entries(blocks) {
    if (!blocks.length)
        return [];
    const entries = [];
    let current = null;
    for (const block of blocks) {
        if (block.type === 'text') {
            // Start a new entry for each text block
            current = { entry_id: next_entry_id(), comment: block.content, files: [] };
            entries.push(current);
        }
        else {
            // File block — attach to current entry, or create an implicit entry
            if (!current) {
                current = { entry_id: next_entry_id(), comment: '', files: [] };
                entries.push(current);
            }
            current.files.push(block);
        }
    }
    return entries;
}
/** Flatten entries back to FileTextboxBlock[] for onChange. */
export function entries_to_blocks(entries) {
    const blocks = [];
    for (const entry of entries) {
        blocks.push({ type: 'text', content: entry.comment });
        blocks.push(...entry.files);
    }
    // Ensure at least one text block
    if (blocks.length === 0) {
        blocks.push({ type: 'text', content: '' });
    }
    return blocks;
}
//# sourceMappingURL=file_textbox.js.map