import _ from "lodash";
import Moment from "moment";
import queryString from 'query-string'

export const visualizationRouterBasePath = "/visualization"
export const visualizationRouterPath = visualizationRouterBasePath + '/:processId'

export function visualizationUrl(path, processName, nodeId, edgeId) {
  if (!_.isEmpty(nodeId) && !_.isEmpty(edgeId)) {
    throw new Error("cannot visualize both nodeId and edgeId")
  }
  const baseUrl = `${path}/${encodeURIComponent(processName)}`
  const nodeIdPart = nodeId ? this.nodeIdPart(nodeId) : ""
  const edgeIdPart = edgeId ? this.edgeIdPart(edgeId) : ""
  return baseUrl + nodeIdPart + edgeIdPart
}

export function nodeIdPart(nodeId) {
  return `?nodeId=${encodeURIComponent(nodeId)}`
}

export function edgeIdPart(edgeId) {
  return `?edgeId=${encodeURIComponent(edgeId)}`
}

export function extractVisualizationParams(search) {
  let queryParams = queryString.parse(search)
  const nodeId = queryParams.nodeId;
  const edgeId = queryParams.edgeId;
  return {nodeId, edgeId}
}

export function extractBusinessViewParams(queryParams) {
  if (queryParams.businessView) {
    return queryParams.businessView.toLowerCase() === "true"
  }

  return false
}

export function extractCountParams(queryParams) {
  if (queryParams.from || queryParams.to) {
    const from = queryParams.from ? fromTimestampOrDate(queryParams.from) : null;
    const to = queryParams.to ? fromTimestampOrDate(queryParams.to) : Moment();
    return {from, to};
  }

  return null
}

function fromTimestampOrDate(tsOrDate) {
  const asInt = parseInt(tsOrDate);
  if (Number.isInteger(asInt) && !isNaN(tsOrDate))
    return Moment(asInt);
  else
    return Moment(tsOrDate);
}

export function setAndPreserveLocationParams(params){
  let queryParams = queryString.parse(location.search)
  let resultParams = _.omitBy(Object.assign({}, queryParams, params), (e) => {
    return e === false || e == null || e === ""
  })

  return queryString.stringify(resultParams)
}
@@ -490,24 +490,27 @@
 const nodeId
+Url
 Part = nodeI
@@ -513,21 +513,16 @@
 odeId ? 
-this.
 nodeIdPa
@@ -547,24 +547,27 @@
 const edgeId
+Url
 Part = edgeI
@@ -574,13 +574,8 @@
 d ? 
-this.
 edge
@@ -615,24 +615,27 @@
 Url + nodeId
+Url
 Part + edgeI
@@ -631,24 +631,27 @@
 art + edgeId
+Url
 Part%0A%7D%0A%0Aexpo

Fix invalid URL in component search part 2 (#83)