"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.TeamComposer = void 0;
const crypto = __importStar(require("crypto"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const MEMBER_SALT = 'fivo-cell-team-v1';
function hashMemberId(username) {
    return crypto.createHash('sha256').update(MEMBER_SALT + username).digest('hex').substring(0, 16);
}
class TeamComposer {
    store;
    teamDir;
    constructor(store, cellDir) {
        this.store = store;
        this.teamDir = path.join(cellDir, 'team');
        this.ensureDirectory(this.teamDir);
    }
    computeTeamWeight(member) {
        const seniorityFactors = {
            junior: 1.0,
            mid: 1.5,
            senior: 2.0,
            principal: 2.5,
            lead: 3.0,
        };
        const rawWeight = Math.log(1 + member.signalCount) * seniorityFactors[member.seniority];
        return Math.round(rawWeight * 100) / 100;
    }
    computeTeamTaste(members) {
        const allPatterns = this.store.getAllPatterns();
        const teamPatterns = allPatterns.map((p) => ({ ...p, scope: 'team' }));
        return {
            teamId: `team_${Date.now()}`,
            members,
            patterns: teamPatterns,
            lastSynced: new Date().toISOString(),
        };
    }
    pushTaste(username) {
        const model = this.store.exportTasteModel();
        const memberHash = hashMemberId(username);
        const memberFile = path.join(this.teamDir, `${memberHash}.json`);
        if (fs.existsSync(memberFile)) {
            const backupFile = path.join(this.teamDir, `${memberHash}.backup.json`);
            fs.copyFileSync(memberFile, backupFile);
        }
        const memberData = {
            id: memberHash,
            taste: model,
            pushedAt: new Date().toISOString(),
        };
        fs.writeFileSync(memberFile, JSON.stringify(memberData, null, 2), 'utf-8');
        return true;
    }
    pullTaste(username) {
        if (username) {
            const memberHash = hashMemberId(username);
            const memberFile = path.join(this.teamDir, `${memberHash}.json`);
            if (!fs.existsSync(memberFile))
                return null;
            const data = JSON.parse(fs.readFileSync(memberFile, 'utf-8'));
            return data.taste;
        }
        return this.pullTeamTaste();
    }
    pullTeamTaste() {
        const files = fs.readdirSync(this.teamDir).filter((f) => f.endsWith('.json'));
        if (files.length === 0)
            return null;
        const allPatterns = [];
        const patternMap = new Map();
        for (const file of files) {
            const data = JSON.parse(fs.readFileSync(path.join(this.teamDir, file), 'utf-8'));
            const taste = data.taste;
            for (const pattern of taste.patterns) {
                const key = `${pattern.category}_${pattern.from}_${pattern.to}`;
                if (patternMap.has(key)) {
                    const existing = patternMap.get(key);
                    existing.pattern.signals.positive += pattern.signals.positive;
                    existing.pattern.signals.negative += pattern.signals.negative;
                    existing.pattern.signals.total += pattern.signals.total;
                    existing.count++;
                }
                else {
                    patternMap.set(key, { pattern: { ...pattern, scope: 'team' }, count: 1 });
                }
            }
        }
        for (const [, value] of patternMap) {
            allPatterns.push(value.pattern);
        }
        return {
            version: '0.1.0',
            lastUpdated: new Date().toISOString(),
            totalSignals: allPatterns.reduce((sum, p) => sum + p.signals.total, 0),
            patterns: allPatterns,
        };
    }
    getTeamMembers() {
        if (!fs.existsSync(this.teamDir))
            return [];
        return fs
            .readdirSync(this.teamDir)
            .filter((f) => f.endsWith('.json') && !f.endsWith('.backup.json'))
            .map((f) => {
            const data = JSON.parse(fs.readFileSync(path.join(this.teamDir, f), 'utf-8'));
            return data.id || f.replace('.json', '');
        });
    }
    getOnboardingWeight(weekNumber) {
        const ramp = {
            1: 0.2,
            2: 0.4,
            3: 0.6,
            4: 0.8,
        };
        return ramp[weekNumber] || 0.8;
    }
    ensureDirectory(dir) {
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
        }
    }
}
exports.TeamComposer = TeamComposer;
//# sourceMappingURL=team-composer.js.map