import { keyCodes,keyNames } from './keyCodes'

var restoreFocus = null;
const keyBoardShortCut = (event,currentActiveEle,defaultAriaShortCut,customAriaShortCut)=>{
  let { space,enter } = keyCodes;
  let { space : spaceKey } = keyNames;
  
  if(currentActiveEle.dataset.ariaShortcut && customAriaShortCut){
    let splitEvent = currentActiveEle.dataset.ariaShortcut.split(',');
      splitEvent.forEach((item)=>{
          item && [item].forEach((key)=>{
              let [ eventTope,keyNames ] = key && key.split(':');
              let getKeyNames = keyNames && keyNames.split('-');
              getKeyNames && getKeyNames.forEach((keyName)=>{
                  if(keyName && eventTope && ((keyName.toLowerCase()) === (event.code.toLowerCase()) || parseInt(keyName) === event.keyCode)){
                      if((keyName.toLowerCase()) === spaceKey || parseInt(keyName) === space){ event.preventDefault() }
                      currentActiveEle[eventTope] && currentActiveEle[eventTope]();
                  }
              })
          })
      })
  }
  else{
    if(defaultAriaShortCut){
      let semanticEle = ['A','BUTTON','INPUT']
      if(event.keyCode === enter && semanticEle.indexOf(currentActiveEle.tagName) == -1){
        currentActiveEle['click'] && currentActiveEle.click();
      }
    }
  }
}

export default keyBoardShortCut;
