import Morph from 'morphmorph'

let createHistory
let history
if (typeof window !== 'undefined') {
  createHistory = require('history').createBrowserHistory
  history = createHistory()
}

const mapper = new Morph()

const mappings = [
  'bg:backgroundColor',
  't:theme',
  'l:language',
  'ds:dropShadow',
  'wc:windowControls',
  'wa:widthAdjustment',
  'pv:paddingVertical',
  'ph:paddingHorizontal',
  'ln:lineNumbers',
  'code:code'
]

const reverseMappings = mappings.map(field =>
  field
    .split(':')
    .reverse()
    .join(':')
)

const keysToQuery = keys =>
  `?${Object.keys(keys)
    .map(key => `${key}=${keys[key]}`)
    .join('&')}`

export const getQueryStringState = query => mapper.map(mappings, query)
export const updateQueryString = state =>
  history.replace({
    search: encodeURI(keysToQuery(mapper.map(reverseMappings, state)))
  })

@@ -206,16 +206,119 @@
 w Morph(
+%7B%0A  types: %7B%0A    bool: v =%3E %7B%0A      if (v === 'false') return false%0A      return Boolean(v)%0A    %7D%0A  %7D%0A%7D
 )%0A%0Aconst
@@ -332,16 +332,25 @@
 gs = %5B%0A 
+ %7B field:
  'bg:bac
@@ -362,19 +362,30 @@
 ndColor'
+ %7D
 ,%0A 
+ %7B field:
  't:them
@@ -386,19 +386,30 @@
 t:theme'
+ %7D
 ,%0A 
+ %7B field:
  'l:lang
@@ -413,19 +413,30 @@
 anguage'
+ %7D
 ,%0A 
+ %7B field:
  'ds:dro
@@ -448,57 +448,132 @@
 ow',
-%0A  'wc:windowControls',%0A  'wa:widthAdjustment',%0A 
+ type: 'bool' %7D,%0A  %7B field: 'wc:windowControls', type: 'bool' %7D,%0A  %7B field: 'wa:widthAdjustment', type: 'bool' %7D,%0A  %7B field:
  'pv
@@ -589,19 +589,30 @@
 ertical'
+ %7D
 ,%0A 
+ %7B field:
  'ph:pad
@@ -626,19 +626,30 @@
 izontal'
+ %7D
 ,%0A 
+ %7B field:
  'ln:lin
@@ -658,18 +658,43 @@
 umbers',
-%0A
  
+type: 'bool' %7D,%0A  %7B field:
  'code:c
@@ -697,16 +697,18 @@
 de:code'
+ %7D
 %0A%5D%0A%0Acons
@@ -744,19 +744,69 @@
 map(
-field =%3E%0A  
+mapping =%3E%0A  Object.assign(%7B%7D, mapping, %7B%0A    field: mapping.
 fiel
@@ -811,16 +811,18 @@
 eld%0A    
+ 
+ 
 .split('
@@ -829,16 +829,18 @@
 :')%0A    
+  
 .reverse
@@ -842,16 +842,18 @@
 verse()%0A
+  
     .joi
@@ -859,16 +859,21 @@
 in(':')%0A
+  %7D)%0A
 )%0A%0Aconst

Fix boolean params (#195)