var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMessageDetailsForState = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _types = require("../../../types/types");
var _utils = require("../../../utils/utils");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var useMessageDetailsForState = exports.useMessageDetailsForState = function useMessageDetailsForState(message, initialValue) {
  var _useState = (0, _react.useState)([]),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    fileUploads = _useState2[0],
    setFileUploads = _useState2[1];
  var _useState3 = (0, _react.useState)([]),
    _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
    imageUploads = _useState4[0],
    setImageUploads = _useState4[1];
  var _useState5 = (0, _react.useState)([]),
    _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
    mentionedUsers = _useState6[0],
    setMentionedUsers = _useState6[1];
  var _useState7 = (0, _react.useState)(0),
    _useState8 = (0, _slicedToArray2["default"])(_useState7, 2),
    numberOfUploads = _useState8[0],
    setNumberOfUploads = _useState8[1];
  var initialTextValue = initialValue || '';
  var _useState9 = (0, _react.useState)(initialTextValue),
    _useState10 = (0, _slicedToArray2["default"])(_useState9, 2),
    text = _useState10[0],
    setText = _useState10[1];
  var isEqualToInitialText = text === initialTextValue;
  var _useState11 = (0, _react.useState)(true),
    _useState12 = (0, _slicedToArray2["default"])(_useState11, 2),
    showMoreOptions = _useState12[0],
    setShowMoreOptions = _useState12[1];
  (0, _react.useEffect)(function () {
    if (!isEqualToInitialText) {
      setShowMoreOptions(false);
    }
    if (fileUploads.length || imageUploads.length) {
      setShowMoreOptions(false);
    }
  }, [isEqualToInitialText, imageUploads.length, fileUploads.length]);
  var messageValue = message ? (0, _utils.stringifyMessage)(message) : '';
  (0, _react.useEffect)(function () {
    if (message && Array.isArray(message == null ? void 0 : message.mentioned_users)) {
      var _mentionedUsers = message.mentioned_users.map(function (user) {
        return user.id;
      });
      setMentionedUsers(_mentionedUsers);
    }
  }, [messageValue]);
  var mapAttachmentToFileUpload = function mapAttachmentToFileUpload(attachment) {
    var id = (0, _utils.generateRandomId)();
    if (attachment.type === _types.FileTypes.Audio) {
      return {
        file: {
          duration: attachment.duration || 0,
          name: attachment.title || '',
          size: attachment.file_size || 0,
          type: attachment.mime_type || '',
          uri: attachment.asset_url || ''
        },
        id: id,
        state: 'finished',
        type: _types.FileTypes.Audio,
        url: attachment.asset_url
      };
    } else if (attachment.type === _types.FileTypes.Video) {
      return {
        file: {
          duration: attachment.duration || 0,
          name: attachment.title || '',
          size: attachment.file_size || 0,
          thumb_url: attachment.thumb_url || '',
          type: attachment.mime_type || '',
          uri: attachment.asset_url || ''
        },
        id: id,
        state: 'finished',
        thumb_url: attachment.thumb_url,
        type: _types.FileTypes.Video,
        url: attachment.asset_url
      };
    } else if (attachment.type === _types.FileTypes.VoiceRecording) {
      return {
        file: {
          duration: attachment.duration || 0,
          name: attachment.title || '',
          size: attachment.file_size || 0,
          type: attachment.mime_type || '',
          uri: attachment.asset_url || '',
          waveform_data: attachment.waveform_data
        },
        id: id,
        state: 'finished',
        type: _types.FileTypes.VoiceRecording,
        url: attachment.asset_url
      };
    } else {
      return {
        file: {
          name: attachment.title || '',
          size: attachment.file_size || 0,
          type: attachment.mime_type || '',
          uri: attachment.asset_url || ''
        },
        id: id,
        state: 'finished',
        type: (0, _utils.getFileTypeFromMimeType)(attachment.mime_type || ''),
        url: attachment.asset_url
      };
    }
  };
  (0, _react.useEffect)(function () {
    if (message) {
      setText((message == null ? void 0 : message.text) || '');
      var newFileUploads = [];
      var newImageUploads = [];
      var attachments = Array.isArray(message.attachments) ? message.attachments : [];
      var _iterator = _createForOfIteratorHelper(attachments),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var attachment = _step.value;
          if (attachment.type === _types.FileTypes.Image) {
            var id = (0, _utils.generateRandomId)();
            newImageUploads.push({
              file: {
                height: attachment.original_height || 0,
                name: attachment.fallback || '',
                size: attachment.file_size || 0,
                type: attachment.type || '',
                uri: attachment.image_url || '',
                width: attachment.original_width || 0
              },
              id: id,
              state: 'finished',
              type: _types.FileTypes.Image,
              url: attachment.image_url || attachment.asset_url || attachment.thumb_url
            });
          } else {
            var fileUpload = mapAttachmentToFileUpload(attachment);
            if (fileUpload) {
              newFileUploads.push(fileUpload);
            }
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
      if (newFileUploads.length) {
        setFileUploads(newFileUploads);
      }
      if (newImageUploads.length) {
        setImageUploads(newImageUploads);
      }
    }
  }, [messageValue]);
  return {
    fileUploads: fileUploads,
    imageUploads: imageUploads,
    mentionedUsers: mentionedUsers,
    numberOfUploads: numberOfUploads,
    setFileUploads: setFileUploads,
    setImageUploads: setImageUploads,
    setMentionedUsers: setMentionedUsers,
    setNumberOfUploads: setNumberOfUploads,
    setShowMoreOptions: setShowMoreOptions,
    setText: setText,
    showMoreOptions: showMoreOptions,
    text: text
  };
};
//# sourceMappingURL=useMessageDetailsForState.js.map