"use strict";

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

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

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

require("./App.css");

var _ChatWindow = _interopRequireDefault(require("./components/ChatWindow"));

var _ChatIcon = _interopRequireDefault(require("./components/ChatIcon"));

var _sampleData = require("./dataProvider/sampleData");

var _botResponse = _interopRequireDefault(require("./models/botResponse"));

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; }

const contextManager = require("./stateManager/manageSession");

function ChatbotWindow() {
  const [isConnected, setIsConnected] = (0, _react.useState)(false);
  const [chatHistory, setChatHistory] = (0, _react.useState)([]);
  const [openChatWindow, setOpenChatWindow] = (0, _react.useState)(false);
  const [messageLoader, setMessageLoader] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    if (openChatWindow) {
      setTimeout(function () {
        console.log("loading chat history if any");
        setIsConnected(true); // fetch the conversation data from the database, set true if success else false

        if (chatHistory.length === 0) {
          (0, _sampleData.conversations)("welcome").then(res => {
            console.log("welcome res obj > ", res);
            contextManager.cachePreviousState(res);
            const welcomeMessage = (0, _botResponse.default)(res); // console.log("welcomeMessage > ", welcomeMessage);

            setChatHistory([...chatHistory, welcomeMessage]); // set the conversation data
          }).catch(e => {
            console.log("error while setting the conversations >", e);
          });
        }
      }, 500);
    }
  }, [openChatWindow]);

  function handleUserMessage(userResponse) {
    setMessageLoader(true);
    appendMessage(userResponse); // botResponse(userResponse.text);

    (0, _sampleData.conversations)(userResponse.message.toLowerCase()).then(res => {
      // console.log("handleUserMessage res >> ", res);
      const botResp = (0, _botResponse.default)(res);
      const delay = 500;
      setTimeout(() => {
        appendMessage(botResp);
        setMessageLoader(false);
      }, delay);
    }).catch(e => {
      console.log("error while setting the conversations >", e);
    });
  }

  function appendMessage(message) {
    console.log("appending message to chathistory");
    setChatHistory(oldData => [...oldData, message]);
  }

  function handleChatIconClick() {
    contextManager.createConversationId();
    setOpenChatWindow(!openChatWindow);
  } // function botResponse(userMsg) {
  //   conversations(userMsg.toLowerCase()).then(res => {
  //     console.log("welcome res > ", res);
  //     const botResp = generateBotResponse(res);
  //     const delay = 500;
  //     setTimeout(() => {
  //       appendMessage(botResp);
  //       setMessageLoader(false);
  //     }, delay);
  //   }).catch(e => {
  //     console.log("error while setting the conversations >", e);
  //   });
  // }


  function handleMinimizeWindow() {
    contextManager.getConversationId();
    setOpenChatWindow(false);
  }

  function closeConversation() {
    setOpenChatWindow(false);
    setChatHistory([]);
    setIsConnected(false);
  }

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, openChatWindow ? /*#__PURE__*/_react.default.createElement(_ChatWindow.default, {
    theme: "blue",
    title: "efia",
    chatHistory: chatHistory,
    isConnected: isConnected,
    minimizeWindow: handleMinimizeWindow,
    closeConversation: closeConversation,
    handleUserResponse: handleUserMessage,
    enableMessageLoader: messageLoader,
    windowSize: "large"
  }) : /*#__PURE__*/_react.default.createElement(_ChatIcon.default, {
    onClick: handleChatIconClick
  }));
}

var _default = ChatbotWindow;
exports.default = _default;