import { isDefined as g } from "@scalar/helpers/array/is-defined";
import { isObject as h } from "@scalar/helpers/object/is-object";
import { replaceEnvVariables as u } from "@scalar/helpers/regex/replace-variables";
import { getResolvedRef as k } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { xScalarCookieSchema as m } from "@scalar/workspace-store/schemas/extensions/general/x-scalar-cookies";
import { coerceValue as S } from "@scalar/workspace-store/schemas/typebox-coerce";
import { deSerializeParameter as z } from "./de-serialize-parameter.js";
import { isParamDisabled as x } from "../../request-block/helpers/is-param-disabled.js";
import { getExample as v } from "./get-example.js";
import { serializeSimpleStyle as b, serializeFormStyleForCookies as j, serializeContentValue as C, serializeDeepObjectStyle as D, serializeSpaceDelimitedStyle as O, serializePipeDelimitedStyle as P, serializeFormStyle as $ } from "./serialize-parameter.js";
const d = (e, o) => "explode" in e && e.explode !== void 0 ? e.explode : o, G = (e = [], o = {}, l = "default") => {
  const t = {
    cookies: [],
    headers: {},
    pathVariables: {},
    urlParams: new URLSearchParams()
  };
  if (e.length === 0)
    return t;
  for (const c of e) {
    const r = k(c), n = v(r, l, void 0);
    if (!n || x(r, n))
      continue;
    const s = typeof n.value == "string" ? u(n.value, o) : n.value, i = z(s, r), a = u(r.name, o);
    switch (r.in) {
      case "header": {
        if (a.toLowerCase() === "content-type" && i === "multipart/form-data")
          break;
        const p = b(i, d(r, !1));
        if (!g(p))
          break;
        const y = String(p);
        t.headers[a] ? t.headers[a] = `${t.headers[a]},${y}` : t.headers[a] = y;
        break;
      }
      case "path": {
        const f = b(i, d(r, !1));
        t.pathVariables[a] = encodeURIComponent(String(f));
        break;
      }
      case "query": {
        R(r, a, i, t.urlParams);
        break;
      }
      case "cookie": {
        V(a, i, d(r, !0), t.cookies);
        break;
      }
    }
  }
  return t;
}, w = (e, o) => !("style" in e) || !e.style ? "form" : e.style === "deepObject" ? h(o) ? "deepObject" : "form" : e.style, R = (e, o, l, t) => {
  const c = "explode" in e && e.explode !== void 0 ? e.explode : !0, r = w(e, l);
  if ("content" in e && e.content) {
    const s = Object.keys(e.content)[0] ?? "application/json", i = C(l, s);
    t.set(o, i);
    return;
  }
  if (r === "deepObject" && c) {
    const s = D(o, l);
    for (const i of s)
      t.append(i.key, i.value);
    return;
  }
  if (r === "spaceDelimited") {
    const s = O(l), i = t.get(o);
    t.set(o, i ? `${i} ${s}` : s);
    return;
  }
  if (r === "pipeDelimited") {
    const s = P(l), i = t.get(o);
    t.set(o, i ? `${i}|${s}` : s);
    return;
  }
  const n = $(l, c);
  if (Array.isArray(n))
    for (const s of n) {
      const i = s.key || o;
      t.append(i, String(s.value));
    }
  else
    t.append(o, String(n));
}, V = (e, o, l, t) => {
  const c = j(o, l);
  if (Array.isArray(c))
    for (const r of c) {
      const n = r.key || e;
      t.push(
        S(m, {
          name: n,
          value: String(r.value),
          path: "/"
        })
      );
    }
  else
    t.push(
      S(m, {
        name: e,
        value: String(c),
        path: "/"
      })
    );
};
export {
  G as buildRequestParameters
};
