import { REGEX as t } from "@scalar/oas-utils/helpers";
import { buildRequestSecurity as T } from "../../../../libs/send-request/build-request-security.js";
import { replaceTemplateVariables as a } from "../../../../libs/string-template.js";
import { convertToHarRequest as P } from "./convert-to-har-request.js";
const y = "YOUR_SECRET_TOKEN", h = ({
  operation: c,
  example: o,
  server: s,
  securitySchemes: m = [],
  environment: n
}) => {
  const u = n && Array.isArray(n) ? Object.fromEntries(n.map((e) => [e.key, e.value])) : n || {}, A = (o?.parameters?.path ?? []).reduce((e, r) => {
    if (r.enabled) {
      const l = a(r.value, u);
      e[r.key] = encodeURIComponent(l);
    }
    return e;
  }, {}), v = (() => {
    if (s?.url && (t.VARIABLES.test(s.url) || t.PATH.test(s.url))) {
      const e = Object.entries(s?.variables || {}).reduce(
        (r, [l, i]) => (A[l] ? r[l] = A[l] : i.default && (r[l] = a(i.default, u)), r),
        {}
      );
      return a(a(s.url, u), e);
    }
    return s?.url;
  })(), p = (() => {
    const e = c?.path ?? "/";
    return e && (t.VARIABLES.test(e) || t.PATH.test(e)) ? a(a(e, u), A) : e;
  })(), d = T(m, u, y), E = [
    ...(o?.parameters.headers ?? []).map((e) => ({
      ...e,
      value: t.VARIABLES.test(e.value) || t.PATH.test(e.value) ? a(e.value, u) : e.value
    })) ?? [],
    ...Object.entries(d.headers).map(([e, r]) => ({
      key: e,
      value: r,
      enabled: !0
    }))
  ], b = [
    ...(o?.parameters.cookies ?? []).map((e) => ({
      ...e,
      value: t.VARIABLES.test(e.value) || t.PATH.test(e.value) ? a(e.value, u) : e.value
    })) ?? [],
    ...d.cookies.map((e) => ({
      key: e.name,
      value: e.value,
      enabled: !0
    }))
  ], f = [
    ...(o?.parameters.query ?? []).map((e) => ({
      ...e,
      value: t.VARIABLES.test(e.value) || t.PATH.test(e.value) ? a(e.value, u) : e.value
    })) ?? [],
    ...Array.from(d.urlParams.entries()).map(([e, r]) => ({
      key: e,
      value: r,
      enabled: !0
    }))
  ], R = (() => {
    const e = o?.body;
    return e?.raw?.value && (t.VARIABLES.test(e.raw.value) || t.PATH.test(e.raw.value)) ? {
      ...e,
      raw: {
        ...e.raw,
        value: a(e.raw.value, u)
      }
    } : e;
  })();
  return P({
    baseUrl: v,
    method: c?.method ?? "get",
    path: p,
    body: R,
    cookies: b,
    headers: E,
    query: f
  });
};
export {
  h as getHarRequest
};
