"use strict";

exports.__esModule = true;
exports.useColumnsSyncProps = useColumnsSyncProps;
var _react = require("react");
var _mobx = require("mobx");
var _react2 = require("@wix/bex-core/react");
var _WixPatternsRouterProvider = require("../providers/WixPatternsRouterProvider");
function useColumnsSyncProps(props) {
  const {
    toolbar,
    customColumns,
    columns: tableColumns
  } = props;
  const {
    modalsContainerRef
  } = (0, _react2.usePageContext)();
  const router = (0, _WixPatternsRouterProvider.useWixPatternsRouter)();
  const customColumnsProps = (0, _react.useMemo)(() => {
    if (customColumns == null) {
      return null;
    }
    const {
      onColumnClick
    } = customColumns.props;
    return {
      onColumnClick,
      dragAndDrop: customColumns.type.dragAndDrop
    };
  }, [customColumns]);
  (0, _react.useState)(() => customColumns == null ? void 0 : customColumns.type.setCustomColumnsState({
    toolbar,
    customColumnsProps: {
      dragAndDrop: customColumnsProps == null ? void 0 : customColumnsProps.dragAndDrop
    },
    modalsContainerRef
  }));
  (0, _react.useEffect)(() => {
    (0, _mobx.runInAction)(() => {
      toolbar.customColumnsProps = customColumnsProps;
    });
  }, [customColumnsProps]);
  (0, _react.useEffect)(() => {
    if (router != null && router.isUsingCache) {
      return;
    }
    toolbar._adjustSortWithNewColumns(tableColumns);
  }, [tableColumns.map((e, i) => e.id ?? i).join('')]);
  (0, _react.useEffect)(() => {
    toolbar.setNewColumns(tableColumns);
  }, [tableColumns, customColumnsProps != null, toolbar.customColumnsStateInitialized]);
  const customColumnsDependency = (0, _react.useMemo)(() => JSON.stringify(tableColumns.map(({
    id,
    reorderDisabled,
    hiddenFromCustomColumnsSelection,
    hideable
  }, i) => ({
    id: id ?? i,
    reorderDisabled,
    hiddenFromCustomColumnsSelection,
    hideable
  }))), [tableColumns]);
  (0, _react.useEffect)(() => {
    toolbar._adjustSelectedColumns();
  }, [customColumnsDependency, customColumnsProps != null, toolbar.customColumnsStateInitialized]);
}
//# sourceMappingURL=useColumnsSyncProps.js.map