// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Js_dict from "/node_modules/@rescript/runtime/lib/es6/Js_dict.js";
import * as Core__Int from "/node_modules/@rescript/core/src/Core__Int.res.mjs";
import * as Belt_Array from "/node_modules/@rescript/runtime/lib/es6/Belt_Array.js";
import * as Core__Float from "/node_modules/@rescript/core/src/Core__Float.res.mjs";
import * as Core__Option from "/node_modules/@rescript/core/src/Core__Option.res.mjs";

function parse(queryString) {
  var dict = {};
  var str = queryString.startsWith("?") ? queryString.slice(1) : queryString;
  if (str.length === 0) {
    return dict;
  } else {
    str.split("&").forEach(function (pair) {
          var match = pair.split("=");
          var len = match.length;
          if (len >= 3) {
            return ;
          }
          switch (len) {
            case 0 :
                return ;
            case 1 :
                var key = match[0];
                var decodedKey = decodeURIComponent(key);
                dict[decodedKey] = "";
                return ;
            case 2 :
                var key$1 = match[0];
                var value = match[1];
                var decodedKey$1 = decodeURIComponent(key$1);
                var decodedValue = decodeURIComponent(value);
                dict[decodedKey$1] = decodedValue;
                return ;
            
          }
        });
    return dict;
  }
}

function parseMulti(queryString) {
  var dict = {};
  var str = queryString.startsWith("?") ? queryString.slice(1) : queryString;
  if (str.length > 0) {
    str.split("&").forEach(function (pair) {
          var match = pair.split("=");
          var len = match.length;
          if (len >= 3) {
            return ;
          }
          switch (len) {
            case 0 :
                return ;
            case 1 :
                var key = match[0];
                var decodedKey = decodeURIComponent(key);
                var existing = Core__Option.getWithDefault(Js_dict.get(dict, decodedKey), []);
                dict[decodedKey] = existing.concat([""]);
                return ;
            case 2 :
                var key$1 = match[0];
                var value = match[1];
                var decodedKey$1 = decodeURIComponent(key$1);
                var decodedValue = decodeURIComponent(value);
                var existing$1 = Core__Option.getWithDefault(Js_dict.get(dict, decodedKey$1), []);
                dict[decodedKey$1] = existing$1.concat([decodedValue]);
                return ;
            
          }
        });
  }
  return dict;
}

function build(params) {
  var pairs = Js_dict.entries(params);
  if (pairs.length === 0) {
    return "";
  }
  var encoded = pairs.map(function (param) {
        return encodeURIComponent(param[0]) + "=" + encodeURIComponent(param[1]);
      });
  return "?" + Belt_Array.joinWith(encoded, "&", (function (v) {
                return v;
              }));
}

function buildMulti(params) {
  var pairs = Js_dict.entries(params);
  if (pairs.length === 0) {
    return "";
  }
  var allPairs = pairs.flatMap(function (param) {
        var key = param[0];
        return param[1].map(function (value) {
                    return encodeURIComponent(key) + "=" + encodeURIComponent(value);
                  });
      });
  return "?" + Belt_Array.joinWith(allPairs, "&", (function (v) {
                return v;
              }));
}

var get = Js_dict.get;

function getOr(params, key, $$default) {
  return Core__Option.getWithDefault(Js_dict.get(params, key), $$default);
}

function getInt(params, key) {
  return Core__Option.flatMap(Js_dict.get(params, key), (function (s) {
                return Core__Int.fromString(s, undefined);
              }));
}

function getFloat(params, key) {
  return Core__Option.flatMap(Js_dict.get(params, key), Core__Float.fromString);
}

function getBool(params, key) {
  return Core__Option.map(Js_dict.get(params, key), (function (v) {
                var match = v.toLowerCase();
                switch (match) {
                  case "1" :
                  case "true" :
                  case "yes" :
                      return true;
                  default:
                    return false;
                }
              }));
}

function has(params, key) {
  return Core__Option.isSome(Js_dict.get(params, key));
}

function set(params, key, value) {
  var newDict = Js_dict.fromArray(Js_dict.entries(params));
  newDict[key] = value;
  return newDict;
}

