import React, { useState, useEffect } from "react";
import { render, Text, Box } from "ink";
import Spinner from "ink-spinner";
import * as fs from "fs";
import * as path from "path";
import { InstanceStateManager } from "../lib/dev/instance-state.js";
function CleanUI() {
    const [state, setState] = useState({ status: "checking" });
    useEffect(() => {
        async function clean() {
            const projectRoot = process.cwd();
            const specificDir = path.join(projectRoot, ".specific");
            // Check if .specific directory exists
            if (!fs.existsSync(specificDir)) {
                setState({ status: "nothing" });
                return;
            }
            // Check if specific dev is running
            const stateManager = new InstanceStateManager(projectRoot);
            const existingInstances = await stateManager.getExistingInstances();
            if (existingInstances) {
                setState({
                    status: "error",
                    error: `Cannot clean while 'specific dev' is running (PID ${existingInstances.owner.pid}). Please stop it first.`,
                });
                return;
            }
            // Clean stale state if any
            await stateManager.cleanStaleState();
            // Remove the .specific directory
            setState({ status: "cleaning" });
            try {
                fs.rmSync(specificDir, { recursive: true, force: true });
                setState({ status: "success" });
            }
            catch (err) {
                setState({
                    status: "error",
                    error: err instanceof Error ? err.message : String(err),
                });
            }
        }
        clean();
    }, []);
    if (state.status === "checking") {
        return (React.createElement(Box, null,
            React.createElement(Text, { color: "blue" },
                React.createElement(Spinner, { type: "dots" })),
            React.createElement(Text, null, " Checking for running instances...")));
    }
    if (state.status === "cleaning") {
        return (React.createElement(Box, null,
            React.createElement(Text, { color: "blue" },
                React.createElement(Spinner, { type: "dots" })),
            React.createElement(Text, null, " Removing .specific directory...")));
    }
    if (state.status === "error") {
        return React.createElement(Text, { color: "red" },
            "Error: ",
            state.error);
    }
    if (state.status === "nothing") {
        return React.createElement(Text, { color: "yellow" }, "Nothing to clean (.specific directory does not exist)");
    }
    return React.createElement(Text, { color: "green" }, "Cleaned .specific directory");
}
export function cleanCommand() {
    render(React.createElement(CleanUI, null));
}
//# sourceMappingURL=clean.js.map