"use strict";

import React from 'react';
import FileDownload from '../styled/UnsupportedType';
import CustomMessageImage from '../styled/MessageImage';
import CustomMessageVideo from '../styled/VideoMessage';
import AudioMessage from '../styled/AudioMessage';
import { ActivityIndicator, Image, StyleSheet, Text, View } from 'react-native';
import { deriveDisplayFilename, isLikelyAudio } from '../../helpers/mimeToExtension';
import { FileIcon, PlayIcon } from '../../assets/icons';
import { defaultMediaDims } from '../../helpers/mediaDimensions';
import { useChatSettingState } from '../../hooks/useChatSettingState';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const pendingImageDims = defaultMediaDims();
const getPendingStatusLabel = mimeType => {
  if (mimeType?.startsWith('image/')) {
    return 'Uploading image';
  }
  if (mimeType?.startsWith('video/')) {
    return 'Uploading video';
  }
  if (mimeType?.startsWith('audio/') || mimeType?.includes('application/octet-stream')) {
    return 'Uploading audio';
  }
  return 'Uploading file';
};
const PendingMediaMessage = ({
  fileName,
  mimeType,
  previewUri,
  isUser,
  size
}) => {
  const {
    config
  } = useChatSettingState();
  const statusLabel = getPendingStatusLabel(mimeType);
  const isImage = mimeType?.startsWith('image/');
  const isVideo = mimeType?.startsWith('video/');
  const isAudio = mimeType?.startsWith('audio/') || mimeType?.includes('application/octet-stream');
  const primaryColor = config?.colors?.primary || '#0A84FF';
  if (isImage && previewUri) {
    return /*#__PURE__*/_jsxs(View, {
      style: styles.pendingImageWrapper,
      children: [/*#__PURE__*/_jsx(Image, {
        source: {
          uri: previewUri
        },
        style: styles.pendingImage,
        resizeMode: "cover"
      }), /*#__PURE__*/_jsxs(View, {
        style: styles.pendingImageOverlay,
        children: [/*#__PURE__*/_jsx(ActivityIndicator, {
          size: "small",
          color: "#FFFFFF"
        }), /*#__PURE__*/_jsx(Text, {
          style: styles.pendingOverlayText,
          children: statusLabel
        })]
      })]
    });
  }
  if (isVideo) {
    return /*#__PURE__*/_jsxs(View, {
      style: styles.pendingVideoCard,
      children: [/*#__PURE__*/_jsxs(View, {
        style: styles.pendingVideoPreview,
        children: [/*#__PURE__*/_jsx(View, {
          style: styles.pendingVideoIcon,
          children: /*#__PURE__*/_jsx(PlayIcon, {
            width: 24,
            height: 24
          })
        }), /*#__PURE__*/_jsx(ActivityIndicator, {
          size: "small",
          color: "#5B6B8C"
        })]
      }), /*#__PURE__*/_jsx(Text, {
        style: styles.pendingVideoLabel,
        children: fileName
      }), /*#__PURE__*/_jsx(Text, {
        style: styles.pendingVideoStatus,
        children: statusLabel
      })]
    });
  }
  if (isAudio) {
    return /*#__PURE__*/_jsxs(View, {
      style: styles.pendingAudioCard,
      children: [/*#__PURE__*/_jsx(View, {
        style: [styles.pendingAudioButton, {
          backgroundColor: primaryColor
        }],
        children: /*#__PURE__*/_jsx(PlayIcon, {
          width: 18,
          height: 18,
          color: "#fff"
        })
      }), /*#__PURE__*/_jsxs(View, {
        style: styles.pendingAudioContent,
        children: [/*#__PURE__*/_jsx(View, {
          style: styles.pendingAudioTrack,
          children: /*#__PURE__*/_jsx(View, {
            style: [styles.pendingAudioTrackFill, {
              backgroundColor: primaryColor
            }]
          })
        }), /*#__PURE__*/_jsxs(View, {
          style: styles.pendingAudioMeta,
          children: [/*#__PURE__*/_jsx(Text, {
            style: styles.pendingAudioTime,
            children: "0:00"
          }), /*#__PURE__*/_jsxs(View, {
            style: styles.pendingAudioStatusPill,
            children: [/*#__PURE__*/_jsx(ActivityIndicator, {
              size: "small",
              color: "#5B6B8C"
            }), /*#__PURE__*/_jsx(Text, {
              style: styles.pendingAudioStatusText,
              children: statusLabel
            })]
          })]
        })]
      })]
    });
  }
  return /*#__PURE__*/_jsx(FileDownload, {
    fileURL: previewUri || '',
    fileName: fileName,
    mimetype: mimeType || 'application/octet-stream',
    size: size,
    isUser: isUser,
    pending: true,
    placeholderIcon: /*#__PURE__*/_jsx(FileIcon, {
      width: 36,
      height: 36
    })
  });
};
const MediaMessage = ({
  mimeType,
  location,
  messageText,
  message,
  isUser
}) => {
  if (mimeType) {
    const displayName = deriveDisplayFilename({
      fileName: message?.fileName,
      originalName: message?.originalName,
      url: location || messageText,
      mime: mimeType
    });
    const isAudioPayload = isLikelyAudio(mimeType, displayName, location, {
      duration: message?.duration,
      waveForm: message?.waveForm,
      originalName: message?.originalName
    });
    if (message?.pending) {
      return /*#__PURE__*/_jsx(PendingMediaMessage, {
        fileName: displayName,
        mimeType: mimeType,
        previewUri: messageText || location || '',
        isUser: isUser,
        size: message?.size
      });
    }
    switch (true) {
      case mimeType.startsWith('image/'):
        return /*#__PURE__*/_jsx(CustomMessageImage, {
          fileName: displayName,
          fileURL: location || messageText || '',
          mimetype: mimeType
        });
      case mimeType.startsWith('video/'):
        return /*#__PURE__*/_jsx(CustomMessageVideo, {
          fileName: displayName,
          fileURL: location || '',
          mimetype: mimeType
        });
      case mimeType.startsWith('audio/'):
        {
          return /*#__PURE__*/_jsx(AudioMessage, {
            src: location || '',
            mimeType: mimeType,
            fileName: displayName,
            originalName: message?.originalName,
            duration: message?.duration,
            waveForm: message?.waveForm
          });
        }
      case mimeType.includes('application/octet-stream'):
        {
          return /*#__PURE__*/_jsx(AudioMessage, {
            src: location || '',
            mimeType: mimeType,
            fileName: displayName,
            originalName: message?.originalName,
            duration: message?.duration,
            waveForm: message?.waveForm
          });
        }
      default:
        {
          // Some senders (notably the web app's voice-message uploader)
          // ship audio as `application/octet-stream` with no audio file
          // extension. `isLikelyAudio` recognises those via the URL path,
          // filename, or voice-message naming hints (voicemail-,
          // voice-note-, recording-, …) so we still render the AudioMessage
          // player instead of falling through to the generic FileDownload
          // card (where the voicemail rendered as a broken `.bin`
          // attachment). Customer-reported #9 voicemail fix.
          if (isAudioPayload) {
            return /*#__PURE__*/_jsx(AudioMessage, {
              src: location || '',
              mimeType: mimeType,
              fileName: displayName,
              originalName: message?.originalName,
              duration: message?.duration,
              waveForm: message?.waveForm
            });
          }
          return /*#__PURE__*/_jsx(FileDownload, {
            fileURL: location ? location : '',
            fileName: displayName,
            mimetype: mimeType,
            isUser: isUser,
            originalName: message?.originalName,
            duration: message?.duration,
            waveForm: message?.waveForm
          });
        }
    }
  }
  return /*#__PURE__*/_jsx(Text, {
    children: "Unsupported media type"
  });
};
export default MediaMessage;
const styles = StyleSheet.create({
  pendingImageWrapper: {
    width: pendingImageDims.width,
    height: pendingImageDims.height,
    borderRadius: 12,
    overflow: 'hidden',
    backgroundColor: '#EAF0FB'
  },
  pendingImage: {
    width: '100%',
    height: '100%'
  },
  pendingImageOverlay: {
    ...StyleSheet.absoluteFillObject,
    alignItems: 'center',
    justifyContent: 'center',
    gap: 8,
    backgroundColor: 'rgba(17, 24, 39, 0.28)'
  },
  pendingOverlayText: {
    color: '#FFFFFF',
    fontSize: 13,
    fontWeight: '600'
  },
  pendingVideoCard: {
    width: 220,
    padding: 12,
    borderRadius: 12,
    backgroundColor: '#F3F6FC',
    gap: 10
  },
  pendingVideoPreview: {
    height: 144,
    borderRadius: 10,
    backgroundColor: '#DCE6F7',
    alignItems: 'center',
    justifyContent: 'center',
    gap: 10
  },
  pendingVideoIcon: {
    width: 48,
    height: 48,
    borderRadius: 24,
    backgroundColor: 'rgba(17, 24, 39, 0.45)',
    alignItems: 'center',
    justifyContent: 'center'
  },
  pendingVideoLabel: {
    color: '#141414',
    fontSize: 14,
    fontWeight: '600'
  },
  pendingVideoStatus: {
    color: '#5B6B8C',
    fontSize: 12,
    fontWeight: '500'
  },
  pendingAudioCard: {
    minWidth: 252,
    maxWidth: 288,
    flexDirection: 'row',
    alignItems: 'center',
    gap: 12,
    paddingHorizontal: 12,
    paddingVertical: 10,
    borderRadius: 12,
    backgroundColor: '#F3F6FC'
  },
  pendingAudioButton: {
    width: 44,
    height: 44,
    borderRadius: 22,
    alignItems: 'center',
    justifyContent: 'center'
  },
  pendingAudioContent: {
    flex: 1,
    gap: 8
  },
  pendingAudioTrack: {
    height: 6,
    borderRadius: 999,
    overflow: 'hidden',
    backgroundColor: '#D0D7E6'
  },
  pendingAudioTrackFill: {
    width: '32%',
    height: '100%',
    borderRadius: 999
  },
  pendingAudioMeta: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    gap: 12
  },
  pendingAudioTime: {
    color: '#667085',
    fontSize: 12,
    fontWeight: '500'
  },
  pendingAudioStatusPill: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: 6,
    paddingHorizontal: 10,
    paddingVertical: 5,
    borderRadius: 999,
    backgroundColor: '#FFFFFF'
  },
  pendingAudioStatusText: {
    color: '#5B6B8C',
    fontSize: 12,
    fontWeight: '600'
  }
});
//# sourceMappingURL=MediaMessage.js.map