"use strict";

exports.__esModule = true;
exports.useToolbarCollectionSyncProps = useToolbarCollectionSyncProps;
var _react = require("react");
var _mobx = require("mobx");
var _checkConflictingToolbarProps = require("./checkConflictingToolbarProps");
var _providers = require("../../providers");
var _WixPatternsRouterProvider = require("../../providers/WixPatternsRouterProvider");
var _useSyncTotal = require("../../hooks/useSyncTotal");
function useToolbarCollectionSyncProps(props) {
  const {
    state,
    views,
    tabs,
    exportButton,
    importButton,
    dragAndDropCategories,
    maxSelection,
    search,
    filters,
    isUsingDragAndDrop,
    isUsingBulkActionsToolbar,
    isUsingHorizontalScroll,
    isUsingFolders,
    isUsingTableVirtualization,
    summaryBar,
    secondaryActions,
    moreActions,
    internalScroll,
    showFieldTypeIcons
  } = props;
  (0, _checkConflictingToolbarProps.checkConflictingToolbarProps)(props);
  state.internalScroll = state.container.isPanelLayout ? true : internalScroll;
  (0, _mobx.runInAction)(() => {
    state.showFieldTypeIcons = showFieldTypeIcons ?? false;
  });
  const pageState = (0, _providers.useCollectionPageContext)();
  const router = (0, _WixPatternsRouterProvider.useWixPatternsRouter)();
  (0, _react.useEffect)(() => {
    pageState.primaryToolbar = state;
    return () => {
      if (pageState.primaryToolbar === state) {
        pageState.primaryToolbar = null;
      }
    };
  }, []);
  (0, _react.useEffect)(() => {
    (0, _mobx.reaction)(() => ({
      scrollTop: state.scrollTopBeforeUnmount,
      routerState: router == null ? void 0 : router.currentState
    }), ({
      scrollTop,
      routerState
    }) => {
      pageState.scrollTopBeforeUnmount = routerState != null && routerState._createdEntity ? 0 : scrollTop;
    }, {
      fireImmediately: true
    });
    return () => {
      var _pageState$pageState$;
      const scrollTop = (_pageState$pageState$ = pageState.pageState.scrollableContentRef.current) == null ? void 0 : _pageState$pageState$.scrollTop;
      if (scrollTop !== undefined) {
        state.scrollTopBeforeUnmount = scrollTop;
      }
    };
  }, []);
  const getIsUsingAllItemsView = () => !!(
  // views=undefined, it means thw user didn't pass so allItemsViews=false

  views && (
  // views.props=undefined, it means the user passed views without configuring allItems so allItemsViews=true
  !views.props ||
  // views.props.allItemsViewProps=undefined, it means the user passed allItemsViewProps
  // without configuring the hidden so allItemsViews=true
  !views.props.allItemsViewProps ||
  // allItemsViews depends on hidden's value
  !views.props.allItemsViewProps.hidden));
  (0, _react.useEffect)(() => {
    if (exportButton) {
      state.featuredComponents.exportButton.set(exportButton);
    }
    return () => {
      state.featuredComponents.exportButton.set(null);
    };
  }, [exportButton]);
  (0, _react.useEffect)(() => {
    if (importButton) {
      state.featuredComponents.importButton.set(importButton);
    }
    return () => {
      state.featuredComponents.importButton.set(null);
    };
  }, [importButton]);
  const tabsFilterProps = (0, _react.useMemo)(() => {
    return tabs != null && tabs.props ? {
      filter: tabs.props.filter
    } : undefined;
  }, [tabs]);
  (0, _react.useEffect)(() => (0, _mobx.reaction)(() => pageState.hasHeader, hasHeader => {
    if (hasHeader) {
      state.minimizedHeaderHeight = 66;
    }
  }, {
    fireImmediately: true
  }), []);
  (0, _useSyncTotal.useSyncTotal)(state.collection);
  (0, _react.useEffect)(() => {
    (0, _mobx.runInAction)(() => {
      state.dragAndDropCategories = dragAndDropCategories;
    });
    // We accept this prop once on purpose because it's array it changes on every render. We expose a setter `set dragAndDropCategories` instead.
  }, []);
  (0, _react.useState)(() => views == null ? void 0 : views.type.setViewsState(state, views == null ? void 0 : views.props));
  (0, _react.useEffect)((0, _mobx.action)(() => {
    state.viewsProps = views == null ? void 0 : views.props;
  }), [views]);
  (0, _react.useState)(() => {
    var _props$AddApplyFilter, _props$title, _props$title2;
    (_props$AddApplyFilter = props.AddApplyFiltersButton) == null || _props$AddApplyFilter.setStateObject(state);
    state.featuresAvailability = {
      isDisablingSearch: search === false,
      isUsingFilters: !!filters,
      isUsingDragAndDrop,
      isUsingBulkActionsToolbar,
      isUsingHorizontalScroll,
      isUsingFolders,
      isUsingTableVirtualization,
      isUsingTags: !!props.tags,
      isUsingTabs: !!tabs,
      isUsingSummaryRow: !!summaryBar,
      isUsingSecondaryActions: !!secondaryActions,
      isUsingMoreActions: !!moreActions
    };
    state.additionalFeaturesAvailability = {
      isUsingTableSubtitle: !!((_props$title = props.title) != null && _props$title.props.subtitle),
      isUsingCounterInHeader: !!((_props$title2 = props.title) != null && _props$title2.props.showTotal),
      isUsingSelectAllLimitation: !!props.maxSelection,
      isUsingErrorMessage: !!(props.errorState ?? props.renderError),
      isUsingAllItemsView: getIsUsingAllItemsView(),
      isUsingCustomEmptyState: !!props.emptyState,
      isUsingCustomNoResultsState: !!props.noResultsState,
      isUsingPrimaryActionCta: !!props.primaryActionButton,
      isUsingExpandCollapse: !!props.isUsingExpandCollapse
    };
  });
  (0, _react.useEffect)((0, _mobx.action)(() => {
    if (tabsFilterProps != null && tabsFilterProps.filter) {
      state.tabsFilterProps = tabsFilterProps;
      tabsFilterProps.filter.persistent = true;
    }
  }), [tabsFilterProps]);
  (0, _react.useEffect)(() => {
    (0, _mobx.runInAction)(() => {
      state.bulk.maxSelection = maxSelection;
    });
  }, [maxSelection]);
  return state;
}
//# sourceMappingURL=useToolbarCollectionSyncProps.js.map