// ---------------------------------------------------------------------------
// extractFrames — walk document tree and collect FRAME nodes
// ---------------------------------------------------------------------------
export function extractFrames(file) {
    const frames = [];
    const pages = file.document.children ?? [];
    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' || child.type === 'SECTION') {
                frames.push({
                    nodeId: child.id,
                    name: child.name,
                    pageId,
                    pageName,
                    section: deriveSectionPrefix(child.name),
                });
                // Also collect nested frames one level deep
                collectNestedFrames(child, pageId, pageName, frames);
            }
        }
    }
    return frames;
}
// ---------------------------------------------------------------------------
// groupFramesBySection — partition frames into section buckets
// ---------------------------------------------------------------------------
export function groupFramesBySection(frames, groupBy) {
    const result = new Map();
    for (const frame of frames) {
        const key = groupBy === 'page' ? frame.pageName : frame.section;
        const existing = result.get(key);
        if (existing !== undefined) {
            existing.push(frame);
        }
        else {
            result.set(key, [frame]);
        }
    }
    return result;
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
function deriveSectionPrefix(name) {
    // Priority: "/" > " - " > "_" > "-"
    // "Auth/Login" → "Auth"
    // "Checkout - Step 1" → "Checkout"
    // "Home_Empty" → "Home"
    // "Employees-List" → "Employees"
    // "Dashboard" → "Dashboard"
    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();
}
function collectNestedFrames(parent, pageId, pageName, frames) {
    const children = parent.children ?? [];
    for (const child of children) {
        if (child.type === 'FRAME' || child.type === 'COMPONENT') {
            frames.push({
                nodeId: child.id,
                name: child.name,
                pageId,
                pageName,
                section: deriveSectionPrefix(child.name),
            });
        }
    }
}
const BREAKPOINT_PAGE_KEYWORDS = {
    mobile: ['mobile', 'sm', 'phone', 'iphone', 'android'],
    tablet: ['tablet', 'md', 'ipad'],
    desktop: ['desktop', 'lg', 'xl', 'web', 'pc'],
};
const BREAKPOINT_NAME_SUFFIXES = {
    mobile: ['_mobile', '_sm', '_phone'],
    tablet: ['_tablet', '_md', '_ipad'],
    desktop: ['_desktop', '_lg', '_xl', '_web'],
};
function detectPageBreakpoint(pageName) {
    const lower = pageName.toLowerCase();
    for (const [bp, keywords] of Object.entries(BREAKPOINT_PAGE_KEYWORDS)) {
        for (const kw of keywords) {
            if (lower.includes(kw)) {
                return bp;
            }
        }
    }
    return null;
}
function detectNameSuffixBreakpoint(frameName) {
    const lower = frameName.toLowerCase();
    for (const [bp, suffixes] of Object.entries(BREAKPOINT_NAME_SUFFIXES)) {
        for (const suffix of suffixes) {
            if (lower.endsWith(suffix)) {
                return { breakpoint: bp, baseName: frameName.slice(0, -suffix.length) };
            }
        }
    }
    return null;
}
function makeEmptyBreakpointGroup(section) {
    return {
        section,
        frames: { mobile: [], tablet: [], desktop: [], unknown: [] },
        allNodeIds: [],
    };
}
export function groupFramesByBreakpoint(frames) {
    const result = new Map();
    for (const frame of frames) {
        let sectionKey;
        let breakpoint;
        const suffixMatch = detectNameSuffixBreakpoint(frame.name);
        if (suffixMatch !== null) {
            sectionKey = suffixMatch.baseName;
            breakpoint = suffixMatch.breakpoint;
        }
        else {
            const pageBreakpoint = detectPageBreakpoint(frame.pageName);
            if (pageBreakpoint !== null) {
                sectionKey = frame.name;
                breakpoint = pageBreakpoint;
            }
            else {
                sectionKey = deriveSectionPrefix(frame.name);
                breakpoint = 'unknown';
            }
        }
        const existing = result.get(sectionKey) ?? makeEmptyBreakpointGroup(sectionKey);
        existing.frames[breakpoint].push(frame);
        if (!existing.allNodeIds.includes(frame.nodeId)) {
            existing.allNodeIds.push(frame.nodeId);
        }
        result.set(sectionKey, existing);
    }
    return result;
}
//# sourceMappingURL=frame-extractor.js.map