import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { useState, useEffect } from 'react';
import { visibleColumnsHaveWidth } from './TableHead/columnWidthUtilityFunctions.js';

/**
 * The expander column will grow when the fixed layout table grows in order to keep
 * defined column widths. For resizable tables that don't have any initial
 * column widths we have to keep the expander column out of the first
 * render cycle, where column widths are measured, so that it won't be
 * assigned a width (and occupy space).
 * @param {*} hasResize true f the table has resizable columns
 * @param {*} useAutoTableLayoutForResize true for table-layout should be 'auto' instead of 'fixed'
 * @param {array} ordering the table ordering prop that specifies the order and visibility of columns
 * @param {array} columns The table column props*
 * @returns
 */
var useShowExpanderColumn = function useShowExpanderColumn(_ref) {
  var hasResize = _ref.hasResize,
    useAutoTableLayoutForResize = _ref.useAutoTableLayoutForResize,
    ordering = _ref.ordering,
    columns = _ref.columns;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    showDelayedExpander = _useState2[0],
    setShowDelayedExpander = _useState2[1];

  // extra deps will be automatically added by `eslint --fix` and it breaks the tests
  // eslint-disable-next-line react-hooks/exhaustive-deps
  useEffect(function () {
    if (hasResize && !useAutoTableLayoutForResize) {
      setShowDelayedExpander(true);
    }
  });
  var showExpanderImmediately = hasResize && !useAutoTableLayoutForResize && visibleColumnsHaveWidth(ordering, columns);
  return showExpanderImmediately || showDelayedExpander;
};

export { useShowExpanderColumn };
