import fs from "node:fs/promises";
import path from "node:path";
import { LISA_HOME } from "../paths.js";
import { pathExists } from "../fs-utils.js";
export const CHANNELS_CONFIG_PATH = path.join(LISA_HOME, "channels.json");
export async function loadChannelsConfig() {
    if (!(await pathExists(CHANNELS_CONFIG_PATH)))
        return { channels: {} };
    const raw = await fs.readFile(CHANNELS_CONFIG_PATH, "utf8");
    try {
        const parsed = JSON.parse(expandEnv(raw));
        return { channels: parsed.channels ?? {} };
    }
    catch (err) {
        throw new Error(`failed to parse ${CHANNELS_CONFIG_PATH}: ${err.message}`);
    }
}
/** Replace ${VAR_NAME} placeholders with process.env values. */
function expandEnv(raw) {
    return raw.replace(/\$\{([A-Z0-9_]+)\}/g, (_, name) => {
        const v = process.env[name];
        if (v === undefined)
            return "";
        return v.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
    });
}
//# sourceMappingURL=config.js.map