class $ACL {
  constructor(AV, handler, request, that) {
    this.AV = AV
    this.$http = new AV.ACL()
    this.request = request
    this.that = that
    this.avObject = null
  }

  setPublicReadAccess(allowed) {
    this.$http.setPublicReadAccess(allowed)
    this.avObject = this.$http
    return this
  }

  setPublicWriteAccess(allowed) {
    this.$http.setPublicWriteAccess(allowed)
    this.avObject = this.$http
    return this
  }

  setReadAccess(user, allowed) {
    this.$http.setReadAccess(user, allowed)
    this.avObject = this.$http
    return this
  }

  setWriteAccess(user, allowed) {
    this.$http.setWriteAccess(user, allowed)
    this.avObject = this.$http
    return this
  }

  setRoleReadAccess(role, allowed) {
    this.$http.setRoleReadAccess(user, allowed)
    this.avObject = this.$http
    return this
  }

  setRoleWriteAccess(role, allowed) {
    this.$http.setRoleWriteAccess(role, allowed)
    this.avObject = this.$http
    return this
  }

  // 个人定制API
  createACL({ readUser, writeUser, readRole, writeRole }) {
    const { $http } = this
    readUser = readUser || true
    writeUser = writeUser || false
    if (readUser && readUser.length)
      readUser.forEach((item) => this.setReadAccess(item, true))
    else this.setPublicReadAccess(readUser)
    if (writeUser && writeUser.length)
      writeUser.forEach((item) => this.setWriteAccess(item, true))
    else this.setPublicWriteAccess(writeUser)
    if (readRole && readRole.length)
      readRole.forEach((item) => this.setRoleReadAccess(item, true))
    if (writeRole && writeRole.length)
      writeRole.forEach((item) => this.setRoleWriteAccess(item, true))
    this.avObject = $http
    return this
  }
}
export default $ACL
