import { nowIso } from '../fsx.js';
export const APPSHOTS_CAPABILITY_SCHEMA = 'sks.appshots-capability.v1';
export const APPSHOTS_OFFICIAL_DOC_URL = 'https://developers.openai.com/codex/appshots';
export function detectAppshotsCapability(input = {}) {
    const visualRequired = input.visualRequired === true || needsVisualContext(input.prompt || '');
    const operatorActionRecorded = input.operatorActionRecorded === true;
    const appshotsToolAvailable = input.appshotsToolAvailable === true;
    const threadAttachmentDiscovery = discoverAppshotsThreadAttachments(input.threadAttachments || [], { visualRequired });
    const hasThreadAppshot = threadAttachmentDiscovery.appshot_attachment_count > 0;
    const requiredButMissing = visualRequired && !operatorActionRecorded && !appshotsToolAvailable && !hasThreadAppshot;
    return {
        schema: APPSHOTS_CAPABILITY_SCHEMA,
        generated_at: nowIso(),
        ok: !requiredButMissing && threadAttachmentDiscovery.ok,
        status: visualRequired ? appshotsToolAvailable ? 'available' : 'operator_required' : 'not_required',
        official_doc_url: APPSHOTS_OFFICIAL_DOC_URL,
        visual_required: visualRequired,
        operator_action_required: visualRequired && !operatorActionRecorded && !appshotsToolAvailable && !hasThreadAppshot,
        capability_signals: [
            ...(visualRequired ? ['visual_context_requested'] : ['visual_context_not_required']),
            ...(operatorActionRecorded ? ['operator_action_recorded'] : []),
            ...(appshotsToolAvailable ? ['appshots_tool_available'] : []),
            ...(hasThreadAppshot ? ['codex_thread_appshot_attachment_detected'] : [])
        ],
        thread_attachment_discovery: threadAttachmentDiscovery,
        blockers: [
            ...(requiredButMissing ? ['appshots_operator_action_missing_for_visual_proof'] : []),
            ...threadAttachmentDiscovery.blockers
        ]
    };
}
export function discoverAppshotsThreadAttachments(attachments = [], opts = {}) {
    const rows = attachments.map((attachment) => {
        const kind = normalizeAttachmentKind(attachment.kind, attachment.mime_type, attachment.codex_appshot === true);
        return {
            thread_id: stringOrNull(attachment.thread_id),
            attachment_id: stringOrNull(attachment.attachment_id),
            kind,
            mime_type: stringOrNull(attachment.mime_type),
            source_app: stringOrNull(attachment.source_app),
            source_window: stringOrNull(attachment.source_window),
            local_only: attachment.local_only === true,
            codex_appshot: attachment.codex_appshot === true || kind === 'appshot'
        };
    });
    const appshotAttachmentCount = rows.filter((row) => row.codex_appshot || row.kind === 'appshot').length;
    const blockers = [
        ...(opts.visualRequired === true && attachments.length > 0 && appshotAttachmentCount === 0 ? ['appshots_thread_attachment_missing_appshot_kind'] : []),
        ...rows.filter((row) => row.codex_appshot || row.kind === 'appshot').flatMap((row) => [
            ...(row.thread_id ? [] : [`appshots_thread_attachment_thread_id_missing:${row.attachment_id || 'unknown'}`]),
            ...(row.attachment_id ? [] : [`appshots_thread_attachment_attachment_id_missing:${row.thread_id || 'unknown'}`]),
            ...(row.source_app ? [] : [`appshots_thread_attachment_source_app_missing:${row.attachment_id || 'unknown'}`]),
            ...(row.source_window ? [] : [`appshots_thread_attachment_source_window_missing:${row.attachment_id || 'unknown'}`]),
            ...(row.local_only ? [] : [`appshots_thread_attachment_not_local_only:${row.attachment_id || 'unknown'}`])
        ])
    ];
    return {
        schema: 'sks.appshots-thread-attachment-discovery.v1',
        ok: blockers.length === 0,
        status: attachments.length === 0 ? 'not_provided' : blockers.length > 0 ? 'missing_required' : 'discovered',
        attachments: rows,
        appshot_attachment_count: appshotAttachmentCount,
        blockers,
        warnings: attachments.length === 0 ? ['thread_attachment_metadata_not_provided'] : []
    };
}
function needsVisualContext(prompt) {
    const text = String(prompt || '');
    return /appshot|screenshot|ui|ux|preview|browser|image|design|화면|시각|스크린샷/i.test(text) || /\bvisual\b/i.test(text);
}
function normalizeAttachmentKind(kind, mimeType, codexAppshot) {
    const text = `${kind || ''} ${mimeType || ''}`.toLowerCase();
    if (codexAppshot || /appshot|codex-appshot/.test(text))
        return 'appshot';
    if (/image|png|jpe?g|webp|gif/.test(text))
        return 'image';
    if (/text|markdown|plain|json/.test(text))
        return 'text';
    return 'unknown';
}
function stringOrNull(value) {
    const text = String(value || '').trim();
    return text || null;
}
//# sourceMappingURL=appshots-detector.js.map