import {frontendPreferences as frontendPrefsConfig} from '../config'

export function getCookie(name){
  const begin = document.cookie.indexOf(name)
  if (begin === -1){
    return ''
  }
  const fromBegin = document.cookie.substr(begin)
  const tokenWithName = fromBegin.split(';')
  const tokenWithNameSplit = tokenWithName[0].split('=')
  const token = tokenWithNameSplit[1]
  return token.trim()
}

export function setCookie(name, value = '', expireDays, path) {
  const expiresDate = Date.now() + expireDays * 24 * 60 * 60 * 1000
  const expiresTime = new Date(expiresDate).toUTCString()
  //http://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain
  const cookie = `${name}=${value}; expires=${expiresTime}; path=${path}`
  return document.cookie = cookie
}

import moment from 'moment'

export function fromNowOrNow(date) {
  var now = moment(date)

  if (Math.abs(moment().diff(now)) < 1000) { // 1000 milliseconds
    return 'just now'
  }

  return now.fromNow()
}

import defaultUserpic50Path from 'assets/images/default-userpic-50.png'
import defaultUserpic75Path from 'assets/images/default-userpic-75.png'

const userDefaults = {
  profilePictureMediumUrl: defaultUserpic50Path,
  profilePictureLargeUrl: defaultUserpic75Path,
  frontendPreferences: {
    displayNames: {
      displayOption: 1,
      useYou: true
    }
  }
}

export function userParser(user) {
  // Profile pictures
  user.profilePictureMediumUrl = user.profilePictureMediumUrl || userDefaults.profilePictureMediumUrl
  user.profilePictureLargeUrl = user.profilePictureLargeUrl || userDefaults.profilePictureLargeUrl

  // Frontend preferences (only use this client's subtree)
  const prefSubTree = user.frontendPreferences && user.frontendPreferences[frontendPrefsConfig.clientId]
  user.frontendPreferences = _.merge(userDefaults.frontendPreferences, prefSubTree)

  return {...user}
}

export function postParser(post) {
  post.commentsDisabled = (post.commentsDisabled === '1')
  return {...post}
}

export function preventDefault(realFunction) {
  return (event) => {
    event.preventDefault()
    return realFunction && realFunction()
  }
}

export function confirmFirst(realFunction) {
  return () => {
    if (confirm('Are you sure?')) {
      return realFunction && realFunction()
    }
  }
}

export function getCurrentRouteName(router) {
  return router && router.routes[router.routes.length - 1].name
}

export function pluralForm(n, singular, plural = null, format = 'n w') {
  let w

  if (n == 1) {
    w = singular
  } else if (plural) {
    w = plural
  } else {
    w = singular + 's'
  }

  return format.replace('n', n).replace('w', w)
}

@@ -1398,16 +1398,45 @@
 user) %7B%0A
+  const newUser = %7B...user%7D%0A%0A
   // Pro
@@ -1451,17 +1451,20 @@
 tures%0A  
-u
+newU
 ser.prof
@@ -1556,17 +1556,20 @@
 umUrl%0A  
-u
+newU
 ser.prof
@@ -1823,17 +1823,20 @@
 ntId%5D%0A  
-u
+newU
 ser.fron
@@ -1861,16 +1861,20 @@
 _.merge(
+%7B%7D, 
 userDefa
@@ -1922,25 +1922,23 @@
  return 
-%7B...u
+newU
 ser
-%7D
 %0A%7D%0A%0Aexpo

Fix properties leaking in userParser()