"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 _useChatSettingState = require("../../hooks/useChatSettingState");
var _icons = require("../../assets/icons");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const FileDownload = ({
  fileName,
  fileURL,
  mimetype,
  size,
  isUser
}) => {
  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 = () => {
    dispatch((0, _chatSettingsSlice.setActiveFile)({
      fileName,
      fileURL,
      mimetype
    }));
    dispatch((0, _chatSettingsSlice.setActiveModal)(_MODAL_TYPES.MODAL_TYPES.FILE_PREVIEW));
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_MediaComponents.UnsupportedContainer, {
    onPress: handleOpen,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.BackgroundFile, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.FileIcon, {})
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_MediaComponents.FileInformation, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileName, {
        isUser: isUser,
        colorIsUser: config?.colors?.primary,
        colorUsers: config?.colors?.secondary,
        children: formatFileName(fileName, 20)
      }), size && /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileSizeContainer, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaComponents.FileSize, {
          children: formatFileSize(size)
        })
      })]
    })]
  });
};
var _default = exports.default = FileDownload;
//# sourceMappingURL=UnsupportedType.js.map