var AuthoritySample = {
  authorityKey: '',
  authorityName: '',
  authorityNameEn: '',
  fatherauthorityKey: '',
  editFlag: -1
}

class AuthorityManagerImplement {
  //根据key值获取权限
  findAuthorityByKey(AuthorityCollection, AuthorityKey) {
    return AuthorityCollection[AuthorityKey]
  }

  //获取权限的editFlag
  getEditFlag(AuthorityCollection) {
    let editFlagList = {}
    for (var authorityKey in AuthorityCollection) {
      let authority = this.findAuthorityByKey(AuthorityCollection, authorityKey);
      editFlagList[authorityKey] = authority.editFlag;
      if (authority.children !== null) {
        if (authority.children.length !== 0){
          for (var i = 0; i < authority.children.length; i++) {
            editFlagList[authority.children[i].authorityKey] = authority.children[i].editFlag
          }
        }
      }
    }
    return editFlagList
  }

  AuthorityCollectionToList (AuthorityCollection) {
    let authorityList = this.authorityAsArray(AuthorityCollection)
    return authorityList
  }

  authorityAsArray (AuthorityCollection) {
    return Object.values(AuthorityCollection)
  }
}

let implementMethods = new AuthorityManagerImplement()

function AuthorityManager (AuthorityCollection) {
  // 根据key值获取权限
  function findAuthorityByKey (AuthorityKey) {
    return implementMethods.findAuthorityByKey(AuthorityCollection, AuthorityKey)
  }

  // 获取权限的editFlag
  function getEditFlag () {
    return implementMethods.getEditFlag(AuthorityCollection)
  }

  // 将结构体转成List
  function AuthorityCollectionToList () {
    return implementMethods.AuthorityCollectionToList(AuthorityCollection)
  }

  function asArray () {
    return implementMethods.authorityAsArray(AuthorityCollection)
  }

  return Object.freeze(Object.assign(
    {
      // 在此列出模型对象方法
      findAuthorityByKey,
      getEditFlag,
      AuthorityCollectionToList,
      asArray
    }
  ))
}

export default AuthorityManager
