"use strict";
/**
 * 🧹 Clear Command - Reset caches and temporary state
 * Claude Code consistency: Similar to /clear command
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearCommand = clearCommand;
const championship_style_1 = require("../utils/championship-style");
const file_utils_1 = require("../utils/file-utils");
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
/**
 * Clear caches, temporary files, and reset state
 */
async function clearCommand(options = {}) {
    try {
        const startTime = Date.now();
        console.log(championship_style_1.FAF_COLORS.fafCyan(`${championship_style_1.FAF_ICONS.broom} Clear Cache & State`));
        console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}Resetting state for fresh context`);
        // Default to clearing all if no specific options
        const clearAll = options.all || (!options.cache && !options.todos && !options.backups);
        let itemsCleared = 0;
        // Clear trust cache
        if (options.cache || clearAll) {
            itemsCleared += await clearTrustCache();
        }
        // Clear todo lists
        if (options.todos || clearAll) {
            itemsCleared += await clearTodoLists();
        }
        // Clear backup files
        if (options.backups || clearAll) {
            itemsCleared += await clearBackupFiles();
        }
        const duration = Date.now() - startTime;
        console.log();
        if (itemsCleared > 0) {
            console.log(championship_style_1.FAF_COLORS.fafGreen(`${championship_style_1.FAF_ICONS.party} Cleared ${itemsCleared} items in ${duration}ms!`));
            console.log(championship_style_1.FAF_COLORS.fafCyan('Fresh start - all caches and temporary files removed'));
        }
        else {
            console.log(championship_style_1.FAF_COLORS.fafCyan(`${championship_style_1.FAF_ICONS.sparkles} Already clean! No items to clear`));
        }
        console.log(`${championship_style_1.FAF_COLORS.fafCyan(`${championship_style_1.FAF_ICONS.magic_wand} Try: `)}faf status${championship_style_1.FAF_COLORS.fafCyan(' - Check fresh system status')}`);
    }
    catch (error) {
        console.error(championship_style_1.FAF_COLORS.fafOrange(`${championship_style_1.FAF_ICONS.shield} Clear failed: ${error instanceof Error ? error.message : String(error)}`));
        process.exit(1);
    }
}
/**
 * Clear trust cache in home directory
 */
async function clearTrustCache() {
    try {
        const cacheDir = path.join(os.homedir(), '.faf-cli-cache');
        const trustCachePath = path.join(cacheDir, 'trust-cache.json');
        try {
            await fs.access(trustCachePath);
            await fs.unlink(trustCachePath);
            console.log(`${championship_style_1.FAF_COLORS.fafGreen('├─ ')}Trust cache cleared`);
            return 1;
        }
        catch {
            console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}No trust cache to clear`);
            return 0;
        }
    }
    catch {
        return 0;
    }
}
/**
 * Clear todo lists in current project and parents
 */
async function clearTodoLists() {
    let cleared = 0;
    try {
        const fafPath = await (0, file_utils_1.findFafFile)();
        if (fafPath) {
            const projectDir = path.dirname(fafPath);
            const todoPath = path.join(projectDir, '.faf-todos.json');
            try {
                await fs.access(todoPath);
                await fs.unlink(todoPath);
                console.log(`${championship_style_1.FAF_COLORS.fafGreen('├─ ')}Todo list cleared`);
                cleared++;
            }
            catch {
                console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}No todo list to clear`);
            }
        }
    }
    catch {
        console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}No .faf project found`);
    }
    return cleared;
}
/**
 * Clear backup files (.faf.backup*, .faf.garage-backup, etc.)
 */
async function clearBackupFiles() {
    let cleared = 0;
    try {
        const fafPath = await (0, file_utils_1.findFafFile)();
        if (fafPath) {
            const projectDir = path.dirname(fafPath);
            const files = await fs.readdir(projectDir);
            const backupFiles = files.filter(file => file.startsWith('.faf.backup') ||
                file.endsWith('.garage-backup') ||
                file.endsWith('.backup'));
            for (const backupFile of backupFiles) {
                const backupPath = path.join(projectDir, backupFile);
                try {
                    await fs.unlink(backupPath);
                    console.log(`${championship_style_1.FAF_COLORS.fafGreen('├─ ')}Cleared ${backupFile}`);
                    cleared++;
                }
                catch {
                    // Skip if can't delete
                }
            }
            if (backupFiles.length === 0) {
                console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}No backup files to clear`);
            }
        }
    }
    catch {
        console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}No .faf project found`);
    }
    return cleared;
}
//# sourceMappingURL=clear.js.map