import { color, wrap } from './terminal.js'; export function createCommandRouter(api) { const commands = new Map(); const add = command => commands.set(command.name, command); const page = name => { const item = api.config.pages?.[name]; if (!item) return `${color('red', `No page named “${name}”.`)}\nTry: pages`; return wrap((item.lines || []).join('\n')).join('\n'); }; add({ name: 'home', description: 'Return home', run: () => { const p = api.config.persona || {}; const status = api.ownerOnline() ? color('green', 'Owner is online') : color(api.config.theme?.muted || 'brightBlack', 'Owner is offline'); return `${color(api.config.theme?.accent || 'yellow', `Welcome to ${p.name || 'my portfolio'}`)}\n\n${wrap(p.bio || 'You are exploring a portfolio over SSH. Type “help” to get started.').join('\n')}\n\n${status}\n\nCommands: home · pages · open · chat · contact · clear · quit`; }}); add({ name: 'help', description: 'Show this help', run: ctx => { const actions = [...commands.values()].filter(c => !c.admin || api.isAdmin(ctx)).map(c => `${c.name.padEnd(18)} ${c.description}`); const pages = Object.entries(api.config.pages || {}).filter(([name]) => !commands.has(name)).map(([name, item]) => `${name.padEnd(18)} Open ${item.title || name}`); return [...actions, ...pages].join('\n'); }}); add({ name: 'pages', description: 'List available pages', run: () => Object.entries(api.config.pages || {}).map(([name, item]) => `${color(api.config.theme?.accent || 'yellow', name.padEnd(14))} ${item.title || ''}`).join('\n') || 'No pages configured.' }); add({ name: 'open', description: 'Open a page', run: (ctx, arg) => arg ? page(arg) : 'Usage: open ' }); add({ name: 'contact', description: 'Show contact details', run: () => `Email: ${api.config.persona?.email || 'not configured'}` }); add({ name: 'chat', description: 'Chat with the owner/visitors', run: (ctx, arg) => { if (!arg) return 'Usage: chat '; const author = api.isAdmin(ctx) ? `${api.config.persona?.name || 'Owner'} [owner]` : (ctx.username || 'visitor'); const line = `${author}: ${arg}`; api.broadcast(line); api.sendNotification(line); return api.ownerOnline() ? 'Message sent to the owner.' : 'Message posted; the owner is currently offline.'; }}); add({ name: 'clear', description: 'Clear the screen', run: () => '' }); add({ name: 'quit', description: 'Disconnect', run: ctx => { ctx.channel?.end(); return ''; } }); add({ name: 'reload', description: 'Reload configuration', admin: true, run: () => { api.reload(); return 'Configuration reloaded.'; } }); add({ name: 'theme', description: 'Set runtime primary color', admin: true, run: (ctx, arg) => { if (!arg) return 'Usage: theme '; api.config.theme.primary = arg; return `Primary color set to ${arg} (runtime only).`; } }); add({ name: 'announce', description: 'Broadcast an announcement', admin: true, run: (ctx, arg) => { if (!arg) return 'Usage: announce '; api.broadcast(`OWNER: ${arg}`); api.sendNotification(`OWNER: ${arg}`); return 'Announcement sent.'; } }); return { home: ctx => commands.get('home').run(ctx, ''), execute(ctx, input) { const [name, ...rest] = input.trim().split(/\s+/); const command = commands.get(name || 'home') || (api.config.pages?.[name] ? { run: () => page(name) } : null); if (!command || (command.admin && !api.isAdmin(ctx))) return `Unknown command: ${name}. Try “help”.`; return command.run(ctx, rest.join(' ')); } }; }