<script>
function hasPermission (roles, include) {
  if (include) {
    if (typeof include === 'string') {
      return roles.has(include)
    }
    return include.some(role => roles.has(role))
  }
  return false
}

export default {
  name: 'Permission',
  abstract: true,
  props: {
    skip: {
      type: [Boolean, String],
      default: false
    },
    condition: {
      type: Boolean,
      default: true
    },
    include: {
      type: [String, Array],
      default: null
    },
    access: {
      type: [String, Array],
      default: null
    }
  },
  computed: {
    allow () {
      const include = this.include
      const access = this.access
      if (include || access) {
        if (include && !hasPermission(this.roleSet, include)) {
          return false
        }
        if (access && !(Array.isArray(access) ? access.some(item => this.accessSet.has(item)) : this.accessSet.has(access))) {
          return false
        }
        return true
      }
      return false
    }
  },
  render () {
    if (this.skip || this.condition && this.allow) {
      return this.$slots.default
    }
    return null
  }
}
</script>
