import { PublicWidgetData as D } from "./index-public-widgets-data.store.js";
import H from "./index-_virtual19.js";
import { BaseStore as T } from "./index-base.store.js";
import { parseParams as l } from "./index-parse-params.js";
import { stores as q } from "./index-stores.js";
import { map as u } from "./index-map.js";
import { catchError as m } from "./index-catchError.js";
import { from as x } from "./index-from.js";
import { Observable as h } from "./index-Observable.js";
class F {
  request(e) {
    return x(H.request(e));
  }
}
const g = new F();
H.interceptors.response.use((a) => a, (a) => {
  var e, t, o, r;
  if (a.response.status && a.response.status == 500) {
    const n = [{
      info: {
        url: (e = a.config) == null ? void 0 : e.url,
        method: (t = a.config) == null ? void 0 : t.method,
        data: (r = (o = a.config) == null ? void 0 : o.data) == null ? void 0 : r.slice(0, 2e3)
      },
      href: window.location.href,
      host: window.location.host,
      pathname: window.location.pathname
    }];
    fetch(`https://${window.location.host}/rest_api/v1/log?type=error_api_logs`, {
      method: "PUT",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(n)
    });
  }
  return Promise.reject(a);
});
class J extends T {
  constructor() {
    super(), q.addStore(this);
  }
  getHeaders(e = !1, t = "") {
    const o = {};
    return o["Content-Type"] = e ? "application/x-www-form-urlencoded" : "application/json", t && (o.Authorization = `Bearer ${t}`), o;
  }
  buildFormData(e, t, o) {
    if (t && typeof t == "object" && !(t instanceof Date) && !(t instanceof File))
      Object.keys(t).forEach((r) => {
        this.buildFormData(e, t[r], o ? `${o}[${r}]` : r);
      });
    else {
      const r = t ?? "";
      e.append(o, r);
    }
  }
  jsonToFormData(e) {
    const t = new FormData();
    return this.buildFormData(t, e, null), t;
  }
  get(e, t, o) {
    const r = D.getInstance(), {
      host: n,
      token: s,
      locationId: i,
      widgetId: w
    } = r.get();
    n && (e.base = n);
    const d = {
      store: this,
      method: "GET",
      headers: this.getHeaders(!1, s),
      url: `${e.get()}${l(t)}`,
      signal: o
    };
    return g.request(d).pipe(u((c) => c.data ? c.data : c), m((c) => new h((p) => {
      p.error(c), p.next(c), p.complete();
    })));
  }
  post(e, t, o, r, n = "", s) {
    const i = n === "multipart/form-data" ? this.jsonToFormData(t) : t, w = D.getInstance(), {
      host: d,
      token: c,
      locationId: p,
      widgetId: $
    } = w.get();
    d && (e.base = d);
    const I = {
      method: "POST",
      headers: this.getHeaders(n === "multipart/form-data", s),
      data: i,
      url: `${e.get()}${l(o)}`,
      signal: r,
      onUploadProgress: (f) => {
      }
    };
    return g.request(I).pipe(u((f) => f.data), m((f) => new h((j) => {
      j.error(f), j.complete();
    })));
  }
  put(e, t, o, r) {
    const n = {
      method: "PUT",
      headers: this.getHeaders(),
      data: t,
      url: `${e.get()}${l(o)}`,
      signal: r
    };
    return g.request(n).pipe(u((s) => s.data), m((s) => new h((i) => {
      i.error(s), i.complete();
    })));
  }
  delete(e, t, o, r) {
    const n = {
      method: "DELETE",
      headers: this.getHeaders(),
      data: t,
      url: `${e.get()}${l(o)}`,
      signal: r
    };
    return g.request(n).pipe(u((s) => s.data), m((s) => new h((i) => {
      i.error(s), i.complete();
    })));
  }
  patch(e, t, o, r) {
    const n = D.getInstance(), {
      host: s,
      token: i,
      locationId: w,
      widgetId: d
    } = n.get();
    s && (e.base = s);
    const c = {
      method: "PATCH",
      headers: this.getHeaders(),
      data: t,
      url: `${e.get()}${l(o)}`,
      signal: r
    };
    return g.request(c).pipe(u((p) => p.data), m((p) => new h(($) => {
      $.error(p), $.complete();
    })));
  }
}
export {
  g as Axios,
  J as HttpStore
};
