import { createEmptySpecification as w } from "../libs/openapi.js";
import { normalizeHttpMethod as m } from "@scalar/helpers/http/normalize-http-method";
const P = (s, i) => new Promise((e, b) => {
  try {
    return e(s ? k(s, i) : k(w()));
  } catch (p) {
    console.error("[@scalar/api-reference]", "Failed to parse the OpenAPI document. It might be invalid?"), console.error(p), b(p);
  }
  return e(k(w()));
}), k = (s, i) => {
  let e = {};
  s && typeof s == "object" ? e = s : e = w(), e.tags || (e.tags = []), e.paths || (e.paths = {}), e.components || (e.components = {}), e.webhooks || (e.webhooks = {}), e.servers || (e.servers = []), e.security || (e.security = []);
  const b = [], p = (o) => {
    var h;
    !("children" in o) || !((h = o.children) != null && h.length) || o.children.forEach((a) => {
      var f, u, t, n, g, d;
      let r = (f = e.tags) == null ? void 0 : f.findIndex((y) => "tag" in o && y.name === o.tag.name);
      r === -1 && "tag" in o && (e.tags.push({
        name: o.tag.name,
        operations: []
      }), r = e.tags.length - 1), (u = e.tags[r]).operations || (u.operations = []), "tag" in a ? p(a) : "operation" in a ? e.tags[r].operations.push({
        id: a.id,
        httpVerb: m(a.method),
        path: a.path,
        name: a.operation.summary || a.path || "",
        description: a.operation.description || "",
        isWebhook: !1,
        information: a.operation,
        pathParameters: (n = (t = e.paths) == null ? void 0 : t[a.path ?? ""]) == null ? void 0 : n.parameters
      }) : "webhook" in a && e.tags[r].operations.push({
        id: a.id,
        httpVerb: m(a.method),
        path: a.name,
        name: a.webhook.summary || a.name || "",
        description: a.webhook.description || "",
        isWebhook: !0,
        information: a.webhook,
        pathParameters: (d = (g = e.webhooks) == null ? void 0 : g[a.name ?? ""]) == null ? void 0 : d.parameters
      });
    });
  };
  return i == null || i.forEach((o) => {
    var h, a, r, f, u;
    if (o.id.startsWith("tag/default") && "operation" in o) {
      let t = (h = e.tags) == null ? void 0 : h.findIndex((n) => n.name === "default");
      t === -1 && (e.tags.push({
        name: "default",
        description: "",
        operations: []
      }), t = e.tags.length - 1), e.tags[t].operations.push({
        id: o.id,
        httpVerb: m(o.method),
        path: o.path,
        name: o.operation.summary || o.path || "",
        description: o.operation.description || "",
        isWebhook: !1,
        information: o.operation,
        pathParameters: (r = (a = e.paths) == null ? void 0 : a[o.path ?? ""]) == null ? void 0 : r.parameters
      });
    }
    "tag" in o && o.isGroup && ((f = o.children) != null && f.length) ? o.children.forEach(p) : "tag" in o && p(o), "isWebhooks" in o && o.isWebhooks && ((u = o.children) != null && u.length) && o.children.forEach((t) => {
      var n, g;
      "webhook" in t && t.name && t.method && b.push({
        id: t.id,
        httpVerb: m(t.method),
        path: t.name,
        name: t.webhook.summary || t.name || "",
        description: t.webhook.description || "",
        isWebhook: !0,
        information: t.webhook,
        pathParameters: (g = (n = e.webhooks) == null ? void 0 : n[t.name ?? ""]) == null ? void 0 : g.parameters
      });
    });
  }), {
    ...e,
    webhooks: b
  };
};
export {
  P as parse
};
