"use strict";

require("core-js/modules/web.dom-collections.iterator.js");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _QuickReply = _interopRequireDefault(require("../chatResponseFormats/QuickReply"));

var _Hyperlink = _interopRequireDefault(require("../chatResponseFormats/Hyperlink"));

var _TitleText = _interopRequireDefault(require("./TitleText"));

var _reactCalendar = _interopRequireDefault(require("react-calendar"));

require("react-calendar/dist/Calendar.css");

var _moment = _interopRequireDefault(require("moment"));

var _MultiLineText = _interopRequireDefault(require("../chatResponseFormats/MultiLineText"));

var _CardCarousel = _interopRequireDefault(require("../chatResponseFormats/CardCarousel"));

var _responseTypes = _interopRequireDefault(require("../../models/responseTypes"));

var _PlainText = _interopRequireDefault(require("../chatResponseFormats/PlainText"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function Conversation(props) {
  const el = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (el.current) {
      el.current.scrollIntoView({
        behavior: 'smooth',
        block: 'center'
      });
    }
  });

  function setDate(value) {
    if (value) props.handleButtonClick({
      action: "date",
      date: (0, _moment.default)(value).format('DD-MM-YYYY')
    });
  }

  function generateFollowText(ft, i) {
    // console.log("generateFollowText  >>", ft, i);
    switch (ft.messageType) {
      case _responseTypes.default.quickReplies:
        return i === props.messageData.length - 1 ? /*#__PURE__*/_react.default.createElement(_QuickReply.default, {
          handleButtonClick: data => props.handleButtonClick(data),
          options: ft.options
        }) : "";

      case _responseTypes.default.hyperLink:
        return /*#__PURE__*/_react.default.createElement(_Hyperlink.default, {
          handleButtonClick: data => props.handleButtonClick(data),
          options: ft.options
        });

      case _responseTypes.default.date:
        return i === props.messageData.length - 1 ? /*#__PURE__*/_react.default.createElement("div", {
          style: {
            marginBottom: "12px"
          }
        }, /*#__PURE__*/_react.default.createElement(_reactCalendar.default, {
          calendarType: "US",
          onChange: setDate
        })) : "";

      case _responseTypes.default.cardCarousel:
        return i === props.messageData.length - 1 ? /*#__PURE__*/_react.default.createElement(_CardCarousel.default, {
          params: ft,
          handleButtonClick: data => props.handleButtonClick(data)
        }) : "";

      case _responseTypes.default.multiLine:
        return /*#__PURE__*/_react.default.createElement(_MultiLineText.default, {
          handleButtonClick: data => props.handleButtonClick(data),
          options: ft.options
        });

      default:
        return /*#__PURE__*/_react.default.createElement(_PlainText.default, {
          text: ft.message
        });
    }
  }

  return /*#__PURE__*/_react.default.createElement("main", {
    className: "messenger-chat"
  }, /*#__PURE__*/_react.default.createElement("div", {
    id: "printme"
  }, console.log("Conversation props.messageData >>", props.messageData.length, props.messageData), props.messageData && props.messageData.map((msgObj, i) => {
    // console.log("Conversation inloop msgObj >>", msgObj);
    return /*#__PURE__*/_react.default.createElement("div", {
      key: "message".concat(new Date().getTime()).concat(i)
    }, /*#__PURE__*/_react.default.createElement(_TitleText.default, {
      params: msgObj
    }), "followMessage" in msgObj ? msgObj.followMessage.map(ft => generateFollowText(ft, i)) : "");
  })), /*#__PURE__*/_react.default.createElement("div", {
    id: "el",
    ref: el
  }));
}

var _default = Conversation;
exports.default = _default;