"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _useToggle2 = _interopRequireDefault(require("../../hooks/useToggle"));
var _typography = require("../../components/typography");
var _flex = _interopRequireDefault(require("../../components/templates/flex"));
var _layer = _interopRequireDefault(require("../../components/templates/layer"));
var _container = _interopRequireDefault(require("./container"));
var _header = _interopRequireDefault(require("./header"));
var _item = _interopRequireDefault(require("./item"));
var _useFetchNews = _interopRequireDefault(require("./useFetchNews"));
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 _interopRequireWildcard(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 (var _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); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
var emptyArray = [];
var News = function News(_ref) {
  var _ref$app = _ref.app,
    app = _ref$app === void 0 ? "cloud" : _ref$app,
    onCloseClick = _ref.onCloseClick,
    _ref$modalProps = _ref.modalProps,
    modalProps = _ref$modalProps === void 0 ? {} : _ref$modalProps,
    children = _ref.children;
  var lastSeen = localStorage.getItem("news_last_seen");
  var _useState = (0, _react.useState)(emptyArray),
    news = _useState[0],
    setNews = _useState[1];
  var _useState2 = (0, _react.useState)(),
    error = _useState2[0],
    setError = _useState2[1];
  var _useToggle = (0, _useToggle2["default"])(),
    isOpen = _useToggle[0],
    toggle = _useToggle[1];
  var fetchNews = (0, _useFetchNews["default"])();
  (0, _react.useEffect)(function () {
    fetchNews(app, function (_ref2) {
      var results = _ref2.results;
      return setNews(results);
    }, function () {
      return setError(true);
    });
  }, []);
  var upToDate = (0, _react.useMemo)(function () {
    if (!news.length) return true;
    var firstItem = news[0];
    var publishedAt = firstItem.last_publication_date;
    return new Date(lastSeen) >= new Date(publishedAt);
  }, [lastSeen, news]);
  var onClose = (0, _react.useCallback)(function () {
    toggle();
    localStorage.setItem("news_last_seen", new Date());
    if (onCloseClick) onCloseClick();
  }, [onCloseClick]);
  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, children({
    toggle: toggle,
    isOpen: isOpen,
    upToDate: upToDate
  }), isOpen && /*#__PURE__*/_react["default"].createElement(_layer["default"], {
    backdrop: true,
    onClickOutside: onClose,
    onEsc: onClose
  }, /*#__PURE__*/_react["default"].createElement(_flex["default"], _extends({
    background: "dropdown",
    round: true,
    padding: [6],
    width: "640px",
    height: {
      max: "640px"
    },
    gap: 4,
    column: true
  }, modalProps), /*#__PURE__*/_react["default"].createElement(_header["default"], {
    onClose: onClose
  }), /*#__PURE__*/_react["default"].createElement(_container["default"], {
    column: true,
    gap: 6
  }, error && /*#__PURE__*/_react["default"].createElement(_typography.TextSmall, {
    textAlign: "center"
  }, "Something went wrong \uD83D\uDE14"), !error && !news.length && /*#__PURE__*/_react["default"].createElement(_typography.TextSmall, {
    textAlign: "center"
  }, "There are no latest news"), !error && news.length > 0 && news.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_item["default"], {
      key: item.id,
      item: item
    });
  })))));
};
var _default = exports["default"] = News;