// ---------------------------------------------------------------------------
// extractFlows — walk document tree and build flow graph from interactions
// ---------------------------------------------------------------------------
export function extractFlows(file) {
    const pages = file.document.children ?? [];
    const nodeNameMap = buildNodeNameMap(pages);
    const flows = [];
    for (const page of pages) {
        const pageId = page.id;
        const pageName = page.name;
        const pageChildren = page.children ?? [];
        for (const child of pageChildren) {
            if (child.type === 'FRAME' || child.type === 'COMPONENT') {
                collectFrameFlows(child, pageId, pageName, nodeNameMap, flows);
            }
        }
    }
    return flows;
}
// ---------------------------------------------------------------------------
// groupFlowsBySection — partition flows into section buckets
// ---------------------------------------------------------------------------
export function groupFlowsBySection(flows) {
    const result = new Map();
    for (const flow of flows) {
        const existing = result.get(flow.section);
        if (existing !== undefined) {
            existing.push(flow);
        }
        else {
            result.set(flow.section, [flow]);
        }
    }
    return result;
}
// ---------------------------------------------------------------------------
// formatFlowsAsMarkdown — render flows as markdown tables per section
// ---------------------------------------------------------------------------
export function formatFlowsAsMarkdown(sections) {
    if (sections.size === 0) {
        return 'No prototype flows found.';
    }
    const parts = [];
    for (const [sectionName, flows] of sections) {
        const header = `**Section: ${sectionName}**`;
        const tableHeader = '| From Frame | Trigger | To Frame | Transition |';
        const tableSep = '|-----------|---------|---------|-----------|';
        const rows = flows.map((f) => `| ${f.fromFrameName} | ${f.trigger} | ${f.toFrameName} | ${f.transitionType} |`);
        parts.push([header, tableHeader, tableSep, ...rows].join('\n'));
    }
    return parts.join('\n\n');
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
function buildNodeNameMap(pages) {
    const map = new Map();
    for (const page of pages) {
        for (const child of page.children ?? []) {
            map.set(child.id, child.name);
        }
    }
    return map;
}
function collectFrameFlows(frame, pageId, pageName, nodeNameMap, flows) {
    const interactions = frame.interactions ?? [];
    const section = deriveSectionPrefix(frame.name);
    for (const interaction of interactions) {
        const trigger = interaction.trigger.type;
        for (const action of interaction.actions) {
            if (action.destinationId === undefined || action.destinationId === '') {
                continue;
            }
            const toFrameName = nodeNameMap.get(action.destinationId) ?? action.destinationId;
            flows.push({
                fromNodeId: frame.id,
                fromFrameName: frame.name,
                toNodeId: action.destinationId,
                toFrameName,
                trigger,
                transitionType: action.navigation ?? 'NAVIGATE',
                pageId,
                pageName,
                section,
            });
        }
    }
}
function deriveSectionPrefix(name) {
    const slashIndex = name.indexOf('/');
    if (slashIndex > 0) {
        return name.slice(0, slashIndex).trim();
    }
    const spaceDashIndex = name.indexOf(' - ');
    if (spaceDashIndex > 0) {
        return name.slice(0, spaceDashIndex).trim();
    }
    const underscoreIndex = name.indexOf('_');
    if (underscoreIndex > 0) {
        return name.slice(0, underscoreIndex).trim();
    }
    const dashIndex = name.indexOf('-');
    if (dashIndex > 0) {
        return name.slice(0, dashIndex).trim();
    }
    return name.trim();
}
//# sourceMappingURL=flow-analyzer.js.map