// Exposes vendored Open Design content to non-Claude-Code engines.
//
// CC-backed agents already see workspace/skills/open-design/ for free via
// `--add-dir workspace`. Other engines (Anthropic, Hermes, Codex) need a
// curated catalog injected into their system prompt so the model knows the
// resources exist and how to reference them.
//
// The strings returned here are short — slug + title + relative path — so the
// engine can fetch full contents on demand via the marketplace API or the
// orchestrator's file tools.

import { listOpenDesignSkills, listDesignSystems, listCraftNotes, listDesignTemplates, isVendored, getCommit } from './open-design.js';

const HEADER = '## Open Design corpus (vendored)\n';

function fmt(entries, label, limit) {
  if (!entries.length) return '';
  const lines = [`### ${label} (${entries.length})`];
  for (const e of entries.slice(0, limit)) {
    lines.push(`- \`${e.slug}\` — ${e.title} [${e.path}]`);
  }
  if (entries.length > limit) {
    lines.push(`- … and ${entries.length - limit} more (see workspace/skills/open-design/INDEX.md)`);
  }
  return lines.join('\n') + '\n';
}

/**
 * Build a short markdown catalog of the vendored Open Design resources, suitable
 * for prepending to an agent system prompt. Engines that already mount workspace/
 * (Claude Code) do not need this — call `isCorpusInjectable(engine)` first.
 *
 * @param {{ skillLimit?: number, systemLimit?: number, craftLimit?: number, templateLimit?: number }} [opts]
 */
export function buildOpenDesignContextBlock(opts = {}) {
  if (!isVendored()) return '';
  const skillLimit = opts.skillLimit ?? 40;
  const systemLimit = opts.systemLimit ?? 30;
  const craftLimit = opts.craftLimit ?? 12;
  const templateLimit = opts.templateLimit ?? 30;

  const skills = listOpenDesignSkills();
  const systems = listDesignSystems();
  const craft = listCraftNotes();
  const templates = listDesignTemplates();
  const commit = getCommit();

  let body = HEADER;
  body += `Vendored from nexu-io/open-design (commit ${commit ? commit.slice(0, 10) : 'pinned'}). Read-only. License: Apache-2.0.\n`;
  body += 'Reference these resources by their slug. To inspect contents, read the listed path under workspace/skills/open-design/.\n\n';
  body += fmt(craft, 'Craft notes', craftLimit);
  body += fmt(skills, 'Skills', skillLimit);
  body += fmt(systems, 'Design systems', systemLimit);
  body += fmt(templates, 'Design templates', templateLimit);
  return body.trim();
}

const ENGINES_WITH_NATIVE_FS_ACCESS = new Set(['claude-code']);

/**
 * Whether to inject the context block into the engine's system prompt.
 * CC-backed engines already see workspace/ natively — skip injection there.
 */
export function isCorpusInjectable(engine) {
  if (!engine) return true;
  return !ENGINES_WITH_NATIVE_FS_ACCESS.has(String(engine));
}

/**
 * Convenience: return a catalog block tailored to an engine. Returns empty
 * string when the engine has native access or the corpus is not vendored.
 */
export function openDesignContextFor(engine, opts = {}) {
  if (!isCorpusInjectable(engine)) return '';
  return buildOpenDesignContextBlock(opts);
}
