import { observable, extendObservable } from 'mobx'
import keyMapConfig, { modifierKeysMap } from 'commands/keymaps'

const findKeyByValue = value => Object
    .keys(keyMapConfig)
    .reduce((p, v) => {
      p[keyMapConfig[v]] = v
      return p
    }, {})[value] || ''

const withModifierKeys = value => value.split('+')
    .map(e => modifierKeysMap[e] || e.toUpperCase()).join('')


function MenuScope (defaultMenuItems=[]) {

  class MenuItem {
    constructor (props) {
      let subItems
      if (Array.isArray(props.items)) {
        subItems = props.items.map(itemProps => new MenuItem(itemProps))
        delete props.items
      }
      extendObservable(this, props)

      if (subItems) extendObservable(this, { items: subItems })
      if (this.command) {
        extendObservable(this, {
          get shortcut () {
            return withModifierKeys(findKeyByValue(this.command))
          }
        })
      }
      return this
    }
  }

  const state = observable({
    items: defaultMenuItems.map(props => new MenuItem(props)),
  })

  return { state, MenuItem }
}

export default MenuScope

@@ -1,12 +1,38 @@
+import React from 'react'%0A
 import %7B obs
@@ -42,24 +42,31 @@
 able, extend
+Shallow
 Observable %7D
@@ -495,20 +495,19 @@
 ructor (
-pr
 op
+t
 s) %7B%0A   
@@ -513,68 +513,141 @@
     
-let subItems%0A      if (Array.isArray(props.items)) %7B%0A       
+const keys = Object.keys(opts)%0A      keys.forEach((keyName) =%3E %7B%0A        switch (keyName) %7B%0A          case 'items':%0A            const
  sub
@@ -654,20 +654,19 @@
 Items = 
-pr
 op
+t
 s.items.
@@ -673,20 +673,19 @@
 map(item
-Prop
+Opt
 s =%3E new
@@ -702,12 +702,11 @@
 item
-Prop
+Opt
 s))%0A
@@ -717,47 +717,32 @@
     
-delete props.items%0A      %7D%0A      extend
+    return extendShallow
 Obse
@@ -758,34 +758,39 @@
 is, 
-props)%0A%0A
+%7B%0A        
       i
-f (
+tems: 
 subItems
 ) ex
@@ -789,113 +789,197 @@
 tems
-) extendObservable(this, %7B items: subItems %7D)%0A      if (this.command) %7B%0A        extendObservable(this, %7B%0A
+,%0A              get itemsMap () %7B%0A                return this.items.reduce((acc, item) =%3E %7B%0A                  if (item.key) acc%5Bitem.key%5D = item%0A                %7D, %7B%7D)%0A              %7D,%0A    
     
@@ -998,24 +998,28 @@
 ortcut () %7B%0A
+    
             
@@ -1086,18 +1086,24 @@
     
-%7D%0A
+    %7D%0A  
         
 %7D)%0A 
@@ -1098,16 +1098,18 @@
         
+  
 %7D)%0A     
@@ -1105,25 +1105,277 @@
    %7D)%0A      
+   
-%7D
+ // case 'key':%0A          // case 'name':%0A          // case 'className':%0A          // case 'command':%0A          // case 'isDisabled':%0A          default:%0A            return extendShallowObservable(this, %7B %5BkeyName%5D: opts%5BkeyName%5D %7D)%0A        %7D%0A      %7D)
 %0A      retur
@@ -1432,16 +1432,40 @@
  items: 
+observable.shallowArray(
 defaultM
@@ -1477,20 +1477,19 @@
 ems.map(
-pr
 op
+t
 s =%3E new
@@ -1502,16 +1502,148 @@
 tem(
-props)),
+opts))),%0A    get itemsMap () %7B%0A      return this.items.reduce((acc, item) =%3E %7B%0A        if (item.key) acc%5Bitem.key%5D = item%0A      %7D, %7B%7D)%0A    %7D
 %0A  %7D

Revert ":bug: fix bug in 'commons/Menu/state'"