import { BrowserHelper as ye, DesktopChecker as Ee } from "@accelbyte/sdk";
function q() {
  return q = Object.assign || function(e) {
    for (var n = 1; n < arguments.length; n++) {
      var r = arguments[n];
      for (var t in r)
        Object.prototype.hasOwnProperty.call(r, t) && (e[t] = r[t]);
    }
    return e;
  }, q.apply(this, arguments);
}
function X(e) {
  return e.charAt(0) === "/";
}
function ee(e, n) {
  for (var r = n, t = r + 1, i = e.length; t < i; r += 1, t += 1)
    e[r] = e[t];
  e.pop();
}
function Pe(e, n) {
  n === void 0 && (n = "");
  var r = e && e.split("/") || [], t = n && n.split("/") || [], i = e && X(e), g = n && X(n), w = i || g;
  if (e && X(e) ? t = r : r.length && (t.pop(), t = t.concat(r)), !t.length)
    return "/";
  var m;
  if (t.length) {
    var f = t[t.length - 1];
    m = f === "." || f === ".." || f === "";
  } else
    m = !1;
  for (var h = 0, v = t.length; v >= 0; v--) {
    var y = t[v];
    y === "." ? ee(t, v) : y === ".." ? (ee(t, v), h++) : h && (ee(t, v), h--);
  }
  if (!w)
    for (; h--; h)
      t.unshift("..");
  w && t[0] !== "" && (!t[0] || !X(t[0])) && t.unshift("");
  var N = t.join("/");
  return m && N.substr(-1) !== "/" && (N += "/"), N;
}
var Oe = process.env.NODE_ENV === "production";
function k(e, n) {
  if (!Oe) {
    if (e)
      return;
    var r = "Warning: " + n;
    typeof console < "u" && console.warn(r);
    try {
      throw Error(r);
    } catch {
    }
  }
}
var be = process.env.NODE_ENV === "production", ne = "Invariant failed";
function Z(e, n) {
  if (!e) {
    if (be)
      throw new Error(ne);
    var r = typeof n == "function" ? n() : n, t = r ? ne + ": " + r : ne;
    throw new Error(t);
  }
}
function J(e) {
  return e.charAt(0) === "/" ? e : "/" + e;
}
function ae(e) {
  return e.charAt(0) === "/" ? e.substr(1) : e;
}
function ie(e, n) {
  return e.toLowerCase().indexOf(n.toLowerCase()) === 0 && "/?#".indexOf(e.charAt(n.length)) !== -1;
}
function he(e, n) {
  return ie(e, n) ? e.substr(n.length) : e;
}
function le(e) {
  return e.charAt(e.length - 1) === "/" ? e.slice(0, -1) : e;
}
function xe(e) {
  var n = e || "/", r = "", t = "", i = n.indexOf("#");
  i !== -1 && (t = n.substr(i), n = n.substr(0, i));
  var g = n.indexOf("?");
  return g !== -1 && (r = n.substr(g), n = n.substr(0, g)), {
    pathname: n,
    search: r === "?" ? "" : r,
    hash: t === "#" ? "" : t
  };
}
function U(e) {
  var n = e.pathname, r = e.search, t = e.hash, i = n || "/";
  return r && r !== "?" && (i += r.charAt(0) === "?" ? r : "?" + r), t && t !== "#" && (i += t.charAt(0) === "#" ? t : "#" + t), i;
}
function M(e, n, r, t) {
  var i;
  typeof e == "string" ? (i = xe(e), i.state = n) : (i = q({}, e), i.pathname === void 0 && (i.pathname = ""), i.search ? i.search.charAt(0) !== "?" && (i.search = "?" + i.search) : i.search = "", i.hash ? i.hash.charAt(0) !== "#" && (i.hash = "#" + i.hash) : i.hash = "", n !== void 0 && i.state === void 0 && (i.state = n));
  try {
    i.pathname = decodeURI(i.pathname);
  } catch (g) {
    throw g instanceof URIError ? new URIError('Pathname "' + i.pathname + '" could not be decoded. This is likely caused by an invalid percent-encoding.') : g;
  }
  return r && (i.key = r), t ? i.pathname ? i.pathname.charAt(0) !== "/" && (i.pathname = Pe(i.pathname, t.pathname)) : i.pathname = t.pathname : i.pathname || (i.pathname = "/"), i;
}
function oe() {
  var e = null;
  function n(w) {
    return process.env.NODE_ENV !== "production" && k(e == null, "A history supports only one prompt at a time"), e = w, function() {
      e === w && (e = null);
    };
  }
  function r(w, m, f, h) {
    if (e != null) {
      var v = typeof e == "function" ? e(w, m) : e;
      typeof v == "string" ? typeof f == "function" ? f(v, h) : (process.env.NODE_ENV !== "production" && k(!1, "A history needs a getUserConfirmation function in order to use a prompt message"), h(!0)) : h(v !== !1);
    } else
      h(!0);
  }
  var t = [];
  function i(w) {
    var m = !0;
    function f() {
      m && w.apply(void 0, arguments);
    }
    return t.push(f), function() {
      m = !1, t = t.filter(function(h) {
        return h !== f;
      });
    };
  }
  function g() {
    for (var w = arguments.length, m = new Array(w), f = 0; f < w; f++)
      m[f] = arguments[f];
    t.forEach(function(h) {
      return h.apply(void 0, m);
    });
  }
  return {
    setPrompt: n,
    confirmTransitionTo: r,
    appendListener: i,
    notifyListeners: g
  };
}
var ve = !!(typeof window < "u" && window.document && window.document.createElement);
function pe(e, n) {
  n(window.confirm(e));
}
function He() {
  var e = window.navigator.userAgent;
  return (e.indexOf("Android 2.") !== -1 || e.indexOf("Android 4.0") !== -1) && e.indexOf("Mobile Safari") !== -1 && e.indexOf("Chrome") === -1 && e.indexOf("Windows Phone") === -1 ? !1 : window.history && "pushState" in window.history;
}
function Le() {
  return window.navigator.userAgent.indexOf("Trident") === -1;
}
function ke() {
  return window.navigator.userAgent.indexOf("Firefox") === -1;
}
function Ne(e) {
  return e.state === void 0 && navigator.userAgent.indexOf("CriOS") === -1;
}
var se = "popstate", ce = "hashchange";
function fe() {
  try {
    return window.history.state || {};
  } catch {
    return {};
  }
}
function Te(e) {
  e === void 0 && (e = {}), ve || (process.env.NODE_ENV !== "production" ? Z(!1, "Browser history needs a DOM") : Z(!1));
  var n = window.history, r = He(), t = !Le(), i = e, g = i.forceRefresh, w = g === void 0 ? !1 : g, m = i.getUserConfirmation, f = m === void 0 ? pe : m, h = i.keyLength, v = h === void 0 ? 6 : h, y = e.basename ? le(J(e.basename)) : "";
  function N(s) {
    var u = s || {}, o = u.key, a = u.state, d = window.location, p = d.pathname, C = d.search, x = d.hash, E = p + C + x;
    return process.env.NODE_ENV !== "production" && k(!y || ie(E, y), 'You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "' + E + '" to begin with "' + y + '".'), y && (E = he(E, y)), M(E, a, o);
  }
  function T() {
    return Math.random().toString(36).substr(2, v);
  }
  var H = oe();
  function D(s) {
    q(_, s), _.length = n.length, H.notifyListeners(_.location, _.action);
  }
  function V(s) {
    Ne(s) || F(N(s.state));
  }
  function R() {
    F(N(fe()));
  }
  var B = !1;
  function F(s) {
    if (B)
      B = !1, D();
    else {
      var u = "POP";
      H.confirmTransitionTo(s, u, f, function(o) {
        o ? D({
          action: u,
          location: s
        }) : K(s);
      });
    }
  }
  function K(s) {
    var u = _.location, o = A.indexOf(u.key);
    o === -1 && (o = 0);
    var a = A.indexOf(s.key);
    a === -1 && (a = 0);
    var d = o - a;
    d && (B = !0, O(d));
  }
  var $ = N(fe()), A = [$.key];
  function l(s) {
    return y + U(s);
  }
  function c(s, u) {
    process.env.NODE_ENV !== "production" && k(!(typeof s == "object" && s.state !== void 0 && u !== void 0), "You should avoid providing a 2nd state argument to push when the 1st argument is a location-like object that already has state; it is ignored");
    var o = "PUSH", a = M(s, u, T(), _.location);
    H.confirmTransitionTo(a, o, f, function(d) {
      if (d) {
        var p = l(a), C = a.key, x = a.state;
        if (r)
          if (n.pushState({
            key: C,
            state: x
          }, null, p), w)
            window.location.href = p;
          else {
            var E = A.indexOf(_.location.key), W = A.slice(0, E + 1);
            W.push(a.key), A = W, D({
              action: o,
              location: a
            });
          }
        else
          process.env.NODE_ENV !== "production" && k(x === void 0, "Browser history cannot push state in browsers that do not support HTML5 history"), window.location.href = p;
      }
    });
  }
  function P(s, u) {
    process.env.NODE_ENV !== "production" && k(!(typeof s == "object" && s.state !== void 0 && u !== void 0), "You should avoid providing a 2nd state argument to replace when the 1st argument is a location-like object that already has state; it is ignored");
    var o = "REPLACE", a = M(s, u, T(), _.location);
    H.confirmTransitionTo(a, o, f, function(d) {
      if (d) {
        var p = l(a), C = a.key, x = a.state;
        if (r)
          if (n.replaceState({
            key: C,
            state: x
          }, null, p), w)
            window.location.replace(p);
          else {
            var E = A.indexOf(_.location.key);
            E !== -1 && (A[E] = a.key), D({
              action: o,
              location: a
            });
          }
        else
          process.env.NODE_ENV !== "production" && k(x === void 0, "Browser history cannot replace state in browsers that do not support HTML5 history"), window.location.replace(p);
      }
    });
  }
  function O(s) {
    n.go(s);
  }
  function b() {
    O(-1);
  }
  function S() {
    O(1);
  }
  var j = 0;
  function I(s) {
    j += s, j === 1 && s === 1 ? (window.addEventListener(se, V), t && window.addEventListener(ce, R)) : j === 0 && (window.removeEventListener(se, V), t && window.removeEventListener(ce, R));
  }
  var L = !1;
  function Y(s) {
    s === void 0 && (s = !1);
    var u = H.setPrompt(s);
    return L || (I(1), L = !0), function() {
      return L && (L = !1, I(-1)), u();
    };
  }
  function G(s) {
    var u = H.appendListener(s);
    return I(1), function() {
      I(-1), u();
    };
  }
  var _ = {
    length: n.length,
    action: "POP",
    location: $,
    createHref: l,
    push: c,
    replace: P,
    go: O,
    goBack: b,
    goForward: S,
    block: Y,
    listen: G
  };
  return _;
}
var ue = "hashchange", Ae = {
  hashbang: {
    encodePath: function(n) {
      return n.charAt(0) === "!" ? n : "!/" + ae(n);
    },
    decodePath: function(n) {
      return n.charAt(0) === "!" ? n.substr(1) : n;
    }
  },
  noslash: {
    encodePath: ae,
    decodePath: J
  },
  slash: {
    encodePath: J,
    decodePath: J
  }
};
function ge(e) {
  var n = e.indexOf("#");
  return n === -1 ? e : e.slice(0, n);
}
function z() {
  var e = window.location.href, n = e.indexOf("#");
  return n === -1 ? "" : e.substring(n + 1);
}
function Se(e) {
  window.location.hash = e;
}
function te(e) {
  window.location.replace(ge(window.location.href) + "#" + e);
}
function _e(e) {
  e === void 0 && (e = {}), ve || (process.env.NODE_ENV !== "production" ? Z(!1, "Hash history needs a DOM") : Z(!1));
  var n = window.history, r = ke(), t = e, i = t.getUserConfirmation, g = i === void 0 ? pe : i, w = t.hashType, m = w === void 0 ? "slash" : w, f = e.basename ? le(J(e.basename)) : "", h = Ae[m], v = h.encodePath, y = h.decodePath;
  function N() {
    var o = y(z());
    return process.env.NODE_ENV !== "production" && k(!f || ie(o, f), 'You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "' + o + '" to begin with "' + f + '".'), f && (o = he(o, f)), M(o);
  }
  var T = oe();
  function H(o) {
    q(u, o), u.length = n.length, T.notifyListeners(u.location, u.action);
  }
  var D = !1, V = null;
  function R(o, a) {
    return o.pathname === a.pathname && o.search === a.search && o.hash === a.hash;
  }
  function B() {
    var o = z(), a = v(o);
    if (o !== a)
      te(a);
    else {
      var d = N(), p = u.location;
      if (!D && R(p, d) || V === U(d))
        return;
      V = null, F(d);
    }
  }
  function F(o) {
    if (D)
      D = !1, H();
    else {
      var a = "POP";
      T.confirmTransitionTo(o, a, g, function(d) {
        d ? H({
          action: a,
          location: o
        }) : K(o);
      });
    }
  }
  function K(o) {
    var a = u.location, d = c.lastIndexOf(U(a));
    d === -1 && (d = 0);
    var p = c.lastIndexOf(U(o));
    p === -1 && (p = 0);
    var C = d - p;
    C && (D = !0, S(C));
  }
  var $ = z(), A = v($);
  $ !== A && te(A);
  var l = N(), c = [U(l)];
  function P(o) {
    var a = document.querySelector("base"), d = "";
    return a && a.getAttribute("href") && (d = ge(window.location.href)), d + "#" + v(f + U(o));
  }
  function O(o, a) {
    process.env.NODE_ENV !== "production" && k(a === void 0, "Hash history cannot push state; it is ignored");
    var d = "PUSH", p = M(o, void 0, void 0, u.location);
    T.confirmTransitionTo(p, d, g, function(C) {
      if (C) {
        var x = U(p), E = v(f + x), W = z() !== E;
        if (W) {
          V = x, Se(E);
          var Q = c.lastIndexOf(U(u.location)), re = c.slice(0, Q + 1);
          re.push(x), c = re, H({
            action: d,
            location: p
          });
        } else
          process.env.NODE_ENV !== "production" && k(!1, "Hash history cannot PUSH the same path; a new entry will not be added to the history stack"), H();
      }
    });
  }
  function b(o, a) {
    process.env.NODE_ENV !== "production" && k(a === void 0, "Hash history cannot replace state; it is ignored");
    var d = "REPLACE", p = M(o, void 0, void 0, u.location);
    T.confirmTransitionTo(p, d, g, function(C) {
      if (C) {
        var x = U(p), E = v(f + x), W = z() !== E;
        W && (V = x, te(E));
        var Q = c.indexOf(U(u.location));
        Q !== -1 && (c[Q] = x), H({
          action: d,
          location: p
        });
      }
    });
  }
  function S(o) {
    process.env.NODE_ENV !== "production" && k(r, "Hash history go(n) causes a full page reload in this browser"), n.go(o);
  }
  function j() {
    S(-1);
  }
  function I() {
    S(1);
  }
  var L = 0;
  function Y(o) {
    L += o, L === 1 && o === 1 ? window.addEventListener(ue, B) : L === 0 && window.removeEventListener(ue, B);
  }
  var G = !1;
  function _(o) {
    o === void 0 && (o = !1);
    var a = T.setPrompt(o);
    return G || (Y(1), G = !0), function() {
      return G && (G = !1, Y(-1)), a();
    };
  }
  function s(o) {
    var a = T.appendListener(o);
    return Y(1), function() {
      Y(-1), a();
    };
  }
  var u = {
    length: n.length,
    action: "POP",
    location: l,
    createHref: P,
    push: O,
    replace: b,
    go: S,
    goBack: j,
    goForward: I,
    block: _,
    listen: s
  };
  return u;
}
function de(e, n, r) {
  return Math.min(Math.max(e, n), r);
}
function Ce(e) {
  e === void 0 && (e = {});
  var n = e, r = n.getUserConfirmation, t = n.initialEntries, i = t === void 0 ? ["/"] : t, g = n.initialIndex, w = g === void 0 ? 0 : g, m = n.keyLength, f = m === void 0 ? 6 : m, h = oe();
  function v(c) {
    q(l, c), l.length = l.entries.length, h.notifyListeners(l.location, l.action);
  }
  function y() {
    return Math.random().toString(36).substr(2, f);
  }
  var N = de(w, 0, i.length - 1), T = i.map(function(c) {
    return typeof c == "string" ? M(c, void 0, y()) : M(c, void 0, c.key || y());
  }), H = U;
  function D(c, P) {
    process.env.NODE_ENV !== "production" && k(!(typeof c == "object" && c.state !== void 0 && P !== void 0), "You should avoid providing a 2nd state argument to push when the 1st argument is a location-like object that already has state; it is ignored");
    var O = "PUSH", b = M(c, P, y(), l.location);
    h.confirmTransitionTo(b, O, r, function(S) {
      if (S) {
        var j = l.index, I = j + 1, L = l.entries.slice(0);
        L.length > I ? L.splice(I, L.length - I, b) : L.push(b), v({
          action: O,
          location: b,
          index: I,
          entries: L
        });
      }
    });
  }
  function V(c, P) {
    process.env.NODE_ENV !== "production" && k(!(typeof c == "object" && c.state !== void 0 && P !== void 0), "You should avoid providing a 2nd state argument to replace when the 1st argument is a location-like object that already has state; it is ignored");
    var O = "REPLACE", b = M(c, P, y(), l.location);
    h.confirmTransitionTo(b, O, r, function(S) {
      S && (l.entries[l.index] = b, v({
        action: O,
        location: b
      }));
    });
  }
  function R(c) {
    var P = de(l.index + c, 0, l.entries.length - 1), O = "POP", b = l.entries[P];
    h.confirmTransitionTo(b, O, r, function(S) {
      S ? v({
        action: O,
        location: b,
        index: P
      }) : v();
    });
  }
  function B() {
    R(-1);
  }
  function F() {
    R(1);
  }
  function K(c) {
    var P = l.index + c;
    return P >= 0 && P < l.entries.length;
  }
  function $(c) {
    return c === void 0 && (c = !1), h.setPrompt(c);
  }
  function A(c) {
    return h.appendListener(c);
  }
  var l = {
    length: T.length,
    action: "POP",
    location: T[N],
    index: N,
    entries: T,
    createHref: H,
    push: D,
    replace: V,
    go: R,
    goBack: B,
    goForward: F,
    canGo: K,
    block: $,
    listen: A
  };
  return l;
}
const we = class {
};
let me = we;
me.isSSR = () => !ye.isOnBrowser();
me.createHistory = (e) => Ee.isDesktopApp() ? _e() : we.isSSR() ? Ce() : Te({
  basename: e
});
export {
  me as S
};
