/**
 * Hierarchical data model v2 for FB Form instances.
 *
 * Single source of truth for all form data across instances. The structure is
 * flattened compared to v1 — validations and clarifications are direct arrays
 * on each entry rather than nested inside activities[].
 *
 *   data[]
 *   ├── question (tagged with form_instance)
 *   ├── client_data[]
 *   │   └── client_input → processed_data[] (LLM-split items)
 *   ├── validations[] (flat, each with processed_data_ref)
 *   └── clarifications[] (flat, each with thread[], review, response_data[])
 *
 * Key design principles:
 * - Instance-independent: one data[] spans all form instances
 * - Thread captures full conversation history across instances
 * - Review decisions are direct properties, not buried in activities
 * - Clarifications reference processed_data via IDs, not nesting
 * - response_data links to thread entries via thread_ref + thread_index
 *
 * ID notation:
 *   ie_N         — input element (sequential)
 *   pd_N.M       — processed data (ie prefix + sequential suffix)
 *   val_N        — validation (sequential within entry)
 *   clr_*        — clarification (real IDs, not synthetic)
 *   th_N         — thread entry (sequential within clarification)
 */
// ========================================
// ID Generation Utilities
// ========================================
/**
 * Extract the numeric suffix from an ID string.
 * e.g. 'ie_3' → 3, 'pd_1.2' → 2, 'th_5' → 5
 */
function extract_last_number(id) {
    const parts = id.split('_');
    const num_part = parts[parts.length - 1];
    const segments = num_part.split('.');
    return parseInt(segments[segments.length - 1], 10) || 0;
}
/**
 * Find the maximum ie_N number across all data entries.
 */
function find_max_ie_num(data) {
    let max = 0;
    for (const entry of data) {
        if (!entry.client_data)
            continue;
        for (const item of entry.client_data) {
            const num = extract_last_number(item.input_elt_id);
            if (num > max)
                max = num;
        }
        // Also check response_data in clarifications
        for (const clr of entry.clarifications ?? []) {
            for (const rd of clr.response_data) {
                for (const pd of rd.processed_data) {
                    const num = extract_last_number(pd.processed_data_id);
                    if (num > max)
                        max = num;
                }
            }
        }
    }
    return max;
}
/** Generate the next input element ID (ie_N) */
export function next_ie_id(data) {
    return `ie_${find_max_ie_num(data) + 1}`;
}
/** Generate the next processed data ID (pd_N.M) for a given input element number */
export function next_pd_id(ie_num, existing_pds) {
    let max = 0;
    for (const pd of existing_pds) {
        const num = extract_last_number(pd.processed_data_id);
        if (num > max)
            max = num;
    }
    return `pd_${ie_num}.${max + 1}`;
}
/** Generate the next validation ID (val_N) within an entry */
export function next_val_id(existing_validations) {
    let max = 0;
    for (const v of existing_validations) {
        const num = extract_last_number(v.validation_id);
        if (num > max)
            max = num;
    }
    return `val_${max + 1}`;
}
/** Generate the next thread ID (th_N) within a clarification */
export function next_thread_id(existing_thread) {
    let max = 0;
    for (const t of existing_thread) {
        const num = extract_last_number(t.thread_id);
        if (num > max)
            max = num;
    }
    return `th_${max + 1}`;
}
//# sourceMappingURL=shared_data.js.map