"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.HandoffDetector = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class HandoffDetector {
    handoffPath;
    absenceThresholdDays;
    constructor(cellDir) {
        this.handoffPath = path.join(cellDir, 'handoffs');
        this.absenceThresholdDays = 2;
        this.ensureDirectory(this.handoffPath);
    }
    detectAbsence(teammate, branch) {
        const lastActivity = this.getLastActivity(teammate, branch);
        if (!lastActivity)
            return false;
        const daysSince = (Date.now() - new Date(lastActivity).getTime()) / (86400000);
        return daysSince >= this.absenceThresholdDays;
    }
    generateReturnSummary(teammate, branch, filesChanged, patternsUsed, todos, lastCommit) {
        const summary = {
            teammate,
            branch,
            absentDays: Math.max(0, Math.floor((Date.now() - new Date(lastCommit).getTime()) / 86400000)),
            filesChanged,
            patternsUsed,
            todos,
            lastCommit,
        };
        const filePath = path.join(this.handoffPath, `${teammate}_${this.sanitizeBranch(branch)}.json`);
        fs.writeFileSync(filePath, JSON.stringify(summary, null, 2), 'utf-8');
        return summary;
    }
    getReturnPrompt(summary) {
        return `${summary.teammate} was away for ${summary.absentDays} days. ${summary.filesChanged.length} files changed on ${summary.branch}. ${summary.todos.length} TODOs pending. Review changes or continue from TODO?`;
    }
    getReviewPrompt(summary) {
        return `Review changes on ${summary.branch}:\n${summary.filesChanged.map((f) => `  - ${f}`).join('\n')}\n\nPatterns used: ${summary.patternsUsed.join(', ')}\n\nTODOs:\n${summary.todos.map((t) => `  - ${t}`).join('\n')}`;
    }
    getContinuePrompt(summary) {
        return `Continue from TODO on ${summary.branch}:\n${summary.todos.map((t) => `  - ${t}`).join('\n')}`;
    }
    trackActivity(teammate, branch) {
        const activityPath = path.join(this.handoffPath, 'activity.json');
        let activities = {};
        if (fs.existsSync(activityPath)) {
            try {
                activities = JSON.parse(fs.readFileSync(activityPath, 'utf-8'));
            }
            catch { }
        }
        const key = `${teammate}_${this.sanitizeBranch(branch)}`;
        activities[key] = new Date().toISOString();
        try {
            fs.writeFileSync(activityPath, JSON.stringify(activities, null, 2), 'utf-8');
        }
        catch { }
    }
    getLastActivity(teammate, branch) {
        const activityPath = path.join(this.handoffPath, 'activity.json');
        if (!fs.existsSync(activityPath))
            return null;
        try {
            const activities = JSON.parse(fs.readFileSync(activityPath, 'utf-8'));
            const key = `${teammate}_${this.sanitizeBranch(branch)}`;
            return activities[key] || null;
        }
        catch {
            return null;
        }
    }
    sanitizeBranch(branch) {
        return branch.replace(/[^a-zA-Z0-9_-]/g, '_');
    }
    ensureDirectory(dir) {
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
        }
    }
}
exports.HandoffDetector = HandoffDetector;
//# sourceMappingURL=handoff-detector.js.map