'use strict';
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useEffect } from 'react';
import { useAsyncEffect } from '../../../../fd-toolbox/hooks/use-async-effect.js';
import { ViewSheet } from '../../view/ViewSheet.js';
import { tableCustomActionTypes, internalTableColumnTypes } from '../table-models.js';
import { onInitTableHeader, setOverlayVisibility, onEditActionButtonClick } from './TableHeaderActionCell.funcs.js';
import { TableHeaderActionButton } from './TableHeaderCellActionButton.js';
import { navigate } from '../../../../fd-toolbox/redirect/redirect-functions.js';
import { routes } from '../../../../fd-toolbox/routing/routes.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function TableHeaderActionCell({
  cellContext,
  onRefreshItems,
  setIsEditSheetOpen,
  ref
}) {
  const [activePlan, setActivePlan] = useState();
  const [isOverlayVisible, setIsOverlayVisible] = useState(false);
  const customAction = cellContext.tableInstance?.customActions?.find(action => action.actionType === tableCustomActionTypes.create);
  useAsyncEffect(async () => {
    await onInitTableHeader(cellContext, setActivePlan);
  }, []);
  useEffect(() => {
    setOverlayVisibility(cellContext, setIsOverlayVisible, activePlan);
  }, [activePlan, cellContext]);
  if (customAction) {
    return /* @__PURE__ */jsx(TableHeaderActionButton, {
      onClick: () => onEditActionButtonClick(cellContext, customAction, onRefreshItems),
      tooltipText: customAction?.displayName
    });
  }
  return /* @__PURE__ */jsxs("div", {
    ref,
    className: "relative",
    children: [cellContext.internalTableColumn.type === internalTableColumnTypes.customHeaderAction && isOverlayVisible, cellContext.internalTableColumn.type === internalTableColumnTypes.customHeaderAction && !isOverlayVisible && /* @__PURE__ */jsx(TableHeaderActionButton, {
      onClick: () => navigate(routes.contact)
    }), cellContext.internalTableColumn.type !== internalTableColumnTypes.customHeaderAction && !isOverlayVisible && /* @__PURE__ */jsx(ViewSheet, {
      resourceName: cellContext.tableInstance.uiResourceMeta?.resourceMeta.resourceName,
      uiResourceMeta: cellContext.tableInstance.uiResourceMeta,
      onRefreshItems,
      onCloseClick: () => setIsEditSheetOpen(false),
      children: /* @__PURE__ */jsx(TableHeaderActionButton, {
        onClick: () => setIsEditSheetOpen(false)
      })
    })]
  });
}
__name(TableHeaderActionCell, "TableHeaderActionCell");
TableHeaderActionCell.displayName = "TableHeaderActionCell";

export { TableHeaderActionCell };
