"use strict";

exports.__esModule = true;
exports.useSyncCollectionOnEntityReturn = useSyncCollectionOnEntityReturn;
var _react = require("react");
var _WixPatternsRouterProvider = require("../providers/WixPatternsRouterProvider");
var _react2 = require("@wix/bex-core/react");
var _useSelector = require("../useSelector");
function useSyncCollectionOnEntityReturn({
  cachedState,
  updateCollection,
  checkIfViewSynced,
  getEntityFromRouterState,
  getCollectionsToUpdate
}) {
  const container = (0, _react2.useWixPatternsContainer)();
  const router = (0, _WixPatternsRouterProvider.useWixPatternsRouter)();
  const timeoutRef = (0, _react.useRef)();
  const routeState = (0, _useSelector.useSelector)(() => router == null ? void 0 : router.currentState);
  const entity = (0, _react.useMemo)(() => routeState ? getEntityFromRouterState(routeState) : null, [getEntityFromRouterState, routeState]);
  const collections = (0, _react.useMemo)(() => {
    if (!cachedState) {
      return null;
    }
    if (!entity) {
      return [cachedState.toolbar.collection];
    }
    return (getCollectionsToUpdate == null ? void 0 : getCollectionsToUpdate(cachedState, entity)) || [cachedState.toolbar.collection];
  }, [cachedState, entity]);
  const [currentToast, setCurrentToast] = (0, _react.useState)(null);
  const showViewUpdatedToast = () => {
    const toast = container.showToast == null ? void 0 : container.showToast({
      type: 'STANDARD',
      timeout: 'NONE',
      message: container.translate('cairo.updatedView.collectionPage.toast.desc'),
      onCloseClick: () => {
        setCurrentToast(null);
      },
      biName: 'cairo-collection-page-updated-toast',
      action: {
        text: container.translate('cairo.updatedView.collectionPage.toast.cta'),
        onClick: () => {
          collections == null || collections.forEach(collection => collection.clearResultAndMoveToStart({
            force: true
          }));
          setCurrentToast(null);
        }
      }
    });
    if (toast) {
      setCurrentToast(toast);
    }
  };
  (0, _react.useEffect)(() => {
    if (entity && collections && router) {
      collections.forEach(collection => {
        updateCollection(collection, entity);
      });
      const timeoutId = window.setTimeout(() => {
        Promise.all(collections.map(collection => checkIfViewSynced(collection, entity))).then(results => {
          const isViewSynced = results.some(Boolean) && !router.isCollectionStale;
          if (!isViewSynced) {
            router.isCollectionStale = true;
            showViewUpdatedToast();
          }
        });
      }, 3000);
      timeoutRef.current = timeoutId;
    } else if (router != null && router.isCollectionStale) {
      showViewUpdatedToast();
    }
  }, [collections, entity]);
  (0, _react.useEffect)(() => {
    if (!currentToast || !cachedState) {
      return () => {};
    }
    const removeToast = () => {
      currentToast == null || currentToast.remove();
      setCurrentToast(null);
    };
    const onFetch = () => {
      removeToast();
      if (router) {
        router.isUsingCache = false;
        router.isCollectionStale = false;
      }
    };
    collections == null || collections.forEach(collection => {
      collection == null || collection.emitter.on('fetch', () => {
        onFetch();
      });
    });
    return () => {
      collections == null || collections.forEach(collection => {
        collection == null || collection.emitter.off('fetch', () => {
          onFetch();
        });
      });
    };
  }, [currentToast, collections]);
  (0, _react.useEffect)(() => {
    return () => {
      if (timeoutRef.current) {
        window.clearTimeout(timeoutRef.current);
      }
      currentToast == null || currentToast.remove();
    };
  }, [currentToast]);
}
//# sourceMappingURL=useSyncCollectionOnEntityReturn.js.map