"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNginxBindingStorePath = getNginxBindingStorePath;
exports.loadPersistedNginxPath = loadPersistedNginxPath;
exports.savePersistedNginxPath = savePersistedNginxPath;
exports.clearPersistedNginxPath = clearPersistedNginxPath;
const promises_1 = require("fs/promises");
const path_1 = require("path");
const env_1 = require("../env");
const bindingStorePath = (0, path_1.join)(env_1.env.dataDir, 'nginx', 'binding.json');
function getNginxBindingStorePath() {
    return bindingStorePath;
}
async function loadPersistedNginxPath() {
    try {
        const raw = await (0, promises_1.readFile)(bindingStorePath, 'utf-8');
        const parsed = JSON.parse(raw);
        const persistedPath = parsed.path?.trim();
        return persistedPath || null;
    }
    catch {
        return null;
    }
}
async function savePersistedNginxPath(path) {
    const normalizedPath = path.trim();
    if (!normalizedPath) {
        return;
    }
    await (0, promises_1.mkdir)((0, path_1.dirname)(bindingStorePath), { recursive: true });
    const payload = {
        path: normalizedPath,
        updatedAt: new Date().toISOString(),
    };
    await (0, promises_1.writeFile)(bindingStorePath, JSON.stringify(payload, null, 2), 'utf-8');
}
async function clearPersistedNginxPath() {
    await (0, promises_1.rm)(bindingStorePath, { force: true });
}
