"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.ConfigManager = exports.CONFIG_KEYS = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const VALID_TOGGLE_KEYS = ['f12', 'ctrl+]'];
const VALID_MODIFIER_KEYS = ['ctrl', 'alt'];
const MIN_GAME_WIDTH = 20;
const MAX_GAME_WIDTH = 80;
const MIN_SCROLLBACK = 100;
const MAX_SCROLLBACK = 100000;
const DEFAULTS = {
    toggleKey: 'f12',
    modifierKey: 'ctrl',
    gameWidthPercent: 50,
    scrollbackLines: 1000,
};
exports.CONFIG_KEYS = ['toggleKey', 'modifierKey', 'gameWidthPercent', 'scrollbackLines'];
class ConfigManager {
    filePath;
    config;
    constructor() {
        const dir = path.join(os.homedir(), '.splitgame');
        this.filePath = path.join(dir, 'config.json');
        this.ensureDir(dir);
        this.config = this.load();
    }
    get(key) {
        return this.config[key];
    }
    set(key, value) {
        this.validate(key, value);
        this.config[key] = value;
        this.save();
    }
    getAll() {
        return { ...this.config };
    }
    reset() {
        this.config = { ...DEFAULTS };
        this.save();
    }
    resetKey(key) {
        this.config[key] = DEFAULTS[key];
        this.save();
    }
    static defaults() {
        return { ...DEFAULTS };
    }
    static validToggleKeys() {
        return [...VALID_TOGGLE_KEYS];
    }
    static validModifierKeys() {
        return [...VALID_MODIFIER_KEYS];
    }
    static minGameWidth() {
        return MIN_GAME_WIDTH;
    }
    static maxGameWidth() {
        return MAX_GAME_WIDTH;
    }
    static minScrollback() {
        return MIN_SCROLLBACK;
    }
    static maxScrollback() {
        return MAX_SCROLLBACK;
    }
    validate(key, value) {
        switch (key) {
            case 'toggleKey':
                if (!VALID_TOGGLE_KEYS.includes(value)) {
                    throw new Error(`Invalid toggleKey "${value}". Valid: ${VALID_TOGGLE_KEYS.join(', ')}`);
                }
                break;
            case 'modifierKey':
                if (!VALID_MODIFIER_KEYS.includes(value)) {
                    throw new Error(`Invalid modifierKey "${value}". Valid: ${VALID_MODIFIER_KEYS.join(', ')}`);
                }
                break;
            case 'gameWidthPercent': {
                const n = Number(value);
                if (!Number.isInteger(n) || n < MIN_GAME_WIDTH || n > MAX_GAME_WIDTH) {
                    throw new Error(`Invalid gameWidthPercent "${value}". Must be integer ${MIN_GAME_WIDTH}–${MAX_GAME_WIDTH}`);
                }
                break;
            }
            case 'scrollbackLines': {
                const n = Number(value);
                if (!Number.isInteger(n) || n < MIN_SCROLLBACK || n > MAX_SCROLLBACK) {
                    throw new Error(`Invalid scrollbackLines "${value}". Must be integer ${MIN_SCROLLBACK}–${MAX_SCROLLBACK}`);
                }
                break;
            }
            default:
                throw new Error(`Unknown config key: ${String(key)}`);
        }
    }
    load() {
        try {
            const raw = fs.readFileSync(this.filePath, 'utf-8');
            const parsed = JSON.parse(raw);
            return {
                toggleKey: VALID_TOGGLE_KEYS.includes(parsed.toggleKey) ? parsed.toggleKey : DEFAULTS.toggleKey,
                modifierKey: VALID_MODIFIER_KEYS.includes(parsed.modifierKey) ? parsed.modifierKey : DEFAULTS.modifierKey,
                gameWidthPercent: this.clampWidth(parsed.gameWidthPercent),
                scrollbackLines: this.clampScrollback(parsed.scrollbackLines),
            };
        }
        catch {
            return { ...DEFAULTS };
        }
    }
    clampWidth(val) {
        const n = Number(val);
        if (!Number.isInteger(n) || isNaN(n))
            return DEFAULTS.gameWidthPercent;
        return Math.max(MIN_GAME_WIDTH, Math.min(MAX_GAME_WIDTH, n));
    }
    clampScrollback(val) {
        const n = Number(val);
        if (!Number.isInteger(n) || isNaN(n))
            return DEFAULTS.scrollbackLines;
        return Math.max(MIN_SCROLLBACK, Math.min(MAX_SCROLLBACK, n));
    }
    save() {
        try {
            fs.writeFileSync(this.filePath, JSON.stringify(this.config, null, 2), 'utf-8');
        }
        catch (e) {
            throw new Error(`Could not save to ${this.filePath}: ${e.message}`);
        }
    }
    ensureDir(dir) {
        try {
            fs.mkdirSync(dir, { recursive: true });
        }
        catch {
            // Already exists
        }
    }
}
exports.ConfigManager = ConfigManager;
//# sourceMappingURL=config.js.map