function setMany(params, updates) {
  var newDict = Js_dict.fromArray(Js_dict.entries(params));
  updates.forEach(function (param) {
        newDict[param[0]] = param[1];
      });
  return newDict;
}

function remove(params, key) {
  return Js_dict.fromArray(Js_dict.entries(params).filter(function (param) {
                  return param[0] !== key;
                }));
}

function removeMany(params, keys) {
  return Js_dict.fromArray(Js_dict.entries(params).filter(function (param) {
                  return !keys.includes(param[0]);
                }));
}

function merge(base, overrides) {
  var newDict = Js_dict.fromArray(Js_dict.entries(base));
  Js_dict.entries(overrides).forEach(function (param) {
        newDict[param[0]] = param[1];
      });
  return newDict;
}

function parseComparison(params, keyAOpt, keyBOpt, keyViewOpt, param) {
  var keyA = keyAOpt !== undefined ? keyAOpt : "a";
  var keyB = keyBOpt !== undefined ? keyBOpt : "b";
  var keyView = keyViewOpt !== undefined ? keyViewOpt : "view";
  var itemA = Js_dict.get(params, keyA);
  var itemB = Js_dict.get(params, keyB);
  var view = Js_dict.get(params, keyView);
  var extra = removeMany(params, [
        keyA,
        keyB,
        keyView
      ]);
  return {
          itemA: itemA,
          itemB: itemB,
          view: view,
          extra: extra
        };
}

function buildComparison(state, keyAOpt, keyBOpt, keyViewOpt, param) {
  var keyA = keyAOpt !== undefined ? keyAOpt : "a";
  var keyB = keyBOpt !== undefined ? keyBOpt : "b";
  var keyView = keyViewOpt !== undefined ? keyViewOpt : "view";
  var params = state.extra;
  var a = state.itemA;
  var params$1 = a !== undefined ? set(params, keyA, a) : params;
  var b = state.itemB;
  var params$2 = b !== undefined ? set(params$1, keyB, b) : params$1;
  var v = state.view;
  if (v !== undefined) {
    return set(params$2, keyView, v);
  } else {
    return params$2;
  }
}

function parseSwotState(params) {
  return {
          mode: Js_dict.get(params, "mode"),
          quadrant: Js_dict.get(params, "q"),
          expanded: getBool(params, "expanded")
        };
}

function buildSwotState(state) {
  var params = {};
  var m = state.mode;
  var params$1 = m !== undefined ? set(params, "mode", m) : params;
  var q = state.quadrant;
  var params$2 = q !== undefined ? set(params$1, "q", q) : params$1;
  var match = state.expanded;
  if (match !== undefined) {
    if (match) {
      return set(params$2, "expanded", "1");
    } else {
      return set(params$2, "expanded", "0");
    }
  } else {
    return params$2;
  }
}

function current() {
  var match = typeof __unsafe_window === "undefined" ? undefined : __unsafe_window;
  if (match !== undefined) {
    return parse(window.location.search);
  } else {
    return {};
  }
}

function pushToUrl(params) {
  var match = typeof __unsafe_window === "undefined" ? undefined : __unsafe_window;
  if (match === undefined) {
    return ;
  }
  var query = build(params);
  var path = window.location.pathname;
  var newUrl = path + query;
  ((window.history.pushState({}, '', newUrl))(newUrl));
}

function replaceInUrl(params) {
  var match = typeof __unsafe_window === "undefined" ? undefined : __unsafe_window;
  if (match === undefined) {
    return ;
  }
  var query = build(params);
  var path = window.location.pathname;
  var newUrl = path + query;
  ((window.history.replaceState({}, '', newUrl))(newUrl));
}

export {
  parse ,
  parseMulti ,
  build ,
  buildMulti ,
  get ,
  getOr ,
  getInt ,
  getFloat ,
  getBool ,
  has ,
  set ,
  setMany ,
  remove ,
  removeMany ,
  merge ,
  parseComparison ,
  buildComparison ,
  parseSwotState ,
  buildSwotState ,
  current ,
  pushToUrl ,
  replaceInUrl ,
}
/* No side effect */
