/**
 * Metadata-only artifact inspection — the no-browser fallback for
 * `inspect_artifact` while the Playwright runtime is installing (fresh deploy)
 * or unavailable. Parses the bundle's entrypoint markup with linkedom and
 * reports STRUCTURE (frame count, per-frame tag/id/class/text, body text) so
 * the agent keeps some feedback loop; everything that needs a real render —
 * screenshots, layout geometry, console, runtime errors, request failures —
 * is absent, flagged via the result's `degraded` field.
 *
 * Shape-compatible with the real inspection (`ArtifactInspectionResult`) so it
 * flows through `inspectionToModelPayload` and the existing tool plumbing
 * unchanged. Geometry fields are zeroed, not guessed: a zero rect plus the
 * `degraded` note reads as "unknown", a plausible-looking guess would not.
 */
import { parseHTML } from 'linkedom';
import { frameStepperSelector } from '../core/artifact-frame-selectors.js';
const ZERO_RECT = { x: 0, y: 0, width: 0, height: 0 };
function normalizedText(value, limit) {
    return (value ?? '').replace(/\s+/g, ' ').trim().slice(0, limit);
}
export function inspectArtifactMetadata(bundle, options) {
    const frameSelector = options.frameSelector ?? frameStepperSelector(bundle.display?.controls?.stepper?.selector);
    const html = bundle.files[bundle.entrypoint] ?? '';
    const { document } = parseHTML(/<html[\s>]/i.test(html) ? html : `<!doctype html><html><body>${html}</body></html>`);
    let frameElements = [];
    try {
        frameElements = Array.from(document.querySelectorAll(frameSelector));
    }
    catch {
        frameElements = []; // selector linkedom can't evaluate → report zero frames, the degraded note explains
    }
    const frames = frameElements.map((el, index) => ({
        index,
        tagName: el.tagName.toLowerCase(),
        id: el.id || undefined,
        className: typeof el.className === 'string' ? el.className || undefined : undefined,
        textSnippet: normalizedText(el.textContent, 500) || undefined,
        rect: { ...ZERO_RECT },
    }));
    const viewport = bundle.display?.fullscreenViewport ?? bundle.viewport;
    return {
        artifactId: bundle.artifactId,
        title: bundle.title,
        kind: bundle.kind,
        context: options.context ?? 'editor',
        viewport: {
            width: Math.max(1, Math.round(viewport.width)),
            height: Math.max(1, Math.round(viewport.height)),
        },
        documentSize: { width: 0, height: 0 },
        frameSelector,
        frameCount: frames.length,
        bodyTextSnippet: normalizedText(document.body?.textContent, 1200),
        console: [],
        pageErrors: [],
        requestFailures: [],
        frames,
        degraded: {
            reason: options.reason,
            note: 'Inspected WITHOUT a browser: frame data is parsed from the static markup. '
                + 'Screenshots, layout geometry (rects and documentSize are zeroed), console output, runtime errors, '
                + 'and request failures are unavailable. Re-run inspect once the browser runtime is ready for the full picture.',
        },
    };
}
//# sourceMappingURL=artifact-inspect-metadata.js.map