"use strict";

exports.__esModule = true;
exports.useHeaderActions = useHeaderActions;
var _react = _interopRequireWildcard(require("react"));
var _FieldActions = require("../FieldActions");
var _HeaderActionsPopover = require("./HeaderActionsPopover");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/EditableTable/useHeaderActions.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const FIELD_ACTION_GROUPS = [['editField'], ['makePrimary', 'hideColumn', 'duplicateField', 'duplicateFieldWithContent'], ['sortAscending', 'sortDescending', 'filter'], ['delete']];
function useHeaderActions(state, columns, disabled) {
  const {
    toolbar
  } = state;
  const {
    customColumnsState,
    panels,
    toolbarFiltersState
  } = toolbar;
  const {
    handlers: fieldActionHandlers,
    permissions: fieldActionPermissions
  } = state.fieldActions;
  const {
    isMakingPrimary,
    isDuplicating,
    isDuplicatingWithContent
  } = state.fieldActions;
  return (0, _react.useMemo)(() => {
    if (disabled) {
      return {};
    }
    const result = {};
    const loading = {
      makePrimary: isMakingPrimary,
      duplicateField: isDuplicating,
      duplicateFieldWithContent: isDuplicatingWithContent
    };
    for (const col of columns) {
      const handlers = {};
      if (fieldActionHandlers != null && fieldActionHandlers.makePrimary) {
        handlers.makePrimary = id => state.fieldActions.makePrimary(id);
      }
      if (col.hideable !== false && customColumnsState) {
        const toolbarColumn = toolbar.columns.find(c => c.id === col.id);
        if (toolbarColumn) {
          handlers.hideColumn = () => customColumnsState.onColumnCheckboxToggle(toolbarColumn);
        }
      }
      if (fieldActionHandlers != null && fieldActionHandlers.duplicateField) {
        handlers.duplicateField = id => state.fieldActions.duplicate(id);
      }
      if (fieldActionHandlers != null && fieldActionHandlers.duplicateFieldWithContent) {
        handlers.duplicateFieldWithContent = id => state.fieldActions.duplicateWithContent(id);
      }
      if (col.sortable) {
        handlers.sortAscending = id => state.sort(id, {
          forceDirection: 'asc'
        });
        handlers.sortDescending = id => state.sort(id, {
          forceDirection: 'desc'
        });
      }
      const hasFilter = toolbarFiltersState && state.collection.query.customFilters[col.id] != null;
      if (hasFilter) {
        handlers.filter = () => panels.openSidePanel('filters');
      }
      if (fieldActionHandlers != null && fieldActionHandlers.deleteField) {
        handlers.delete = id => state.fieldActions.confirmDelete(id);
      }
      const groups = (0, _FieldActions.buildFieldActionGroups)({
        column: col,
        permissions: fieldActionPermissions,
        handlers,
        loading,
        groups: FIELD_ACTION_GROUPS
      });
      if (groups.length > 0) {
        result[col.id] = /*#__PURE__*/_react.default.createElement(_HeaderActionsPopover.HeaderActionsPopover, {
          actions: groups,
          dataHook: `header-actions-${col.id}`,
          __self: this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 96,
            columnNumber: 11
          }
        });
      }
    }
    return result;
  }, [state, columns, toolbar, customColumnsState, panels, toolbarFiltersState, disabled, fieldActionHandlers, fieldActionPermissions, isDuplicating, isDuplicatingWithContent, isMakingPrimary]);
}
//# sourceMappingURL=useHeaderActions.js.map