"use strict";

exports.__esModule = true;
exports.useAutoScrollOnLoad = useAutoScrollOnLoad;
var _react = require("react");
var _react2 = require("@wix/bex-core/react");
var _bexUtils = require("@wix/bex-utils");
function useAutoScrollOnLoad(scrollToItem, doesItemExist) {
  var _pageState$initTask2;
  const {
    showToast,
    translate: t
  } = (0, _react2.useWixPatternsContainer)();
  const pageState = (0, _react2.useFormPageContext)();
  (0, _react.useEffect)(() => {
    var _pageState$initTask;
    const pageStatus = pageState == null || (_pageState$initTask = pageState.initTask) == null ? void 0 : _pageState$initTask.status;
    if (!pageStatus.isLoading && !pageStatus.isIdle) {
      const anchorName = (0, _bexUtils.getAnchorParam)();
      if (anchorName) {
        if (doesItemExist.current != null && doesItemExist.current()) {
          scrollToItem.current == null || scrollToItem.current();
        } else {
          showToast == null || showToast({
            message: t('cairo.settings.search.anchor-doesnt-exist.error', {
              anchorName
            }),
            type: 'WARNING'
          });
        }
      }
    }
  }, [pageState == null || (_pageState$initTask2 = pageState.initTask) == null ? void 0 : _pageState$initTask2.status]);
}
//# sourceMappingURL=useAutoScrollOnLoad.js.map