"use strict";

exports.__esModule = true;
exports.default = exports.AddToMyListStatusProvider = exports.AddToMyListFeedback = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _link = _interopRequireDefault(require("../../atom/link"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const ADD_TO_MY_LIST_STATUS = {
  IDLE: 'IDLE',
  SUCCESS: 'SUCCESS'
};
const StatusContext = /*#__PURE__*/_react.default.createContext(ADD_TO_MY_LIST_STATUS.IDLE);
const AddToMyListStatusProvider = ({
  children
}) => {
  const [status, setStatus] = (0, _react.useState)(ADD_TO_MY_LIST_STATUS.IDLE);
  (0, _react.useEffect)(() => {
    let timeoutId;
    const DURATION = 3000;
    if (status !== ADD_TO_MY_LIST_STATUS.IDLE) {
      timeoutId = setTimeout(() => setStatus(ADD_TO_MY_LIST_STATUS.IDLE), DURATION);
    }
    return () => clearTimeout(timeoutId);
  }, [status]);
  const statusValue = (0, _react.useMemo)(() => [status, setStatus], [status, setStatus]);
  return /*#__PURE__*/_react.default.createElement(StatusContext.Provider, {
    value: statusValue
  }, children);
};
exports.AddToMyListStatusProvider = AddToMyListStatusProvider;
const AddToMyListFeedback = ({
  addToMyListText,
  removeFromMyListText,
  favorite
}) => {
  const [status] = (0, _react.useContext)(StatusContext);
  if (status === ADD_TO_MY_LIST_STATUS.IDLE) return null;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_style.default.feedback),
    "data-testid": "add-to-my-list-feedback",
    "data-name": 'add-to-my-list-feedback'
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyCheck, {
    className: _style.default.checkIcon,
    width: 13,
    height: 13
  }), /*#__PURE__*/_react.default.createElement("p", {
    role: "status"
  }, favorite ? addToMyListText : removeFromMyListText));
};
exports.AddToMyListFeedback = AddToMyListFeedback;
const AddToMyList = ({
  style,
  addToMyListButton,
  favorite,
  onFavoriteClick
}) => {
  const [, setStatus] = (0, _react.useContext)(StatusContext);
  const cb = () => setStatus(ADD_TO_MY_LIST_STATUS.SUCCESS);
  function onClick() {
    onFavoriteClick(cb);
  }
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (_style.default.container, style)
  }, /*#__PURE__*/_react.default.createElement(_link.default, {
    "data-testid": "add-to-my-list-button",
    "data-name": 'add-to-my-list-button',
    onClick: onClick,
    className: _style.default.cta
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.wrapper
  }, favorite ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyCheck, {
    className: _style.default.shareIcon,
    width: 15,
    height: 15
  }) : /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationMore, {
    className: _style.default.shareIcon,
    width: 12,
    height: 12
  }), /*#__PURE__*/_react.default.createElement("p", null, addToMyListButton))));
};
AddToMyListStatusProvider.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _propTypes.default.node
} : {};
AddToMyListFeedback.propTypes = process.env.NODE_ENV !== "production" ? {
  addToMyListText: _propTypes.default.string,
  removeFromMyListText: _propTypes.default.string,
  favorite: _propTypes.default.bool
} : {};
AddToMyList.propTypes = process.env.NODE_ENV !== "production" ? {
  style: _propTypes.default.string,
  addToMyListButton: _propTypes.default.string,
  favorite: _propTypes.default.bool,
  onFavoriteClick: _propTypes.default.func
} : {};
var _default = exports.default = AddToMyList;
//# sourceMappingURL=index.js.map