"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _MediaComponents = require("./StyledInputComponents/MediaComponents");
var _reactRedux = require("react-redux");
var _MODAL_TYPES = require("../../helpers/constants/MODAL_TYPES");
var _chatSettingsSlice = require("../../roomStore/chatSettingsSlice");
var _reactNative = require("react-native");
var _useChatSettingState = require("../../hooks/useChatSettingState");
var _icons = require("../../assets/icons");
var _mimeToExtension = require("../../helpers/mimeToExtension");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const FileDownload = ({
  fileName,
  fileURL,
  mimetype,
  size,
  isUser,
  originalName,
  duration,
  waveForm,
  pending = false,
  placeholderIcon
}) => {
  const dispatch = (0, _reactRedux.useDispatch)();
  const {
    config
  } = (0, _useChatSettingState.useChatSettingState)();
  const formatFileSize = sizeInBytes => {
    const size = parseInt(sizeInBytes, 10);
    if (isNaN(size)) {
      return 'Invalid size';
    }
    if (size < 1024) {
      return `${size} B`;
    } else if (size < 1024 ** 2) {
      return `${(size / 1024).toFixed(2)} KB`;
    } else if (size < 1024 ** 3) {
      return `${(size / 1024 ** 2).toFixed(2)} MB`;
    } else {
      return `${(size / 1024 ** 3).toFixed(2)} GB`;
    }
  };
  const formatFileName = (name, maxLength) => {
    const dotIndex = name.lastIndexOf('.');
    const extension = dotIndex !== -1 ? name.substring(dotIndex) : '';
    const baseName = dotIndex !== -1 ? name.substring(0, dotIndex) : name;
    if (baseName.length + extension.length <= maxLength) {
      return name;
    }
    const shortenedBaseName = baseName.substring(0, maxLength - extension.length - 3);
    return `${shortenedBaseName}...${extension}`;
  };
  const handleOpen = () => {
    if (!fileURL || pending) {
      return;
    }
    dispatch((0, _chatSettingsSlice.setActiveFile)({
      fileName,
      fileURL,
      mimetype,
      originalName,
      duration,
      waveForm
    }));
    dispatch((0, _chatSettingsSlice.setActiveModal)(_MODAL_TYPES.MODAL_TYPES.FILE_PREVIEW));
  };
  const extensionLabel = (() => {
    const dotIndex = fileName.lastIndexOf('.');
    if (dotIndex !== -1 && dotIndex < fileName.length - 1) {
      return fileName.slice(dotIndex + 1).toUpperCase();
    }
    return (0, _mimeToExtension.getExtensionForMime)(mimetype).replace('.', '').toUpperCase();
  })();
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_MediaComponents.UnsupportedContainer, {
    isUser: isUser,
    onPress: handleOpen,
    activeOpacity: pending ? 1 : 0.7,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.BackgroundFile, {
      children: placeholderIcon || /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.FileIcon, {
        width: 36,
        height: 36
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_MediaComponents.FileInformation, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileName, {
        numberOfLines: 1,
        isUser: isUser,
        colorIsUser: config?.colors?.primary,
        colorUsers: config?.colors?.secondary,
        children: formatFileName(fileName, 20)
      }), (pending || size) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileSizeContainer, {
        children: pending ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
          style: {
            flexDirection: 'row',
            alignItems: 'center',
            gap: 6
          },
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ActivityIndicator, {
            size: "small",
            color: "#5B6B8C"
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileSize, {
            children: "Uploading..."
          })]
        }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileSize, {
          children: formatFileSize(size)
        })
      }), !pending && !size && /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileSizeContainer, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileSize, {
          children: extensionLabel
        })
      })]
    })]
  });
};
var _default = exports.default = FileDownload;
//# sourceMappingURL=UnsupportedType.js.map