var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
  __markAsModule(target);
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, desc) => {
  if (module2 && typeof module2 === "object" || typeof module2 === "function") {
    for (let key of __getOwnPropNames(module2))
      if (!__hasOwnProp.call(target, key) && key !== "default")
        __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  }
  return target;
};
var __toModule = (module2) => {
  return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};

// src/index.browser.ts
__export(exports, {
  REST: () => REST,
  default: () => index_browser_default
});

// src/REST.ts
var import_fetch = __toModule(require("@genee/fetch"));
var fetchingRequests = {};
var AbstractREST = class {
  constructor(baseUrl) {
    this._initializing = false;
    this.baseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
  }
  init() {
    return Promise.resolve();
  }
  initOnlyOnce() {
    if (!this._initPromise) {
      this._initPromise = this.init().then(() => {
        this._initializing = true;
      });
      this._initPromise.finally(() => {
        this._initializing = false;
      });
    }
    return this._initPromise;
  }
  rawRequest(method, path, body) {
    const url = this.url(path);
    const init = {
      method,
      headers: {
        ...this.additionalHeaders
      },
      body
    };
    if (this.hashsum === void 0) {
      return (0, import_fetch.default)(url, init);
    }
    const hash = this.hashsum(url, body);
    if (!(hash in fetchingRequests)) {
      fetchingRequests[hash] = (0, import_fetch.default)(url, init);
      fetchingRequests[hash].finally(() => {
        delete fetchingRequests[hash];
      });
    }
    return fetchingRequests[hash];
  }
  reset() {
    this._initPromise = null;
    this._initializing = false;
  }
  request(method, path, body) {
    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));
  }
  get(path) {
    return this.request("GET", path);
  }
  post(path, body) {
    return this.request("POST", path, body);
  }
  put(path, body) {
    return this.request("PUT", path, body);
  }
  patch(path, body) {
    return this.request("PATCH", path, body);
  }
  delete(path) {
    return this.request("DELETE", path);
  }
};

// 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;
