import Taro from '@tarojs/taro'
import store from '../store'

export const MESSAGE_TYPE = {
  NOTICE: 'notice',
  ACTIVITY: 'activity',
}

export function showSubscribeMessage (messageType) {
  const { user } = store.getState()
  const { miniApp = {} } = (user || {}).userInfo || {}

  const tmplIds = (miniApp.tpls || []).filter(x => x.tplType === messageType).map(x => x.tplId)
  if (tmplIds.length > 0) {
    return new Promise((resolve, reject) => {
      Taro.requestSubscribeMessage({ tmplIds, fail: (e) => { console.error(e); reject(e) }, success: resolve })
    })
  } else {
    return Promise.resolve()
  }
}
