var f = Object.defineProperty;
var i = (o, e) => f(o, "name", { value: e, configurable: !0 });
import { errorFromJson as d } from "./error-json.js";
import { ConnectError as c } from "../connect-error.js";
import { Code as l } from "../code.js";
const A = 2;
function h(o) {
  const e = new c("invalid end stream", l.Unknown);
  let r;
  try {
    r = JSON.parse(typeof o == "string" ? o : new TextDecoder().decode(o));
  } catch {
    throw e;
  }
  if (typeof r != "object" || r == null || Array.isArray(r))
    throw e;
  const t = new Headers();
  if ("metadata" in r) {
    if (typeof r.metadata != "object" || r.metadata == null || Array.isArray(r.metadata))
      throw e;
    for (const [s, n] of Object.entries(r.metadata)) {
      if (!Array.isArray(n) || n.some((a) => typeof a != "string"))
        throw e;
      for (const a of n)
        t.append(s, a);
    }
  }
  const m = "error" in r && r.error != null ? d(r.error, t, e) : void 0;
  return { metadata: t, error: m };
}
i(h, "endStreamFromJson");
export {
  A as endStreamFlag,
  h as endStreamFromJson
};
