"use strict";

exports.__esModule = true;
exports.useCollectionPageLoadReporting = void 0;
var _bi = require("@wix/bex-core/bi");
var _react = require("@wix/bex-core/react");
var _react2 = require("react");
var _WixPatternsRouterProvider = require("../../providers/WixPatternsRouterProvider");
const useCollectionPageLoadReporting = ({
  startTime,
  withinRouter
}) => {
  const [reportedInteractive, setReportedInteractive] = (0, _react2.useState)(false);
  const [reportedFullyLoaded, setReportedFullyLoaded] = (0, _react2.useState)(false);
  const {
    createBILogger,
    initTask: {
      status
    }
  } = (0, _react.useWixPatternsContainer)();
  const router = (0, _WixPatternsRouterProvider.useWixPatternsRouter)();
  const reportBI = createBILogger({
    pageType: 'Collection'
  });
  const getBIParams = () => {
    const loadingTime = Math.round(performance.now() - startTime);
    return {
      componentType: 'Collection',
      loadingTime,
      withinRouter,
      routerUsage: withinRouter
    };
  };
  const reportPageInteractive = () => {
    if (!(router != null && router.currentState)) {
      return;
    }
    reportBI((0, _bi.wixPatternsPageInteractiveSrc144Evid1000)(getBIParams()));
    setReportedInteractive(true);
  };
  const reportPageFullyLoaded = () => {
    if (!(router != null && router.currentState)) {
      return;
    }
    reportBI((0, _bi.wixPatternsFullPageLoadCompletedSrc144Evid1001)(getBIParams()));
    setReportedFullyLoaded(true);
  };
  (0, _react2.useEffect)(() => {
    if (status.isLoading && !reportedInteractive) {
      reportPageInteractive();
      return;
    }
    if (status.isSuccess && !reportedFullyLoaded) {
      if (!reportedInteractive) {
        // it happens when returning from entity page with client side router
        reportPageInteractive();
      }
      reportPageFullyLoaded();
    }
  }, [status.isSuccess, status.isLoading, reportedInteractive, reportedFullyLoaded]);
};
exports.useCollectionPageLoadReporting = useCollectionPageLoadReporting;
//# sourceMappingURL=useCollectionPageLoadReporting.js.map