"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeSkillFileIfNeeded = writeSkillFileIfNeeded;
exports.writeCursorRulesIfNeeded = writeCursorRulesIfNeeded;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const logger_1 = require("./logger");
const claude_skill_1 = require("./skills/claude-skill");
const cursor_rules_1 = require("./skills/cursor-rules");
const log = (0, logger_1.createLogger)('SkillWriter');
const SKILLS_BASE = path_1.default.join(os_1.default.homedir(), '.claude', 'skills');
const SKILL_DIR = path_1.default.join(SKILLS_BASE, claude_skill_1.CLAUDE_SKILL_DIR);
const SKILL_PATH = path_1.default.join(SKILL_DIR, claude_skill_1.CLAUDE_SKILL_FILENAME);
function getInstalledVersion(content, pattern) {
    const match = content.match(pattern);
    return match ? parseInt(match[1], 10) : 0;
}
/**
 * Write the companion Claude skill file.
 * Idempotent — skips if the installed version is current or newer.
 * Returns a result with success and optional error message for user-facing hints.
 */
function writeSkillFileIfNeeded() {
    try {
        const skillsBase = path_1.default.join(os_1.default.homedir(), '.claude', 'skills');
        if (!fs_1.default.existsSync(skillsBase)) {
            fs_1.default.mkdirSync(skillsBase, { recursive: true });
        }
        // If the old flat file exists at SKILL_DIR, remove it — we need a directory there now
        if (fs_1.default.existsSync(SKILL_DIR) && fs_1.default.statSync(SKILL_DIR).isFile()) {
            fs_1.default.unlinkSync(SKILL_DIR);
        }
        if (!fs_1.default.existsSync(SKILL_DIR)) {
            fs_1.default.mkdirSync(SKILL_DIR, { recursive: true });
        }
        if (fs_1.default.existsSync(SKILL_PATH)) {
            const existing = fs_1.default.readFileSync(SKILL_PATH, 'utf8');
            const installed = getInstalledVersion(existing, /rivet-skill-version:\s*(\d+)/);
            if (installed >= claude_skill_1.CLAUDE_SKILL_VERSION)
                return { success: true };
        }
        fs_1.default.writeFileSync(SKILL_PATH, claude_skill_1.CLAUDE_SKILL_CONTENT, 'utf8');
        log.info('Wrote Rivet Claude skill file');
        return { success: true };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        const code = error instanceof Error && 'code' in error
            ? error.code
            : '';
        log.debug('Could not write skill file:', error);
        return { success: false, error: code ? `${code}: ${message}` : message };
    }
}
/**
 * Write a Cursor rules file to {projectPath}/.cursor/rules/rivet.mdc.
 * Idempotent — skips if the installed version is current or newer.
 * Returns true if the file is current or was successfully written.
 */
function writeCursorRulesIfNeeded(projectPath) {
    try {
        const rulesDir = path_1.default.join(projectPath, '.cursor', 'rules');
        const rulesPath = path_1.default.join(rulesDir, cursor_rules_1.CURSOR_RULES_FILENAME);
        if (fs_1.default.existsSync(rulesPath)) {
            const existing = fs_1.default.readFileSync(rulesPath, 'utf8');
            const installed = getInstalledVersion(existing, /rivet-rules-version:\s*(\d+)/);
            if (installed >= cursor_rules_1.CURSOR_RULES_VERSION)
                return true;
        }
        if (!fs_1.default.existsSync(rulesDir)) {
            fs_1.default.mkdirSync(rulesDir, { recursive: true });
        }
        fs_1.default.writeFileSync(rulesPath, cursor_rules_1.CURSOR_RULES_CONTENT, 'utf8');
        log.info('Wrote Rivet Cursor rules file');
        return true;
    }
    catch (error) {
        log.debug('Could not write Cursor rules file:', error);
        return false;
    }
}
//# sourceMappingURL=skillWriter.js.map