import { getHeaders } from "./get-headers.js";
import { parseAcceptHeader } from "./parse-accept-header.js";
export function respondTo(requestOrHeaders, handlers) {
  let headers = getHeaders(requestOrHeaders);
  let accept = headers.get("accept");
  if (!accept) return handlers.default();
  let types = parseAcceptHeader(accept);
  for (let { type, subtype } of types) {
    let handler = handlers[`${type}/${subtype}`];
    if (handler) return handler();
    handler = handlers[subtype];
    if (handler) return handler();
    handler = handlers[type];
    if (handler) return handler();
    continue;
  }
  return handlers.default();
}
