"use strict";
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.doInstallHook = doInstallHook;
exports.doUninstallHook = doUninstallHook;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const utils_1 = require("../utils");
function doInstallHook() {
    const gitDir = path.join(process.cwd(), '.git');
    if (!fs.existsSync(gitDir)) {
        (0, utils_1.printWarning)('No .git directory found. Run this from your project root.');
        return;
    }
    const hooksDir = path.join(gitDir, 'hooks');
    if (!fs.existsSync(hooksDir))
        fs.mkdirSync(hooksDir, { recursive: true });
    const preCommitPath = path.join(hooksDir, 'pre-commit');
    const postCommitPath = path.join(hooksDir, 'post-commit');
    // Check for existing hooks
    const existing = [];
    for (const p of [preCommitPath, postCommitPath]) {
        if (fs.existsSync(p)) {
            const content = fs.readFileSync(p, 'utf-8');
            if (!content.includes('FIVO Cell')) {
                existing.push(path.basename(p));
            }
        }
    }
    if (existing.length > 0) {
        (0, utils_1.printWarning)(`Existing hooks found: ${existing.join(', ')}. Backed up as .bak`);
        for (const name of existing) {
            const p = path.join(hooksDir, name);
            try {
                fs.copyFileSync(p, p + '.bak');
            }
            catch { }
        }
    }
    // pre-commit: scan before commit
    const preCommit = `#!/bin/sh
# FIVO Cell — pre-commit hook
echo "[cell] Scanning changes..."
cell scan --quick --silent 2>/dev/null || true
`;
    // post-commit: log learnings
    const postCommit = `#!/bin/sh
# FIVO Cell — post-commit hook
COMMIT_MSG=$(git log -1 --pretty=%B)
echo "[cell] Learning from commit..."
cell sync push --hook "$COMMIT_MSG" 2>/dev/null || true
`;
    let installed = 0;
    try {
        fs.writeFileSync(preCommitPath, preCommit, { mode: 0o755 });
        installed++;
    }
    catch (e) {
        (0, utils_1.printWarning)(`Failed to write pre-commit: ${String(e)}`);
    }
    try {
        fs.writeFileSync(postCommitPath, postCommit, { mode: 0o755 });
        installed++;
    }
    catch (e) {
        (0, utils_1.printWarning)(`Failed to write post-commit: ${String(e)}`);
    }
    if (installed > 0) {
        (0, utils_1.printSuccess)(`${installed}/2 git hooks installed`);
        (0, utils_1.printDim)('  Cell will auto-learn on every git commit');
        (0, utils_1.printDim)('  To remove: delete .git/hooks/pre-commit and .git/hooks/post-commit');
    }
}
function doUninstallHook() {
    const preCommitPath = path.join(process.cwd(), '.git', 'hooks', 'pre-commit');
    const postCommitPath = path.join(process.cwd(), '.git', 'hooks', 'post-commit');
    let removed = 0;
    for (const p of [preCommitPath, postCommitPath]) {
        if (fs.existsSync(p)) {
            try {
                const content = fs.readFileSync(p, 'utf-8');
                if (content.includes('FIVO Cell')) {
                    fs.unlinkSync(p);
                    removed++;
                }
            }
            catch (e) {
                (0, utils_1.printWarning)(`Failed to remove ${path.basename(p)}: ${String(e)}`);
            }
        }
    }
    if (removed > 0) {
        (0, utils_1.printSuccess)(`${removed} cell hooks removed`);
    }
    else {
        (0, utils_1.printDim)('No cell hooks found');
    }
}
//# sourceMappingURL=hook.js.map