// src/REST.ts
import fetch from "@genee/fetch";
var fetchingRequests = {};
var AbstractREST = class {
  baseUrl;
  constructor(baseUrl) {
    this.baseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
  }
  init() {
    return Promise.resolve();
  }
  _initPromise;
  _initializing = false;
  initOnlyOnce() {
    if (!this._initPromise) {
      this._initPromise = this.init().then(() => {
        this._initializing = true;
      });
      this._initPromise.finally(() => {
        this._initializing = false;
      });
    }
    return this._initPromise;
  }
  timeout = 5e3;
  hashsum;
  _cookie;
  get cookie() {
    return this._cookie;
  }
  rawRequest(method, path, body, init_) {
    const url = this.url(path);
    const init = {
      ...init_,
      method,
      headers: {
        ...init_?.headers,
        ...this.additionalHeaders
      },
      body,
      timeout: this.timeout,
      beforeRequest: (url2, init2) => {
        init_?.beforeRequest?.(url2, init2);
        if (this._cookie) {
          init2.headers = { ...init2.headers, cookie: this._cookie };
          init2.credentials = "include";
        }
      },
      customParser: (r, defaultParser) => {
        const setCookie = r.headers.get("set-cookie");
        if (setCookie) {
          this._cookie = setCookie.replace(/expires=(.+?);\s/gi, "").replace(/path=\/(,?)(\s?)/gi, "").trim();
        }
        return defaultParser(r);
      }
    };
    if (this.hashsum === void 0) {
      return fetch(url, init);
    }
    const hash = this.hashsum(url, body);
    if (!(hash in fetchingRequests)) {
      fetchingRequests[hash] = fetch(url, init);
      fetchingRequests[hash].finally(() => {
        delete fetchingRequests[hash];
      });
    }
    return fetchingRequests[hash];
  }
  reset() {
    this._initPromise = null;
    this._initializing = false;
  }
  request(method, path, body, init) {
    if (this._initializing) {
      throw new Error("you cannot call request inside init function, use rawRequest instead!");
    }
    return this.initOnlyOnce().then(() => this.rawRequest(method, path, body, init));
  }
  get(path, init) {
    return this.request("GET", path, init);
  }
  post(path, body, init) {
    return this.request("POST", path, body, init);
  }
  put(path, body, init) {
    return this.request("PUT", path, body, init);
  }
  patch(path, body, init) {
    return this.request("PATCH", path, body, init);
  }
  delete(path, init) {
    return this.request("DELETE", path, init);
  }
};

// src/index.browser.ts
var REST = class extends AbstractREST {
  url(path) {
    let u;
    if (typeof path !== "string") {
      u = new URL(path.path.replace(/^[\/.]+/, ""), this.baseUrl);
      if (typeof path.query == "string") {
        new URLSearchParams(path.query).forEach((v, k) => u.searchParams.append(k, v));
      } else {
        Object.keys(path.query).forEach((k) => {
          const v = path.query[k];
          return Array.isArray(v) ? v.map((vv) => u.searchParams.append(k, vv)) : u.searchParams.append(k, v);
        });
      }
    } else {
      u = new URL(path.replace(/^[\/.]+/, ""), this.baseUrl);
    }
    if (this.additionalQueries) {
      Object.keys(this.additionalQueries).forEach((k) => {
        const v = this.additionalQueries[k];
        Array.isArray(v) ? v.forEach((vv) => u.searchParams.append(k, vv)) : u.searchParams.append(k, v);
      });
    }
    return u.toString();
  }
};
var index_browser_default = REST;
export {
  REST,
  index_browser_default as default
};
