{"size": 1447, "ext": "js", "max_stars_repo_path": "webapp/src/lib/Location.js", "max_stars_repo_name": "EijiGard/marketplace", "max_stars_repo_stars_event_min_datetime": "2018-07-31T08:43:40.000Z", "max_stars_repo_stars_event_max_datetime": "2018-07-31T08:43:40.000Z", "max_issues_repo_path": "webapp/src/lib/Location.js", "max_issues_repo_name": "EijiGard/marketplace", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "webapp/src/lib/Location.js", "max_forks_repo_name": "EijiGard/marketplace", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.7213114754, "max_line_length": 69, "alphanum_fraction": 0.6116102281}
import queryString from 'query-string'
import { PUBLICATION_STATUS } from 'shared/publication'
import { PAGE_SIZE } from './Pagination'

export class Location {
  static buildUrl(origin, queryParams) {
    return `${origin}?${queryString.stringify(queryParams)}`
  }

  constructor(location = null) {
    if (location) {
      this.location = location
      this.query = queryString.parse(location.search)
    }
  }

  getPageFromRouter() {
    return this.getOptionFromRouter('page')
  }

  getOptionsFromRouter(optionNames) {
    return optionNames.reduce((result, option) => {
      result[option] = this.getOptionFromRouter(option)
      return result
    }, {})
  }

  getOptionFromRouter(optionName) {
    const { page, sort_by, sort_order } = this.query
    let value = null

    switch (optionName) {
      case 'limit':
        value = PAGE_SIZE
        break
      case 'offset':
        value = !isNaN(page) && page > 0 ? (page - 1) * PAGE_SIZE : 0
        break
      case 'sortBy':
        value = sort_by ? sort_by : 'created_at'
        break
      case 'sortOrder':
        value = sort_order ? sort_order : 'desc'
        break
      case 'page':
        value = +page || 1
        break
      case 'status':
        value = PUBLICATION_STATUS.open
        break
      default:
        value = null
    }
    return value
  }

  buildUrl(origin, queryParams = this.query) {
    return Location.buildUrl(origin, queryParams)
  }
}
