"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.HighScoreManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const MAX_ENTRIES_PER_GAME = 10;
class HighScoreManager {
    filePath;
    data = {};
    constructor() {
        const dir = path.join(os.homedir(), '.splitgame');
        this.filePath = path.join(dir, 'scores.json');
        this.ensureDir(dir);
        this.load();
    }
    getHighScore(gameName) {
        const entries = this.data[gameName];
        if (!entries || entries.length === 0)
            return 0;
        return entries[0].score;
    }
    getTopScores(gameName, limit = 5) {
        return (this.data[gameName] || []).slice(0, limit);
    }
    submit(gameName, score) {
        if (score < 0)
            return false;
        if (!this.data[gameName])
            this.data[gameName] = [];
        const entries = this.data[gameName];
        const isHighScore = entries.length === 0 || score > entries[0].score;
        entries.push({ score, date: new Date().toISOString() });
        entries.sort((a, b) => b.score - a.score);
        if (entries.length > MAX_ENTRIES_PER_GAME) {
            entries.length = MAX_ENTRIES_PER_GAME;
        }
        this.save();
        return isHighScore;
    }
    load() {
        try {
            const raw = fs.readFileSync(this.filePath, 'utf-8');
            this.data = JSON.parse(raw);
        }
        catch {
            this.data = {};
        }
    }
    save() {
        try {
            fs.writeFileSync(this.filePath, JSON.stringify(this.data, null, 2), 'utf-8');
        }
        catch {
            // Silently fail — not critical
        }
    }
    ensureDir(dir) {
        try {
            fs.mkdirSync(dir, { recursive: true });
        }
        catch {
            // Already exists or can't create
        }
    }
}
exports.HighScoreManager = HighScoreManager;
//# sourceMappingURL=high-scores.js.map