"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stateManager = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const REPOBURG_DIR = path_1.default.join(os_1.default.homedir(), '.repoburg');
const STATE_FILE_PATH = path_1.default.join(REPOBURG_DIR, 'services.json');
class StateManager {
    constructor() {
        this.state = { services: [], activeBackendPort: null };
        this.ensureDirExists();
        this.loadState();
    }
    ensureDirExists() {
        try {
            fs_extra_1.default.ensureDirSync(REPOBURG_DIR);
        }
        catch (error) {
            console.error('Failed to create .repoburg directory:', error);
            process.exit(1);
        }
    }
    loadState() {
        try {
            if (fs_extra_1.default.existsSync(STATE_FILE_PATH)) {
                const fileContent = fs_extra_1.default.readFileSync(STATE_FILE_PATH, 'utf-8');
                const loadedState = JSON.parse(fileContent);
                this.state = {
                    services: [],
                    activeBackendPort: null,
                    ...loadedState,
                };
            }
            else {
                this.saveState(); // Create the file if it doesn't exist
            }
        }
        catch (error) {
            console.error('Error loading state from disk:', error);
            this.state = { services: [], activeBackendPort: null };
        }
    }
    saveState() {
        try {
            fs_extra_1.default.writeJsonSync(STATE_FILE_PATH, this.state, { spaces: 2 });
        }
        catch (error) {
            console.error('Error saving state to disk:', error);
        }
    }
    getState() {
        return { ...this.state };
    }
    getServices() {
        return [...this.state.services];
    }
    getService(name) {
        return this.state.services.find(s => s.name === name);
    }
    addService(service) {
        if (this.getService(service.name)) {
            throw new Error(`Service with name "${service.name}" already exists.`);
        }
        const newService = { ...service };
        this.state.services.push(newService);
        this.saveState();
        return newService;
    }
    removeService(name) {
        const serviceIndex = this.state.services.findIndex(s => s.name === name);
        if (serviceIndex === -1) {
            throw new Error(`Service "${name}" not found.`);
        }
        this.state.services.splice(serviceIndex, 1);
        this.saveState();
    }
    updateService(name, updates) {
        const service = this.getService(name);
        if (!service) {
            throw new Error(`Service "${name}" not found.`);
        }
        Object.assign(service, updates);
        this.saveState();
    }
    getActiveBackendPort() {
        return this.state.activeBackendPort;
    }
    setActiveBackendPort(port) {
        this.state.activeBackendPort = port;
        this.saveState();
    }
}
exports.stateManager = new StateManager();
