import chalk from "chalk";
const SNIPPET_MAX_LENGTH = 200;
function stripAnsi(str) {
  return str.replace(/\u001b\[[0-9;]*m/g, "");
}
function truncate(text, max) {
  if (text.length <= max) return text;
  return text.slice(0, max - 3) + "...";
}
function domainFromUrl(url) {
  try {
    return new URL(url).hostname;
  } catch {
    return url;
  }
}
function pathFromUrl(url, baseUrl) {
  try {
    const u = new URL(url);
    if (baseUrl) {
      const base = new URL(baseUrl);
      if (u.hostname === base.hostname) {
        return u.pathname;
      }
    }
    return u.pathname;
  } catch {
    return url;
  }
}
function formatSearchResults(output) {
  const lines = [];
  const header = `Search: ${chalk.cyan(`"${output.query}"`)} (${output.results.length} results, ${output.total_time_ms}ms, engines: ${output.engines_used.join(", ")})`;
  lines.push(header);
  if (output.warning) {
    lines.push("");
    lines.push(chalk.yellow(`  Warning: ${output.warning}`));
  }
  if (output.error) {
    lines.push("");
    lines.push(chalk.red(`  Error: ${output.error}`));
    return lines.join("\n");
  }
  if (output.results.length === 0) {
    lines.push("");
    lines.push(chalk.dim("  No results found"));
    return lines.join("\n");
  }
  for (let i = 0; i < output.results.length; i++) {
    const r = output.results[i];
    const domain = domainFromUrl(r.url);
    const score = r.relevance_score.toFixed(2);
    lines.push("");
    lines.push(`  ${chalk.bold(`[${i + 1}]`)} ${chalk.white(r.title)} ${chalk.dim(`- ${domain}`)} ${chalk.green(`(score: ${score})`)}`);
    lines.push(`      ${chalk.dim(truncate(r.snippet, SNIPPET_MAX_LENGTH))}`);
  }
  return lines.join("\n");
}
function formatFetchResult(output) {
  const lines = [];
  lines.push(`Fetch: ${chalk.cyan(output.url)}`);
  if (output.error) {
    lines.push("");
    lines.push(chalk.red(`  Error: ${output.error}`));
    return lines.join("\n");
  }
  lines.push("");
  const markdownLines = output.markdown.split("\n");
  const preview = markdownLines.slice(0, 3).join("\n");
  const indented = preview.split("\n").map((l) => `  ${l}`).join("\n");
  lines.push(indented);
  lines.push("");
  lines.push(chalk.dim(`  [cached: ${output.cached}, ${output.markdown.length} chars]`));
  return lines.join("\n");
}
function formatCrawlResult(output, seedUrl) {
  const lines = [];
  lines.push(`Crawl: ${chalk.cyan(seedUrl)} (${output.crawled} pages crawled, ${output.total_found} found)`);
  if (output.error) {
    lines.push("");
    lines.push(chalk.red(`  Error: ${output.error}`));
    return lines.join("\n");
  }
  if (output.pages.length === 0) {
    lines.push("");
    lines.push(chalk.dim("  No pages crawled"));
    return lines.join("\n");
  }
  for (let i = 0; i < output.pages.length; i++) {
    const p = output.pages[i];
    const path = pathFromUrl(p.url, seedUrl);
    lines.push(`  ${chalk.bold(`[${i + 1}]`)} ${chalk.white(path)} ${chalk.dim(`(depth: ${p.depth}, ${p.markdown.length} chars)`)}`);
  }
  return lines.join("\n");
}
function formatMapResult(output, seedUrl) {
  const lines = [];
  lines.push(`Map: ${chalk.cyan(seedUrl)} (${output.urls.length} URLs found, sitemap: ${output.sitemap_found ? "yes" : "no"})`);
  if (output.error) {
    lines.push("");
    lines.push(chalk.red(`  Error: ${output.error}`));
    return lines.join("\n");
  }
  if (output.urls.length === 0) {
    lines.push("");
    lines.push(chalk.dim("  No URLs found"));
    return lines.join("\n");
  }
  for (let i = 0; i < output.urls.length; i++) {
    const path = pathFromUrl(output.urls[i], seedUrl);
    lines.push(`  ${chalk.bold(`[${i + 1}]`)} ${chalk.white(path)}`);
  }
  return lines.join("\n");
}
function formatExtractResult(output) {
  const lines = [];
  const sourceLabel = output.source_url ? ` ${chalk.cyan(output.source_url)}` : "";
  lines.push(`Extract:${sourceLabel} (mode: ${chalk.yellow(output.mode)})`);
  if (output.error) {
    lines.push("");
    lines.push(chalk.red(`  Error: ${output.error}`));
    return lines.join("\n");
  }
  lines.push("");
  if (output.mode === "tables" && Array.isArray(output.data)) {
    for (const table of output.data) {
      if (table.headers.length === 0) continue;
      const headerRow = "  | " + table.headers.join(" | ") + " |";
      const sepRow = "  | " + table.headers.map((h) => "-".repeat(Math.max(h.length, 4))).join(" | ") + " |";
      lines.push(headerRow);
      lines.push(sepRow);
      for (const row of table.rows) {
        const cells = table.headers.map((h) => row[h] ?? "");
        lines.push("  | " + cells.join(" | ") + "|");
      }
    }
  } else if (output.mode === "selector") {
    if (Array.isArray(output.data)) {
      for (const item of output.data) {
        lines.push(`  ${item}`);
      }
    } else {
      lines.push(`  ${String(output.data)}`);
    }
  } else if (output.mode === "metadata") {
    const meta = output.data;
    if (meta.title) lines.push(`  ${chalk.bold("Title:")} ${meta.title}`);
    if (meta.description) lines.push(`  ${chalk.bold("Description:")} ${meta.description}`);
    if (meta.author) lines.push(`  ${chalk.bold("Author:")} ${meta.author}`);
    if (meta.date) lines.push(`  ${chalk.bold("Date:")} ${meta.date}`);
    if (meta.keywords && meta.keywords.length > 0) {
      lines.push(`  ${chalk.bold("Keywords:")} ${meta.keywords.join(", ")}`);
    }
  } else if (output.mode === "schema") {
    const data = output.data;
    for (const [key, value] of Object.entries(data)) {
      lines.push(`  ${chalk.bold(key + ":")} ${String(value)}`);
    }
  }
  return lines.join("\n");
}
function formatCacheResult(output) {
  const lines = [];
  if (output.error) {
    lines.push(chalk.red(`Error: ${output.error}`));
    return lines.join("\n");
  }
  if (output.stats) {
    lines.push("Cache Statistics:");
    lines.push(`  ${chalk.bold("Total URLs:")}  ${output.stats.total_urls}`);
    lines.push(`  ${chalk.bold("Total Size:")}  ${output.stats.total_size_mb.toFixed(2)} MB`);
    lines.push(`  ${chalk.bold("Oldest:")}      ${output.stats.oldest}`);
    lines.push(`  ${chalk.bold("Newest:")}      ${output.stats.newest}`);
    return lines.join("\n");
  }
  if (output.cleared !== void 0) {
    lines.push(`${chalk.green(String(output.cleared))} cache entries cleared`);
    return lines.join("\n");
  }
  if (output.results) {
    if (output.results.length === 0) {
      lines.push(chalk.dim("No cached results found"));
      return lines.join("\n");
    }
    for (let i = 0; i < output.results.length; i++) {
      const r = output.results[i];
      lines.push(`  ${chalk.bold(`[${i + 1}]`)} ${chalk.cyan(r.url)}`);
      lines.push(`      ${chalk.white(r.title)} ${chalk.dim(`(cached ${r.fetched_at})`)}`);
    }
    return lines.join("\n");
  }
  return chalk.dim("No output");
}
function formatFindSimilarResult(output) {
  const lines = [];
  lines.push(`Find Similar: method=${chalk.yellow(output.method)}, cache=${output.cache_hits}, web=${output.search_hits}, embedding=${output.embedding_available ? "yes" : "no"} (${output.total_time_ms}ms)`);
  if (output.error) {
    lines.push("");
    lines.push(chalk.red(`  Error: ${output.error}`));
    return lines.join("\n");
  }
  if (output.results.length === 0) {
    lines.push("");
    lines.push(chalk.dim("  No similar results found"));
    return lines.join("\n");
  }
  for (let i = 0; i < output.results.length; i++) {
    const r = output.results[i];
    const domain = domainFromUrl(r.url);
    const score = r.relevance_score.toFixed(2);
    lines.push("");
    lines.push(`  ${chalk.bold(`[${i + 1}]`)} ${chalk.white(r.title)} ${chalk.dim(`- ${domain}`)} ${chalk.green(`(score: ${score}, ${r.source})`)}`);
  }
  return lines.join("\n");
}
function formatResearchResult(output) {
  const lines = [];
  lines.push(`Research: depth=${chalk.yellow(output.depth)}, ${output.sources.length} sources, ${output.citations.length} citations (${output.total_time_ms}ms)`);
  if (output.error) {
    lines.push("");
    lines.push(chalk.red(`  Error: ${output.error}`));
    return lines.join("\n");
  }
  if (output.sub_queries.length > 0) {
    lines.push("");
    lines.push(chalk.bold("  Sub-queries:"));
    for (const sq of output.sub_queries) {
      lines.push(`    - ${sq}`);
    }
  }
  if (output.report) {
    lines.push("");
    const preview = output.report.split("\n").slice(0, 10).map((l) => `  ${l}`).join("\n");
    lines.push(preview);
    if (output.report.split("\n").length > 10) {
      lines.push(chalk.dim(`  ... (${output.report.length} chars total)`));
    }
  }
  return lines.join("\n");
}
function formatAgentResult(output) {
  const lines = [];
  lines.push(`Agent: ${output.pages_fetched} pages fetched, ${output.steps.length} steps (${output.total_time_ms}ms)`);
  if (output.error) {
    lines.push("");
    lines.push(chalk.red(`  Error: ${output.error}`));
    return lines.join("\n");
  }
  if (output.steps.length > 0) {
    lines.push("");
    lines.push(chalk.bold("  Steps:"));
    for (const step of output.steps) {
      lines.push(`    ${chalk.yellow(step.action)} ${chalk.dim(`(${step.time_ms}ms)`)} ${step.detail}`);
    }
  }
  if (output.result) {
    lines.push("");
    const text = typeof output.result === "string" ? output.result : JSON.stringify(output.result, null, 2);
    const preview = text.split("\n").slice(0, 10).map((l) => `  ${l}`).join("\n");
    lines.push(preview);
    if (text.split("\n").length > 10) {
      lines.push(chalk.dim(`  ... (${text.length} chars total)`));
    }
  }
  return lines.join("\n");
}
function formatJson(data) {
  return JSON.stringify(data, null, 2);
}
export {
  formatAgentResult,
  formatCacheResult,
  formatCrawlResult,
  formatExtractResult,
  formatFetchResult,
  formatFindSimilarResult,
  formatJson,
  formatMapResult,
  formatResearchResult,
  formatSearchResults,
  stripAnsi
};
//# sourceMappingURL=formatters.js.map