import { SHJDatasourceV2 as U } from "../index.mjs";
import { httpRequest as p } from "../utils/request.mjs";
import { DataSourceUtils as t } from "../utils/utils.mjs";
const q = (f, e, y) => new Promise(function(r, j) {
  const { api: n } = f.source;
  try {
    if (n) {
      let { isHeaders: m, isBody: h, headers: c, contentType: u, url: a, postParam: o } = n;
      const E = t.getEnvironments(), l = t.getVariableData(), i = (P) => {
        P.then((D) => {
          if (D.status === 200) {
            const { filter: V, mapping: g, isUserdata: T } = f.source;
            U.processData(V, D.data, g, e, f.source.dynamicMapping, y, T).then((b) => {
              b && r(b);
            }).catch(() => r(t.noneData(e)));
            return;
          }
          r(t.noneData(e));
        }).catch(() => {
          r(t.noneData(e));
        });
      }, s = t.replaceEnvVariables(a, E);
      a = t.replaceURLVariables(a, l), h && (o = t.replaceObjectVariables(o, l)), m && (c = t.replaceObjectVariables(c, l)), n.type === "GET" && i(p.get(s + a, { headers: c })), n.type === "POST" && i(p.post(s + a, o, { headers: c }, u)), n.type === "PUT" && i(p.put(s + a, o, { headers: c }, u)), n.type === "DELETE" && i(p.delete(s + a, { headers: c }));
    } else
      r(t.noneData(e));
  } catch {
    r(t.noneData(e));
  }
});
export {
  q as parseAPIPort
};
