import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * DataSetupScreen — Checks whether the connected Amplitude project has event data.
 *
 * Calls the Amplitude Data API to determine the project's activation level:
 *   'full'    — 50+ events / well-established data
 *   'partial' — snippet installed but few events
 *   'none'    — no events, no snippet
 *
 * The router's isComplete predicate (projectHasData !== null) advances past
 * this screen automatically once the value is set.
 */
import { Box, Text } from 'ink';
import { useEffect, useState } from 'react';
import { useWizardStore } from '../hooks/useWizardStore.js';
import { useEscapeBack } from '../hooks/useEscapeBack.js';
import { useScreenInput } from '../hooks/useScreenInput.js';
import { Colors, Icons } from '../styles.js';
import { BrailleSpinner } from '../components/BrailleSpinner.js';
import { fetchProjectActivationStatus } from '../../../lib/api.js';
import { DEFAULT_AMPLITUDE_ZONE } from '../../../lib/constants.js';
import { detectAmplitudeInProject, detectAmplitudeInProjectSource, isProjectFullyWired, } from '../../../lib/detect-amplitude.js';
import { analytics } from '../../../utils/analytics.js';
import { resolveZone } from '../../../lib/zone-resolution.js';
import { withTimeout } from '../utils/with-timeout.js';
import { logToFile } from '../../../utils/debug.js';
const ACTIVATION_FETCH_TIMEOUT_MS = 30_000;
export const DataSetupScreen = ({ store }) => {
    useWizardStore(store);
    // Esc → back to Auth's org/project picker. The activation API call is
    // idempotent so backing out mid-flight is safe.
    useEscapeBack(store);
    // Surfaces the timeout fallback prompt — "Couldn't reach Amplitude;
    // continue anyway?" — when the activation fetch hangs past 30s. This
    // matters because a hung proxy with no response would otherwise leave
    // the spinner ticking forever.
    const [timedOut, setTimedOut] = useState(false);
    useScreenInput((input) => {
        if (!timedOut)
            return;
        if (input.toLowerCase() === 'y') {
            // User wants to continue without confirmed activation status.
            // Fall through with 'none' so the framework-detection flow runs.
            store.setActivationLevel('none');
        }
    }, { isActive: timedOut });
    useEffect(() => {
        if (store.session.projectHasData !== null)
            return;
        const { credentials, selectedOrgId, selectedProjectId } = store.session;
        // credentials.appId is 0 for OAuth users; fall back to the project UUID
        const appId = store.session.credentials?.appId || selectedProjectId || null;
        // No credentials or project ID — can't check, fall through to Framework Detection
        if (!credentials || !appId || !selectedOrgId) {
            logToFile('[DataSetup] no credentials/appId — skipping activation check');
            store.setActivationLevel('none');
            return;
        }
        // readDisk: false — region is populated on the session by the time this
        // screen renders (flow is gated on region !== null). Skipping Tier 2/3
        // avoids synchronous readAmpliConfig + getStoredUser disk reads on mount.
        const zone = resolveZone(store.session, DEFAULT_AMPLITUDE_ZONE, {
            readDisk: false,
        });
        logToFile(`[DataSetup] checking activation for appId=${appId} zone=${zone}`);
        // Pre-flight: if the project already has all four local "fully wired"
        // signals (SDK dep, source import, ampli.json scope, event plan on
        // disk), skip the agent loop entirely. The user is re-running the
        // wizard on a project a prior run already finished — re-running the
        // agent for 2-3 minutes to no-op is a launch-blocking UX bug. We set
        // `activationLevel = 'full'` (which the router uses to skip Setup +
        // Run) and `localInstrumentationComplete = true` (which the router
        // uses to KEEP DataIngestionCheck running, since remote events may
        // not have arrived yet — the user may be re-running pre-deploy).
        const wireCheck = isProjectFullyWired(store.session.installDir);
        logToFile(`[DataSetup] local wiring check: present=[${wireCheck.present.join(',')}] missing=[${wireCheck.missing.join(',')}] fullyWired=${wireCheck.fullyWired}`);
        if (wireCheck.fullyWired) {
            analytics.wizardCapture('activation skip on local wiring', {
                present: wireCheck.present,
            });
            store.setLocalInstrumentationComplete(true);
            store.setSnippetConfigured(true);
            store.setActivationLevel('full');
            return;
        }
        // Run local static check in parallel with the API call.
        const localDetection = detectAmplitudeInProject(store.session.installDir);
        logToFile(`[DataSetup] local detection: confidence=${localDetection.confidence} reason=${localDetection.reason ?? 'none'}`);
        void withTimeout(fetchProjectActivationStatus({
            accessToken: credentials.accessToken,
            zone,
            appId,
            orgId: selectedOrgId,
        }), ACTIVATION_FETCH_TIMEOUT_MS, 'fetchProjectActivationStatus')
            .then((status) => {
            logToFile(`[DataSetup] activation status: ${JSON.stringify(status)}`);
            store.setSnippetConfigured(status.hasDetSource);
            if (status.hasAnyEvents && status.hasDetSource) {
                // Has both SDK and events — use a heuristic: if all three core event types
                // are present, call it full.
                const isFull = status.hasPageViewedEvent &&
                    status.hasSessionStartEvent &&
                    status.hasSessionEndEvent;
                // Cross-check the LOCAL project: the Amplitude project may have
                // events from a previous install that has since been deleted from
                // disk. If no source file actually imports an Amplitude SDK, the
                // remote "full" verdict is a stale ghost — rerun setup. We use the
                // source-only grep (not detectAmplitudeInProject) so a leftover
                // dependency entry in package.json doesn't paper over a deleted
                // initializer file. Without this guard, deleting amplitude.js but
                // keeping the package.json dep silently skipped Setup.
                if (isFull &&
                    detectAmplitudeInProjectSource(store.session.installDir)
                        .confidence === 'none') {
                    logToFile('[DataSetup] remote says full but no source imports — downgrading to partial');
                    store.setActivationLevel('partial');
                }
                else {
                    store.setActivationLevel(isFull ? 'full' : 'partial');
                }
            }
            else if (status.hasDetSource) {
                // SDK installed but no events yet
                store.setActivationLevel('partial');
            }
            else if (localDetection.confidence !== 'none') {
                // API sees no SDK, but local files suggest Amplitude is already installed.
                // Treat as partial so the wizard asks what they need rather than running
                // the full install agent.
                logToFile(`[DataSetup] upgrading to partial via local detection: ${localDetection.reason}`);
                store.setActivationLevel('partial');
            }
            else {
                // No SDK, no events, no local evidence — full install needed
                store.setActivationLevel('none');
            }
        })
            .catch((err) => {
            const message = err instanceof Error ? err.message : String(err);
            const isTimeout = err instanceof Error && err.name === 'TimeoutError';
            logToFile(`[DataSetup] activation check failed: ${message}${isTimeout ? ' (timeout)' : ''} — ${isTimeout
                ? 'awaiting user decision'
                : 'falling back to local detection'}`);
            if (isTimeout) {
                // Surface the manual fallback prompt and let the user decide.
                // Local detection alone isn't strong enough signal to silently
                // commit a path — the user knows whether their network is the
                // problem and we should ask.
                setTimedOut(true);
                return;
            }
            // Other errors: use local detection as a fallback.
            store.setActivationLevel(localDetection.confidence !== 'none' ? 'partial' : 'none');
        });
    }, []);
    if (timedOut) {
        return (_jsx(Box, { flexDirection: "column", flexGrow: 1, children: _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, color: Colors.heading, children: "Checking project setup" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.warning, children: "Couldn't reach Amplitude after 30s. Network or proxy issue?" }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.body, children: "Continue anyway? We'll skip the activation check and run full setup." }) }), _jsxs(Box, { marginTop: 1, gap: 2, children: [_jsxs(Box, { children: [_jsx(Text, { color: Colors.muted, children: "[" }), _jsx(Text, { bold: true, color: Colors.body, children: "Y" }), _jsx(Text, { color: Colors.muted, children: "] Yes, continue" })] }), _jsxs(Box, { children: [_jsx(Text, { color: Colors.muted, children: "[" }), _jsx(Text, { bold: true, color: Colors.body, children: "Esc" }), _jsx(Text, { color: Colors.muted, children: "] Cancel and go back" })] })] })] }) }));
    }
    return (_jsx(Box, { flexDirection: "column", flexGrow: 1, children: _jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, color: Colors.heading, children: "Checking project setup" }), _jsxs(Box, { marginTop: 1, gap: 1, children: [_jsx(BrailleSpinner, { color: Colors.accent }), _jsxs(Text, { color: Colors.body, children: ["Analyzing your Amplitude project", Icons.ellipsis] })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.muted, children: "Looking for existing SDK installation and event data" }) })] }) }));
};
