import { ADDRESS_STATUS , ADDRESS_TYPE , MEMBER_PRIVATE , RECEIVE_ADDRESS } from '@proton/shared/lib/constants' ; import { Address , Member , UserModel , CachedOrganizationKey } from '@proton/shared/lib/interfaces' ; const { TYPE_ORIGINAL , TYPE_CUSTOM_DOMAIN , TYPE_PREMIUM , TYPE_EXTERNAL } = ADDRESS_TYPE ; const { READABLE } = MEMBER_PRIVATE ; export const getStatus = ( address , i ) => { const { Status , Receive , DomainID , HasKeys } = address ; const isActive = Status === ADDRESS_STATUS . STATUS_ENABLED && Receive === RECEIVE_ADDRESS . RECEIVE_YES ; const isDisabled = Status === ADDRESS_STATUS . STATUS_DISABLED ; const isOrphan = DomainID === null ; const isMissingKeys = ! HasKeys ; return { isDefault : i === 0 && ! isDisabled && isActive , isActive , isDisabled , isOrphan , isMissingKeys , } ; } ; export const getPermissions = ( { member , address : { ID , Status , HasKeys , Type , Priority } , addresses , user : { isAdmin } , organizationKey , addressIndex , } ) => { const isSpecialAddress = Type === TYPE_ORIGINAL || Type === TYPE_PREMIUM ; const isSelf = ! member || ! ! member . Self ; const isMemberReadable = member ?. Private === READABLE ; const isDefault = addressIndex === 0 ; const isEnabled = Status === ADDRESS_STATUS . STATUS_ENABLED ; const canGenerateMember = organizationKey && isAdmin && isMemberReadable ; const canMakeDefault = ! isDefault && isEnabled ; let canDisable = isEnabled && isAdmin && ! isSpecialAddress ; const isManagedUser = ! addresses . some ( ( address ) => address . Type === TYPE_ORIGINAL || address . Type === TYPE_EXTERNAL ) ; if ( isManagedUser ) { const hasOtherEnabledAddress = addresses . some ( ( otherAddress ) => otherAddress . ID !== ID && otherAddress . Status === ADDRESS_STATUS . STATUS_ENABLED ) ; if ( ! hasOtherEnabledAddress && isSelf ) { canDisable = false ; } if ( hasOtherEnabledAddress && Priority === 1 ) { canDisable = false ; } } return { canMakeDefault , canGenerate : ! HasKeys && ( canGenerateMember || isSelf ) , canDisable , canEnable : Status === ADDRESS_STATUS . STATUS_DISABLED && isAdmin && ! isSpecialAddress , canDelete : ( ( isEnabled && canDisable ) || ! isEnabled ) && Type === TYPE_CUSTOM_DOMAIN && isAdmin , canEdit : isSelf , } ; } ; const addressSort = ( a , b ) => { if ( a . Status === ADDRESS_STATUS . STATUS_DISABLED ) { return 1 ; } if ( b . Status === ADDRESS_STATUS . STATUS_DISABLED ) { return - 1 ; } return a . Order - b . Order ; } ; export const formatAddresses = ( addresses ? ) => { if ( Array . isArray ( addresses ) ) { return addresses . filter ( ( { Type } ) => Type !== ADDRESS_TYPE . TYPE_EXTERNAL ) . sort ( addressSort ) ; } return [ ] ; } ; export type AddressPermissions = ReturnType < typeof getPermissions > ; export type AddressStatuses = ReturnType < typeof getStatus > ;