"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 _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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; }
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;