/**
 * Local helpers for the github plugin — server-module bridge.
 * Mirrors the lazy-import pattern from plugins/publish/src/helpers.ts.
 */
// npm package:  dist/plugins/github/dist/helpers.js → ../../../server/
// Monorepo dev: plugins/github/dist/helpers.js     → ../../../packages/openwriter/dist/server/
const npmBase = new URL('../../../server/', import.meta.url).href;
const monoBase = new URL('../../../packages/openwriter/dist/server/', import.meta.url).href;
let _cached = null;
async function tryImport(base) {
    const [helpers, state, ws, markdown, schemaGate] = await Promise.all([
        import(base + 'helpers.js'),
        import(base + 'state.js'),
        import(base + 'ws.js'),
        import(base + 'markdown.js'),
        import(base + 'blog-schema-gate.js'),
    ]);
    return { helpers, state, ws, markdown, schemaGate };
}
export async function getServerModules() {
    if (_cached)
        return _cached;
    let helpers, state, ws, markdown, schemaGate;
    try {
        ({ helpers, state, ws, markdown, schemaGate } = await tryImport(npmBase));
    }
    catch {
        ({ helpers, state, ws, markdown, schemaGate } = await tryImport(monoBase));
    }
    _cached = {
        getDataDir: helpers.getDataDir,
        readConfig: helpers.readConfig,
        saveConfig: helpers.saveConfig,
        save: state.save,
        cancelDebouncedSave: state.cancelDebouncedSave,
        getDocument: state.getDocument,
        getTitle: state.getTitle,
        getMetadata: state.getMetadata,
        getDocId: state.getDocId,
        setMetadata: state.setMetadata,
        bumpDocVersion: state.bumpDocVersion,
        broadcastSyncStatus: ws.broadcastSyncStatus,
        broadcastMetadataChanged: ws.broadcastMetadataChanged,
        broadcastDocumentsChanged: ws.broadcastDocumentsChanged,
        broadcastToast: ws.broadcastToast,
        tiptapToMarkdown: markdown.tiptapToMarkdown,
        validateBlogFrontmatter: schemaGate.validateBlogFrontmatter,
    };
    return _cached;
}
const PLUGIN_NAME = '@openwriter/plugin-github';
export async function listBlogSites() {
    const srv = await getServerModules();
    const cfg = srv.readConfig() || {};
    const slot = cfg.plugins?.[PLUGIN_NAME];
    return slot?.blogSites || [];
}
// adr: adr/plugin-slot-nested-data.md
export async function writeBlogSites(sites) {
    const srv = await getServerModules();
    const cfg = srv.readConfig() || {};
    const plugins = { ...(cfg.plugins || {}) };
    const slot = { ...(plugins[PLUGIN_NAME] || { enabled: true, config: {} }) };
    slot.blogSites = sites;
    plugins[PLUGIN_NAME] = slot;
    srv.saveConfig({ plugins });
}
