// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Js from "@rescript/runtime/lib/es6/Js.js";
import * as Belt_Option from "@rescript/runtime/lib/es6/Belt_Option.js";

function parse(url) {
  let match = url.split("#");
  let len = match.length;
  let match$1;
  if (len >= 3) {
    match$1 = [
      url,
      undefined
    ];
  } else {
    switch (len) {
      case 0 :
        match$1 = [
          url,
          undefined
        ];
        break;
      case 1 :
        let pq = match[0];
        match$1 = [
          pq,
          undefined
        ];
        break;
      case 2 :
        let pq$1 = match[0];
        let f = match[1];
        match$1 = [
          pq$1,
          f
        ];
        break;
    }
  }
  let pathAndQuery = match$1[0];
  let match$2 = pathAndQuery.split("?");
  let len$1 = match$2.length;
  let match$3;
  if (len$1 >= 3) {
    match$3 = [
      pathAndQuery,
      undefined
    ];
  } else {
    switch (len$1) {
      case 0 :
        match$3 = [
          pathAndQuery,
          undefined
        ];
        break;
      case 1 :
        let p = match$2[0];
        match$3 = [
          p,
          undefined
        ];
        break;
      case 2 :
        let p$1 = match$2[0];
        let q = match$2[1];
        match$3 = [
          p$1,
          q
        ];
        break;
    }
  }
  let path = match$3[0];
  let normalizedPath = path === "" ? "/" : (
      path.startsWith("/") ? (
          path.length > 1 && path.endsWith("/") ? path.slice(0, path.length - 1 | 0) : path
        ) : "/" + path
    );
  return {
    path: normalizedPath,
    query: match$3[1],
    fragment: match$1[1]
  };
}

function toString(param) {
  let queryPart = Belt_Option.mapWithDefault(param.query, "", q => "?" + q);
  let fragmentPart = Belt_Option.mapWithDefault(param.fragment, "", f => "#" + f);
  return param.path + queryPart + fragmentPart;
}

function current() {
  let match = Js.undefinedToOption(typeof __unsafe_window === "undefined" ? undefined : __unsafe_window);
  if (match !== undefined) {
    return parse((window.location.pathname + window.location.search + window.location.hash));
  } else {
    return {
      path: "/",
      query: undefined,
      fragment: undefined
    };
  }
}

export {
  parse,
  toString,
  current,
}
/* No side effect */
