import store from '@/store'

export default {
  methods: {

    /**
     * @param {String} value
     * @returns {Boolean}
     * @example see @/views/permission/directive.vue
     */
    checkPermission(value) {
      const adminRole = 'Administrator'
      // If the user has administrator rights,return true
      const roles = store.getters && store.getters.roles
      const hasAdminRole = roles.some(role => {
        return adminRole === role
      })
      if (hasAdminRole) {
        return true
      }
      if (value) {
        const permissions = store.getters && store.getters.permissions
        return permissions.some(permission => {
          return value === permission
        })
      } else {
        return false
      }
    },
    /**
     * @param {String} value
     * @returns {Boolean}
     * @example see @/views/permission/directive.vue
     */
    checkRole(value) {
      const adminRole = 'Administrator'
      const roles = store.getters && store.getters.roles
      const hasAdminRole = roles.some(role => {
        return adminRole === role
      })
      if (hasAdminRole) {
        return true
      }
      if (value) {
        return roles.some(role => {
          return value === role
        })
      } else {
        return false
      }
    }
  }
}
