appId: ${APP_ID}
env:
  # First image is selected by default on Android
  ANDROID_INDEX: ${ANDROID_INDEX || '0'}
  IOS_POINT: ${IOS_POINT || 'auto'}
  ASSET_NAME: ${ASSET_NAME}
  SECOND_ANDROID_INDEX: ${SECOND_ANDROID_INDEX || 'none'}
  SECOND_IOS_POINT: ${SECOND_IOS_POINT || 'none'}
  SECOND_ASSET_NAME: ${SECOND_ASSET_NAME || 'none'}
---
# iOS image picker flow
- runFlow:
    when:
      platform: ios
    commands:
      - assertVisible: Recents
      - tapOn: Recents
      - assertVisible: Recents
      # If IOS_POINT is 'auto', pick the first image (below Recents)
      - runFlow:
          when:
            true: ${IOS_POINT == 'auto'}
          commands:
            - tapOn:
                below: .*Recents.*
      # In iOS the new images are added at the bottom of the list
      # so we need to tap on specific point to select the last image
      - runFlow:
          when:
            true: ${IOS_POINT != 'auto'}
          commands:
            - tapOn:
                point: ${IOS_POINT}
      - runFlow:
          when:
            true: ${SECOND_IOS_POINT != 'none'}
          commands:
            - tapOn:
                point: ${SECOND_IOS_POINT}
      - tapOn: Done
# Android image picker flow
- runFlow:
    when:
      platform: android
    commands:
      - waitForAnimationToEnd:
          timeout: 2000
      # If the image picker is opened from the cloud media app, we need to navigate up to the main app
      - runFlow:
          when:
            visible: Dismiss
          commands:
            - assertVisible: Dismiss
            - tapOn: Dismiss
            - waitForAnimationToEnd:
                timeout: 1000
      # On Android, images are usually shown directly in a grid
      # Newer Android photo pickers may expose only accessibility labels like
      # "Photo taken on ..." instead of stable thumbnail IDs.
      - runFlow:
          when:
            visible: .*${ASSET_NAME}.*
          commands:
            - tapOn: .*${ASSET_NAME}.*
      - runFlow:
          when:
            notVisible: .*${ASSET_NAME}.*
          commands:
            - runFlow:
                when:
                  visible: .*Photo taken on.*
                commands:
                  - tapOn:
                      text: .*Photo taken on.*
                      index: ${ANDROID_INDEX}
            - runFlow:
                when:
                  notVisible: .*Photo taken on.*
                commands:
                  - tapOn:
                      id: 'com.google.android.providers.media.module:id/icon_thumbnail'
                      index: ${ANDROID_INDEX}
      # Wait for selection to register
      - waitForAnimationToEnd:
          timeout: 1000
      - runFlow:
          when:
            true: ${SECOND_ANDROID_INDEX != 'none'}
          commands:
            - runFlow:
                when:
                  visible: .*Photo taken on.*
                commands:
                  - tapOn:
                      text: .*Photo taken on.*
                      index: ${SECOND_ANDROID_INDEX}
            - runFlow:
                when:
                  notVisible: .*Photo taken on.*
                commands:
                  - tapOn:
                      id: 'com.google.android.providers.media.module:id/icon_thumbnail'
                      index: ${SECOND_ANDROID_INDEX}
      # Wait for selection to register
      - waitForAnimationToEnd:
          timeout: 1000
      # Android pickers may use different button labels
      # Most common: "Add" button (e.g., "Add (1)")
      - tapOn:
          text: .*Add \(\d{1,2}\).*
          optional: true
      - tapOn:
          text: Done
          optional: true
      - tapOn:
          text: Select
          optional: true
      - tapOn:
          text: OK
          optional: true
      # Wait for image picker to close and return to chat
      - waitForAnimationToEnd:
          timeout: 2000
