'use strict';
import { c } from 'react/compiler-runtime';
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { Button } from '../../../fd-component-toolbox/button/Button.js';
import { Icon, iconSizes, iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { ViewSheet } from '../../view/ViewSheet.js';
import { YesOrNoDialog } from '../../../fd-component-toolbox/dialog/YesOrNoDialog.js';
import { messages } from '../../../../fd-toolbox-core/constants/messages-constants.js';
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem } from '../../../fd-component-toolbox/dropdown-menu/DropdownMenu.js';
import { internalTableColumnTypes, tableCustomActionPositions, tableCustomActionTypes } from '../table-models.js';
import { tableActionTypes } from '../../../../fd-toolbox/enums/enums.js';
import { useState } from 'react';
import { TableCustomAction } from '../table-custom-action/TableCustomAction.js';
import { FadeOutText } from '../../../fd-component-toolbox/fade-out-text/FadeOutText.js';
import { getCellId } from './BodyCell.funcs.js';
import { tableElementSuffixes } from '../../../fd-component-toolbox/constants/table-element-suffixes.js';
import { CustomActionLink } from './CustomActionLink.js';
import { onCustomAction, shouldShowActionHelper, handleIconActionClick, handleTextAction } from './ActionBodyCell.funcs.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function ActionBodyCell(t0) {
  const $ = c(13);
  const {
    cellContext,
    actionType,
    shouldShowEditButton,
    shouldShowViewButton,
    shouldShowDeleteButton,
    viewCellId,
    hovered,
    onRefreshItems,
    onViewClick,
    onDialogResponse
  } = t0;
  const [dropdownOpen, setDropdownOpen] = useState(false);
  const shouldShowCustomActions = !!(cellContext.tableInstance.customActions?.length && (cellContext.internalTableColumn.type === internalTableColumnTypes.action || cellContext.internalTableColumn.type === internalTableColumnTypes.customHeaderAction));
  let t1;
  if ($[0] !== actionType || $[1] !== cellContext || $[2] !== dropdownOpen || $[3] !== hovered || $[4] !== onDialogResponse || $[5] !== onRefreshItems || $[6] !== onViewClick || $[7] !== shouldShowCustomActions || $[8] !== shouldShowDeleteButton || $[9] !== shouldShowEditButton || $[10] !== shouldShowViewButton || $[11] !== viewCellId) {
    t1 = jsxs(Fragment, {
      children: [actionType === tableActionTypes.minimal && (shouldShowEditButton || shouldShowViewButton || shouldShowDeleteButton) && jsxs(DropdownMenu, {
        open: dropdownOpen,
        onOpenChange: setDropdownOpen,
        children: [jsx(DropdownMenuTrigger, {
          render: jsx(Button, {
            size: "icon",
            variant: "ghost",
            className: cn("size-5 p-1", hovered || dropdownOpen ? "animate-fade-in" : "animate-fade-out"),
            children: jsx(Icon, {
              name: iconNames.dotsVerticalIcon,
              size: iconSizes.medium
            })
          })
        }), jsxs(DropdownMenuContent, {
          align: "start",
          children: [shouldShowViewButton && jsx(ViewSheet, {
            id: cellContext.row.item.id,
            resourceName: cellContext.tableInstance.uiResourceMeta?.resourceMeta.resourceName,
            onRefreshItems,
            onEditClick: onViewClick,
            onCloseClick: setDropdownOpen,
            children: jsxs(DropdownMenuItem, {
              id: viewCellId,
              onSelect: _temp,
              children: [jsx(Icon, {
                name: iconNames.visibility
              }), jsx("span", {
                children: "View"
              })]
            })
          }), !cellContext.tableInstance.hideDeleteButton && shouldShowDeleteButton && jsx(YesOrNoDialog, {
            title: messages.deleteConfirmation,
            onResponse: onDialogResponse,
            onCloseClick: setDropdownOpen,
            asTriggerChild: true,
            trigger: jsxs(DropdownMenuItem, {
              onSelect: _temp2,
              children: [jsx(Icon, {
                name: iconNames.trash
              }), jsx("span", {
                children: "Delete"
              })]
            })
          }), shouldShowCustomActions && cellContext.tableInstance.customActions.map(item => jsx(DropdownMenuItem, {
            onSelect: _temp3,
            children: jsx(TableCustomAction, {
              onAction: result => onCustomAction(result, onRefreshItems),
              customAction: item,
              cellContext
            })
          }, `customAction_${item.displayName}`))]
        })]
      }), actionType === tableActionTypes.default && jsxs("div", {
        className: "flex w-full items-center justify-center",
        children: [jsx(CustomActionValue, {
          cellContext,
          shouldShowCustomActions,
          shouldShowAction: action => shouldShowActionHelper(action, cellContext, shouldShowCustomActions),
          onCustomBodyCellAction: result_0 => onCustomAction(result_0, onRefreshItems)
        }), (cellContext.internalTableColumn.actionPosition === tableCustomActionPositions.default || !cellContext.internalTableColumn.actionPosition) && jsxs(DropdownMenu, {
          open: dropdownOpen,
          onOpenChange: setDropdownOpen,
          children: [jsx(DropdownMenuTrigger, {
            render: jsx(Button, {
              id: getCellId(cellContext.row.index, cellContext, tableElementSuffixes.actionsMenu),
              size: "icon",
              variant: "ghost",
              className: "size-5 p-1",
              children: jsx(Icon, {
                name: iconNames.dotsVerticalIcon,
                size: iconSizes.medium
              })
            })
          }), jsxs(DropdownMenuContent, {
            align: "end",
            children: [!cellContext.tableInstance.hideDeleteButton && shouldShowDeleteButton && jsx(YesOrNoDialog, {
              title: messages.deleteConfirmation,
              onResponse: onDialogResponse,
              onCloseClick: setDropdownOpen,
              asTriggerChild: true,
              trigger: jsxs(DropdownMenuItem, {
                onSelect: _temp4,
                children: [jsx(Icon, {
                  name: iconNames.trash
                }), jsx("span", {
                  children: "Delete"
                })]
              })
            }), shouldShowCustomActions && cellContext.tableInstance.customActions.filter(_temp5).map(item_0 => jsx(DropdownMenuItem, {
              onSelect: _temp6,
              children: jsx(TableCustomAction, {
                onAction: result_1 => onCustomAction(result_1, onRefreshItems),
                customAction: item_0,
                cellContext
              })
            }, `customAction_${item_0.displayName}`))]
          })]
        })]
      })]
    });
    $[0] = actionType;
    $[1] = cellContext;
    $[2] = dropdownOpen;
    $[3] = hovered;
    $[4] = onDialogResponse;
    $[5] = onRefreshItems;
    $[6] = onViewClick;
    $[7] = shouldShowCustomActions;
    $[8] = shouldShowDeleteButton;
    $[9] = shouldShowEditButton;
    $[10] = shouldShowViewButton;
    $[11] = viewCellId;
    $[12] = t1;
  } else {
    t1 = $[12];
  }
  return t1;
}
function _temp6(e_3) {
  return e_3.preventDefault();
}
function _temp5(action_0) {
  return action_0.position === tableCustomActionPositions.dropdown;
}
function _temp4(e_2) {
  return e_2.preventDefault();
}
function _temp3(e_1) {
  return e_1.preventDefault();
}
function _temp2(e_0) {
  return e_0.preventDefault();
}
function _temp(e) {
  return e.preventDefault();
}
__name(ActionBodyCell, "ActionBodyCell");
function CustomActionValue({
  cellContext,
  shouldShowCustomActions,
  shouldShowAction,
  onCustomBodyCellAction
}) {
  if (shouldShowCustomActions && cellContext.internalTableColumn.actionType === tableCustomActionTypes.icon) {
    return cellContext.tableInstance.customActions.filter(action => shouldShowAction(action) && action.actionType === tableCustomActionTypes.icon).map(action => /* @__PURE__ */jsx(Button, {
      size: "lg",
      variant: "ghost",
      className: "size-7 p-1",
      onClick: () => handleIconActionClick(action, cellContext, onCustomBodyCellAction),
      tooltipText: action.displayName,
      children: action.icon && /* @__PURE__ */jsx(Icon, {
        name: action.icon,
        className: cn("size-8 font-bold", action.getDynamicClassName?.(cellContext))
      })
    }, `customAction_button_${action.displayName}`));
  } else if (cellContext.internalTableColumn.actionType === tableCustomActionTypes.text) {
    return /* @__PURE__ */jsx("div", {
      className: "w-full flex-col",
      children: cellContext.tableInstance.customActions.filter(action => action.actionType === tableCustomActionTypes.text).map(action => /* @__PURE__ */jsx(FadeOutText, {
        className: "text-primary hover:text-primary-hover cursor-pointer justify-center text-center select-none hover:underline",
        onTooltipClick: () => handleTextAction(action, cellContext, onCustomBodyCellAction),
        children: action.displayName
      }, action.displayName))
    });
  } else if (cellContext.internalTableColumn.actionType === tableCustomActionTypes.link) {
    return /* @__PURE__ */jsx("div", {
      className: "w-full flex-col",
      children: cellContext.tableInstance.customActions.filter(action => action.actionType === tableCustomActionTypes.link).map(action => /* @__PURE__ */jsx(FadeOutText, {
        className: "text-primary hover:text-primary-hover cursor-pointer justify-center text-center select-none hover:underline",
        onTooltipClick: () => handleTextAction(action, cellContext, onCustomBodyCellAction),
        children: action.getDynamicLink ? /* @__PURE__ */jsx(CustomActionLink, {
          text: action.displayName,
          dynamicLink: action.getDynamicLink,
          cellContext
        }) : action.displayName
      }, action.displayName))
    });
  }
}
__name(CustomActionValue, "CustomActionValue");

export { ActionBodyCell };
