"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.LAYERS = exports.CELL_DIR = void 0;
exports.savePersonalProfile = savePersonalProfile;
exports.savePersonalStyle = savePersonalStyle;
exports.savePersonalErrors = savePersonalErrors;
exports.savePersonalDecisions = savePersonalDecisions;
exports.savePersonalSession = savePersonalSession;
exports.getPersonalProfile = getPersonalProfile;
exports.getPersonalStyle = getPersonalStyle;
exports.getPersonalErrors = getPersonalErrors;
exports.getPersonalDecisions = getPersonalDecisions;
exports.getPersonalSession = getPersonalSession;
exports.saveTeamMembers = saveTeamMembers;
exports.saveTeamPatterns = saveTeamPatterns;
exports.saveTeamBlindSpots = saveTeamBlindSpots;
exports.getTeamMembers = getTeamMembers;
exports.getTeamPatterns = getTeamPatterns;
exports.getTeamBlindSpots = getTeamBlindSpots;
exports.saveCommunityPatterns = saveCommunityPatterns;
exports.saveCommunityTrends = saveCommunityTrends;
exports.saveCommunityRankings = saveCommunityRankings;
exports.getCommunityPatterns = getCommunityPatterns;
exports.getCommunityTrends = getCommunityTrends;
exports.getCommunityRankings = getCommunityRankings;
exports.populateAllLayers = populateAllLayers;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const CELL_DIR = path.join(os.homedir(), '.fivo', 'cell');
exports.CELL_DIR = CELL_DIR;
const LAYERS = ['personal', 'team', 'community'];
exports.LAYERS = LAYERS;
function ensureDirs() {
    for (const layer of LAYERS) {
        const dir = path.join(CELL_DIR, layer);
        if (!fs.existsSync(dir))
            fs.mkdirSync(dir, { recursive: true });
    }
}
function writeJson(file, data) {
    ensureDirs();
    fs.writeFileSync(file, JSON.stringify(data, null, 2), 'utf-8');
}
function readJson(file) {
    try {
        return JSON.parse(fs.readFileSync(file, 'utf-8'));
    }
    catch {
        return null;
    }
}
// ─── Personal Layer ──────────────────────────────────────────────────────────
function savePersonalProfile(profile) {
    writeJson(path.join(CELL_DIR, 'personal', 'profile.json'), {
        ...profile,
        updatedAt: new Date().toISOString(),
    });
}
function savePersonalStyle(patterns) {
    writeJson(path.join(CELL_DIR, 'personal', 'style.json'), {
        patterns,
        updatedAt: new Date().toISOString(),
    });
}
function savePersonalErrors(errors) {
    writeJson(path.join(CELL_DIR, 'personal', 'errors.json'), {
        patterns: errors,
        updatedAt: new Date().toISOString(),
    });
}
function savePersonalDecisions(decisions) {
    writeJson(path.join(CELL_DIR, 'personal', 'decisions.json'), {
        decisions,
        updatedAt: new Date().toISOString(),
    });
}
function savePersonalSession(session) {
    writeJson(path.join(CELL_DIR, 'personal', 'session.json'), {
        ...session,
        updatedAt: new Date().toISOString(),
    });
}
function getPersonalProfile() {
    return readJson(path.join(CELL_DIR, 'personal', 'profile.json'));
}
function getPersonalStyle() {
    return readJson(path.join(CELL_DIR, 'personal', 'style.json'));
}
function getPersonalErrors() {
    return readJson(path.join(CELL_DIR, 'personal', 'errors.json'));
}
function getPersonalDecisions() {
    return readJson(path.join(CELL_DIR, 'personal', 'decisions.json'));
}
function getPersonalSession() {
    return readJson(path.join(CELL_DIR, 'personal', 'session.json'));
}
// ─── Team Layer ──────────────────────────────────────────────────────────────
function saveTeamMembers(members) {
    writeJson(path.join(CELL_DIR, 'team', 'members.json'), {
        members,
        updatedAt: new Date().toISOString(),
    });
}
function saveTeamPatterns(patterns) {
    writeJson(path.join(CELL_DIR, 'team', 'shared.json'), {
        patterns,
        updatedAt: new Date().toISOString(),
    });
}
function saveTeamBlindSpots(blindSpots) {
    writeJson(path.join(CELL_DIR, 'team', 'blindspots.json'), {
        blindSpots,
        updatedAt: new Date().toISOString(),
    });
}
function getTeamMembers() {
    return readJson(path.join(CELL_DIR, 'team', 'members.json'));
}
function getTeamPatterns() {
    return readJson(path.join(CELL_DIR, 'team', 'shared.json'));
}
function getTeamBlindSpots() {
    return readJson(path.join(CELL_DIR, 'team', 'blindspots.json'));
}
// ─── Community Layer ─────────────────────────────────────────────────────────
function saveCommunityPatterns(patterns) {
    writeJson(path.join(CELL_DIR, 'community', 'patterns.json'), {
        patterns,
        updatedAt: new Date().toISOString(),
    });
}
function saveCommunityTrends(trends) {
    writeJson(path.join(CELL_DIR, 'community', 'trends.json'), {
        trends,
        updatedAt: new Date().toISOString(),
    });
}
function saveCommunityRankings(rankings) {
    writeJson(path.join(CELL_DIR, 'community', 'rankings.json'), {
        rankings,
        updatedAt: new Date().toISOString(),
    });
}
function getCommunityPatterns() {
    return readJson(path.join(CELL_DIR, 'community', 'patterns.json'));
}
function getCommunityTrends() {
    return readJson(path.join(CELL_DIR, 'community', 'trends.json'));
}
function getCommunityRankings() {
    return readJson(path.join(CELL_DIR, 'community', 'rankings.json'));
}
// ─── Scan → Populate All Layers ──────────────────────────────────────────────
function populateAllLayers(scanResult) {
    // 1. Personal layer
    const p = scanResult.profile;
    savePersonalProfile({
        namingStyle: p.namingStyle || 'unknown',
        quoteStyle: p.quoteStyle || 'unknown',
        semicolonStyle: p.semicolonStyle || 'unknown',
        indentStyle: p.indentStyle || 'unknown',
        functionStyle: p.functionStyle || 'unknown',
        asyncStyle: p.asyncStyle || 'unknown',
        errorHandling: p.errorHandling || 'unknown',
        architectureStyle: p.architectureStyle || 'unknown',
        testPattern: p.testPattern || 'unknown',
        strengths: p.strengths || [],
        improvements: p.improvements || [],
        topPatterns: p.topPatterns || [],
    });
    savePersonalStyle(scanResult.patterns.filter((p) => p.category === 'style'));
    // 2. Team layer — extract team-relevant patterns
    const teamPatterns = scanResult.patterns
        .filter((p) => ['architecture', 'testing', 'error_handling'].includes(p.category))
        .map((p) => ({ name: p.category, value: p.value, count: p.count }));
    saveTeamPatterns(teamPatterns);
    const blindSpots = (p.improvements || []).map((i) => ({
        issue: i,
        severity: 'medium',
        count: 1,
    }));
    saveTeamBlindSpots(blindSpots);
    // 3. Community layer — extract community-relevant patterns
    const communityPatterns = scanResult.patterns.map((p) => ({
        category: p.category,
        description: `${p.value} (${p.count}x)`,
        popularity: p.confidence,
    }));
    saveCommunityPatterns(communityPatterns);
}
//# sourceMappingURL=layers.js.map