"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FeedbackBox = void 0;
var _react = _interopRequireWildcard(require("react"));
var _message = require("../message");
var _commonService = require("../../../services/commonService");
var _brandService = require("../../../services/brandService");
var _emailService = require("../../../services/emailService");
var _userContext = require("../../../context/user/userContext");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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 FeedbackBox = props => {
  const {
    state
  } = (0, _react.useContext)(_userContext.UserContext);
  const [feedbackValue, setFeedbackValue] = _react.default.useState("");
  const [isClicked, setClicked] = _react.default.useState(false);
  const [valid, setValid] = _react.default.useState(false);
  const maxLength = 2000;
  const handleKeyPress = e => {
    let count = e.target.value;
    let length = count.length;
    if (length > maxLength) {
      count = count.substring(0, count - 1);
      e.target.value = count;
    }
    setFeedbackValue(count);
    if (count.length > 0) {
      setValid(true);
    } else {
      setValid(false);
    }
  };
  const handleSendFeedback = e => {
    setClicked(true);
    if (valid) {
      var _state$user, _state$user2;
      const validResponse = "Feedback Received!".toUpperCase();
      let brandID = state === null || state === void 0 || (_state$user = state.user) === null || _state$user === void 0 || (_state$user = _state$user.userProfile) === null || _state$user === void 0 ? void 0 : _state$user.brandID;
      let brandName = (0, _brandService.GetBrandName)(brandID);
      let email = state === null || state === void 0 || (_state$user2 = state.user) === null || _state$user2 === void 0 || (_state$user2 = _state$user2.userProfile) === null || _state$user2 === void 0 ? void 0 : _state$user2.email;
      const input = {
        Feedback: feedbackValue,
        Email: email
      };
      (0, _commonService.SendFeedback)(input, brandID).then(res => {
        if (res !== null && res !== undefined && res.toUpperCase() === validResponse) {
          (0, _emailService.SendEmail)({
            Email: email,
            Comments: feedbackValue,
            BrandName: brandName
          });
          (0, _message.showMessage)("We have received your feedback successfully.");
        } else {
          (0, _message.showMessage)("Could not send feedback", -2);
        }
      }, err => {
        (0, _message.showMessage)("Could not send feedback", -2);
      });
      props.callOpen(false);
    }
    e.preventDefault();
  };
  const handleClose = e => {
    props.callOpen(false);
    e.preventDefault();
  };
  const getMainClassName = () => {
    let cls = "modal-wrapper";
    if (props.open === true) {
      cls += " show-modal-wrapper";
    }
    cls += " feedback";
    return cls;
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.default.Fragment, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: getMainClassName(),
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "modal-block",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "feedback-heading-container",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            children: "Please report any issues or provide feedback below."
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "confirmation-content-wrapper",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
            className: "feedback-container",
            children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("textarea", {
              onChange: handleKeyPress,
              className: "empty-comments",
              rows: "7",
              maxLength: maxLength
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: "counter-placeholder",
              children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
                children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
                  className: "current-counter",
                  children: feedbackValue.length
                }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
                  "data-val": maxLength,
                  className: "maximum-counter",
                  children: ["/ ", maxLength]
                })]
              })
            })]
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: "error-message ".concat(isClicked === false ? "hide" : feedbackValue.length === 0 ? "" : "hide", " "),
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
              children: "Please enter feedback"
            })
          })]
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "confirmation-button-block",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
            className: "confirmation-button w-button yes",
            onClick: event => {
              handleSendFeedback(event);
            },
            children: "Submit"
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
            onClick: event => {
              handleClose(event);
            },
            className: "confirmation-button w-button no",
            children: "Cancel"
          })]
        })]
      })
    })
  });
};
exports.FeedbackBox = FeedbackBox;