package expo.modules.contacts.next.domain.model.email import android.database.Cursor import android.provider.ContactsContract.CommonDataKinds.Email import expo.modules.contacts.next.domain.model.ExtractableField import expo.modules.contacts.next.domain.model.email.operations.ExistingEmail import expo.modules.contacts.next.domain.wrappers.DataId import expo.modules.contacts.next.extensions.getNullableInt import expo.modules.contacts.next.extensions.getNullableString import expo.modules.contacts.next.extensions.getRequiredString object EmailField : ExtractableField.Data { override val mimeType = Email.CONTENT_ITEM_TYPE override val projection = arrayOf( DataId.COLUMN_IN_DATA_TABLE, Email.ADDRESS, Email.TYPE, Email.LABEL ) override fun extract(cursor: Cursor): ExistingEmail = with(cursor) { return ExistingEmail( dataId = DataId(getRequiredString(getColumnIndexOrThrow(DataId.COLUMN_IN_DATA_TABLE))), address = getNullableString(getColumnIndexOrThrow(Email.ADDRESS)), label = extractLabel() ) } private fun Cursor.extractLabel(): EmailLabel = when (getNullableInt(Email.TYPE)) { Email.TYPE_HOME -> EmailLabel.Home Email.TYPE_WORK -> EmailLabel.Work Email.TYPE_OTHER -> EmailLabel.Other Email.TYPE_MOBILE -> EmailLabel.Mobile null -> { val customLabel = getNullableString(getColumnIndexOrThrow(Email.LABEL)) EmailLabel.MalformedType(customLabel) } else -> { val customLabel = getNullableString(getColumnIndexOrThrow(Email.LABEL)) customLabel?.let { EmailLabel.Custom(it) } ?: EmailLabel.MalformedCustom } } }