"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.usePostImpression = void 0;
var _react = require("react");
var _useCustomRankingGlobalFeed = require("./useCustomRankingGlobalFeed");
var _AdEngine = _interopRequireDefault(require("../../core/engines/AdEngine"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const usePostImpression = postList => {
  const [postViews, setPostViews] = (0, _react.useState)([]);
  const handleViewChange = (0, _react.useCallback)(({
    viewableItems
  }) => {
    setPostViews([...viewableItems]);
  }, []);
  (0, _react.useEffect)(() => {
    if (postViews.length > 0) {
      for (const viewablePost of postViews) {
        if (viewablePost) {
          const post = postList === null || postList === void 0 ? void 0 : postList.find(post => (0, _useCustomRankingGlobalFeed.isAmityAd)(post) ? post.adId === viewablePost.item.adId : post.postId === viewablePost.item.postId);
          if (post && (0, _useCustomRankingGlobalFeed.isAmityAd)(post)) {
            _AdEngine.default.instance.markSeen(post, 'feed');
          }
          if (post && !(0, _useCustomRankingGlobalFeed.isAmityAd)(post)) {
            post.analytics.markAsViewed();
          }
        }
      }
    }
  }, [postViews, postList]);
  return {
    handleViewChange
  };
};
exports.usePostImpression = usePostImpression;
//# sourceMappingURL=usePostImpression.js.map