"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ProfileImagePlaceholder = require("../MainComponents/ProfileImagePlaceholder");
var _RoomListComponents = require("../styled/RoomListComponents");
var _Composing = _interopRequireDefault(require("../styled/StyledInputComponents/Composing"));
var _reactNative = require("react-native");
var _LastMessageItem = _interopRequireDefault(require("./LastMessageItem"));
var _StyledRoomComponents = require("./styled/StyledRoomComponents");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const ChatRoomItem = ({
  chat,
  index,
  isDriver,
  config
}) => {
  const displayName = String(chat?.title || chat?.name || '').trim();
  const lastMessage = (0, _react.useMemo)(() => {
    if (!chat?.messages || chat.messages.length === 0) {
      return undefined;
    }
    for (let i = chat.messages.length - 1; i >= 0; i--) {
      const msg = chat.messages[i];
      if (!msg.deleted && !msg.isDeleted) {
        return msg;
      }
    }
    return undefined;
  }, [chat?.messages]);

  // Mirrors web's formatter: tolerates undefined/garbage inputs (returns
  // undefined instead of "NaN:NaN"), and uses HH:MM today / MM/DD this
  // year / YYYY/MM/DD older.
  const formatTimeToHHMM = isoTime => {
    if (isoTime == null) {
      return undefined;
    }
    let date;
    try {
      if (isoTime instanceof Date) {
        date = isoTime;
      } else if (typeof isoTime === 'number') {
        date = new Date(isoTime);
      } else if (typeof isoTime === 'string') {
        const trimmed = isoTime.trim();
        date = /^\d+$/.test(trimmed) ? new Date(parseInt(trimmed, 10)) : new Date(trimmed);
      } else {
        return undefined;
      }
      if (isNaN(date.getTime())) {
        return undefined;
      }
      const now = new Date();
      if (date.getFullYear() !== now.getFullYear()) {
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const day = date.getDate().toString().padStart(2, '0');
        return `${year}/${month}/${day}`;
      }
      if (date.toDateString() === now.toDateString()) {
        const hours = date.getHours().toString().padStart(2, '0');
        const minutes = date.getMinutes().toString().padStart(2, '0');
        return `${hours}:${minutes}`;
      }
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      return `${month}/${day}`;
    } catch {
      return undefined;
    }
  };
  const stamp = formatTimeToHHMM(lastMessage?.date ?? chat?.createdAt);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_RoomListComponents.ChatItem, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_ProfileImagePlaceholder.ProfileImagePlaceholder, {
      name: displayName,
      icon: chat?.icon,
      active: false
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: {
        flexDirection: 'column',
        justifyContent: 'space-between',
        flex: 2,
        width: '100%',
        paddingVertical: 8,
        paddingRight: 8,
        borderBottomWidth: 1,
        borderBottomColor: '#F0F0F0'
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: {
          flexDirection: 'row',
          alignItems: 'center'
        },
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_RoomListComponents.ChatInfo, {
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_RoomListComponents.ChatName, {
            text: displayName
          })
        }), stamp ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_RoomListComponents.UserCount, {
          style: {
            color: '#8C8C8C',
            fontSize: 12
          },
          text: stamp
        }) : null]
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: {
          width: '100%',
          flexDirection: 'row',
          justifyContent: 'space-between',
          alignItems: 'flex-start',
          minHeight: 40
        },
        children: [chat.composing ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Composing.default, {
          usersTyping: chat.composingList
          // style={{ color: !isChatActive ? '#141414' : '#fff' }}
        }) : lastMessage?.body ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_LastMessageItem.default, {
          lastMessage: lastMessage
        }) : chat.messages.length === 0 && chat.historyComplete ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledRoomComponents.LastRoomMessageText, {
          children: "Room created"
        }) : undefined, chat.unreadMessages && chat.unreadMessages > 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          style: {
            borderRadius: 8,
            backgroundColor: config?.colors?.primary,
            padding: 2,
            minWidth: 24,
            minHeight: 24,
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            marginLeft: 'auto'
          },
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            style: {
              // color: isChatActive ? "#141414" : "#fff",
              color: '#141414',
              fontSize: 14,
              fontWeight: '600'
            },
            children: chat.unreadMessages || ''
          })
        }) : null]
      }), isDriver && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
        style: {
          height: 1,
          backgroundColor: '#0052CD0D',
          marginTop: 8
        }
      })]
    })]
  }, index);
};
var _default = exports.default = ChatRoomItem;
//# sourceMappingURL=ChatRoomItem.js.map