function ok(stdout) {
  return { stdout, stderr: "", exitCode: 0 };
}
function err(stderr, exitCode = 1) {
  return { stdout: "", stderr, exitCode };
}
function parseArgs(args) {
  const result = {
    url: null,
    outputFile: null,
    silent: false,
    help: false
  };
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg === "--help" || arg === "-h") {
      result.help = true;
    } else if (arg === "-s" || arg === "--silent") {
      result.silent = true;
    } else if (arg === "-o" || arg === "--output") {
      result.outputFile = args[++i] || null;
    } else if (!arg.startsWith("-")) {
      result.url = arg;
    }
  }
  return result;
}
const HELP_TEXT = `Usage: jina [options] <url>

Fetch a URL as markdown via r.jina.ai

Options:
  -o, --output <file>  Write output to file
  -s, --silent         Silent mode (no progress)
  -h, --help           Show this help
`;
async function runJinaCommand(args, _ctx, vfs) {
  const parsed = parseArgs(args);
  if (parsed.help) {
    return ok(HELP_TEXT);
  }
  if (!parsed.url) {
    return err("jina: missing URL argument\nUsage: jina [options] <url>\n");
  }
  let url = parsed.url;
  if (!/^https?:\/\//i.test(url)) {
    url = "https://" + url;
  }
  const jinaUrl = `https://r.jina.ai/${url}`;
  let responseBody;
  try {
    const resp = await fetch(jinaUrl);
    if (!resp.ok) {
      return err(`jina: HTTP error ${resp.status} from r.jina.ai
`);
    }
    responseBody = await resp.text();
  } catch (fetchErr) {
    return err(`jina: failed to fetch: ${fetchErr.message || fetchErr}
`);
  }
  if (parsed.outputFile) {
    try {
      vfs.writeFileSync(parsed.outputFile, responseBody);
      if (!parsed.silent) {
        return ok(`Saved to ${parsed.outputFile}
`);
      }
      return ok("");
    } catch (writeErr) {
      return err(`jina: failed to write output: ${writeErr.message || writeErr}
`);
    }
  }
  return ok(responseBody);
}
export {
  runJinaCommand
};
//# sourceMappingURL=jina-command-Df9MGHto.js.map
