"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _UnsupportedType = _interopRequireDefault(require("../styled/UnsupportedType"));
var _MessageImage = _interopRequireDefault(require("../styled/MessageImage"));
var _VideoMessage = _interopRequireDefault(require("../styled/VideoMessage"));
var _AudioMessage = _interopRequireDefault(require("../styled/AudioMessage"));
var _reactNative = require("react-native");
var _mimeToExtension = require("../../helpers/mimeToExtension");
var _icons = require("../../assets/icons");
var _mediaDimensions = require("../../helpers/mediaDimensions");
var _useChatSettingState = require("../../hooks/useChatSettingState");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const pendingImageDims = (0, _mediaDimensions.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
  } = (0, _useChatSettingState.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__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: styles.pendingImageWrapper,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Image, {
        source: {
          uri: previewUri
        },
        style: styles.pendingImage,
        resizeMode: "cover"
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: styles.pendingImageOverlay,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ActivityIndicator, {
          size: "small",
          color: "#FFFFFF"
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: styles.pendingOverlayText,
          children: statusLabel
        })]
      })]
    });
  }
  if (isVideo) {
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: styles.pendingVideoCard,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: styles.pendingVideoPreview,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          style: styles.pendingVideoIcon,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.PlayIcon, {
            width: 24,
            height: 24
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ActivityIndicator, {
          size: "small",
          color: "#5B6B8C"
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: styles.pendingVideoLabel,
        children: fileName
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: styles.pendingVideoStatus,
        children: statusLabel
      })]
    });
  }
  if (isAudio) {
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: styles.pendingAudioCard,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: [styles.pendingAudioButton, {
          backgroundColor: primaryColor
        }],
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.PlayIcon, {
          width: 18,
          height: 18,
          color: "#fff"
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: styles.pendingAudioContent,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          style: styles.pendingAudioTrack,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
            style: [styles.pendingAudioTrackFill, {
              backgroundColor: primaryColor
            }]
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
          style: styles.pendingAudioMeta,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            style: styles.pendingAudioTime,
            children: "0:00"
          }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
            style: styles.pendingAudioStatusPill,
            children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ActivityIndicator, {
              size: "small",
              color: "#5B6B8C"
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
              style: styles.pendingAudioStatusText,
              children: statusLabel
            })]
          })]
        })]
      })]
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_UnsupportedType.default, {
    fileURL: previewUri || '',
    fileName: fileName,
    mimetype: mimeType || 'application/octet-stream',
    size: size,
    isUser: isUser,
    pending: true,
    placeholderIcon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.FileIcon, {
      width: 36,
      height: 36
    })
  });
};
const MediaMessage = ({
  mimeType,
  location,
  messageText,
  message,
  isUser
}) => {
  if (mimeType) {
    const displayName = (0, _mimeToExtension.deriveDisplayFilename)({
      fileName: message?.fileName,
      originalName: message?.originalName,
      url: location || messageText,
      mime: mimeType
    });
    const isAudioPayload = (0, _mimeToExtension.isLikelyAudio)(mimeType, displayName, location, {
      duration: message?.duration,
      waveForm: message?.waveForm,
      originalName: message?.originalName
    });
    if (message?.pending) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(PendingMediaMessage, {
        fileName: displayName,
        mimeType: mimeType,
        previewUri: messageText || location || '',
        isUser: isUser,
        size: message?.size
      });
    }
    switch (true) {
      case mimeType.startsWith('image/'):
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_MessageImage.default, {
          fileName: displayName,
          fileURL: location || messageText || '',
          mimetype: mimeType
        });
      case mimeType.startsWith('video/'):
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_VideoMessage.default, {
          fileName: displayName,
          fileURL: location || '',
          mimetype: mimeType
        });
      case mimeType.startsWith('audio/'):
        {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_AudioMessage.default, {
            src: location || '',
            mimeType: mimeType,
            fileName: displayName,
            originalName: message?.originalName,
            duration: message?.duration,
            waveForm: message?.waveForm
          });
        }
      case mimeType.includes('application/octet-stream'):
        {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_AudioMessage.default, {
            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__*/(0, _jsxRuntime.jsx)(_AudioMessage.default, {
              src: location || '',
              mimeType: mimeType,
              fileName: displayName,
              originalName: message?.originalName,
              duration: message?.duration,
              waveForm: message?.waveForm
            });
          }
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_UnsupportedType.default, {
            fileURL: location ? location : '',
            fileName: displayName,
            mimetype: mimeType,
            isUser: isUser,
            originalName: message?.originalName,
            duration: message?.duration,
            waveForm: message?.waveForm
          });
        }
    }
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
    children: "Unsupported media type"
  });
};
var _default = exports.default = MediaMessage;
const styles = _reactNative.StyleSheet.create({
  pendingImageWrapper: {
    width: pendingImageDims.width,
    height: pendingImageDims.height,
    borderRadius: 12,
    overflow: 'hidden',
    backgroundColor: '#EAF0FB'
  },
  pendingImage: {
    width: '100%',
    height: '100%'
  },
  pendingImageOverlay: {
    ..._reactNative.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