import { SHJDatasourceV2 as k } from "../index.mjs";
import { httpRequest as o } from "../utils/request.mjs";
import { DataSourceUtils as n } from "../utils/utils.mjs";
const L = async (t, P, c) => {
  var d;
  const e = t == null ? void 0 : t.id, l = (d = t == null ? void 0 : t.source) == null ? void 0 : d.api, a = (m) => {
    c(n.noneData(m));
  };
  if (!l || !e)
    return a(e || "");
  const {
    isHeaders: U,
    isBody: u,
    headers: v,
    contentType: p,
    url: D,
    postParam: E,
    type: y,
    isUseEnv: T = !0
  } = l;
  try {
    const S = n.getEnvironments().envBaseUrl ?? "", V = T ? S : "", g = n.replaceStringVariables(D), i = U ? n.replaceObjectVariables(v) : {}, f = u ? n.replaceObjectVariables(E) : {}, s = V + g;
    if (!s)
      return a(e);
    let r;
    switch (y) {
      case "GET":
        r = o.get(s, { headers: i });
        break;
      case "POST":
        r = o.post(
          s,
          f,
          { headers: i },
          p
        );
        break;
      case "PUT":
        r = o.put(
          s,
          f,
          { headers: i },
          p
        );
        break;
      case "DELETE":
        r = o.delete(s, {
          headers: i
        });
        break;
      default:
        return a(e);
    }
    const b = await r;
    if (b.status === 200) {
      const h = await k.processData(
        t.source,
        b.data,
        e,
        P
      );
      h ? c(h) : a(e);
    } else
      a(e);
  } catch {
    a(e);
  }
};
export {
  o as httpRequest,
  L as parseAPIPort
};
