// tools/define-ui-contract-web/states.ts — Data flow, loading states, and error states
// -- Data flow generation -----------------------------------------------------
export function generateDataFlow(componentTree, knowledge) {
    const flows = [];
    const firstContract = knowledge.apiContracts[0];
    const apiType = firstContract ? firstContract.type : 'REST';
    for (const page of componentTree) {
        flows.push({
            from: 'Server/API',
            to: page.name,
            data: 'Initial page data',
            method: `${apiType} fetch on server`,
        });
        for (const child of page.children) {
            flows.push({
                from: page.name,
                to: child.name,
                data: 'Props (data + callbacks)',
                method: child.type === 'client' ? 'React props / Vue props' : 'Server component props',
            });
            if (child.type === 'client' && child.name.includes('Form')) {
                flows.push({
                    from: child.name,
                    to: 'Server/API',
                    data: 'Form submission',
                    method: `${apiType} POST/PUT`,
                });
            }
            if (child.type === 'client' && child.name.includes('List')) {
                flows.push({
                    from: child.name,
                    to: 'Server/API',
                    data: 'Filter/sort/pagination params',
                    method: `${apiType} GET with query params`,
                });
            }
        }
    }
    return flows;
}
// -- Loading states -----------------------------------------------------------
export function generateLoadingStates(componentTree) {
    const states = [];
    for (const page of componentTree) {
        states.push({
            component: page.name,
            trigger: 'Initial page load / data fetch',
            ui: 'Full-page skeleton loader with content placeholders',
        });
        for (const child of page.children) {
            if (child.type === 'client') {
                if (child.name.includes('Form')) {
                    states.push({
                        component: child.name,
                        trigger: 'Form submission',
                        ui: 'Submit button shows spinner, form fields disabled',
                    });
                }
                if (child.name.includes('List')) {
                    states.push({
                        component: child.name,
                        trigger: 'Data loading / filter change',
                        ui: 'Skeleton rows replacing list items, maintain layout height',
                    });
                }
                if (child.name.includes('Actions')) {
                    states.push({
                        component: child.name,
                        trigger: 'Action in progress',
                        ui: 'Action button disabled with loading indicator',
                    });
                }
            }
        }
    }
    return states;
}
// -- Error states -------------------------------------------------------------
export function generateErrorStates(componentTree) {
    const states = [];
    for (const page of componentTree) {
        states.push({
            component: page.name,
            trigger: 'Server error (5xx) or network failure',
            ui: 'Error boundary with "Something went wrong" message and retry button',
            recovery: 'Retry button triggers full page refresh / re-fetch',
        });
        states.push({
            component: page.name,
            trigger: 'Not found (404)',
            ui: 'Not found page with link back to parent view',
            recovery: 'Navigation to list view',
        });
        for (const child of page.children) {
            if (child.name.includes('Form')) {
                states.push({
                    component: child.name,
                    trigger: 'Validation error (client-side)',
                    ui: 'Inline error messages below invalid fields, red border highlight',
                    recovery: 'User corrects the field, error clears on change',
                });
                states.push({
                    component: child.name,
                    trigger: 'Server rejection (400/422)',
                    ui: 'Toast notification with error message, form remains editable',
                    recovery: 'User corrects data and resubmits',
                });
            }
            if (child.name.includes('List')) {
                states.push({
                    component: child.name,
                    trigger: 'Empty state (no results)',
                    ui: 'Empty illustration with helpful message and CTA button',
                    recovery: 'Clear filters or create new item CTA',
                });
                states.push({
                    component: child.name,
                    trigger: 'Fetch error',
                    ui: 'Error banner above list with retry button, show stale data if available',
                    recovery: 'Retry button re-fetches data',
                });
            }
        }
    }
    return states;
}
//# sourceMappingURL=states.js.map