var _excluded = ["id", "icon", "background", "handleAction", "tooltipText", "confirmation", "confirmationTitle", "confirmationMessage", "handleDecline", "confirmLabel", "declineLabel", "actionButtonDirection", "testPrefix", "dataGa", "disabled", "visible", "currentRow", "selectedRows", "disabledTooltipText", "iconColor", "flavour", "CustomUIAction", "label", "TooltipComponent", "ref"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React, { useState } from "react";
import Tooltip from "../../drops/tooltip";
import Flex from "../../templates/flex";
import { ConfirmationDialog } from "../../confirmation-dialog";
import { Button, IconButton } from "../../button";
var Action = function Action(_ref) {
  var id = _ref.id,
    icon = _ref.icon,
    background = _ref.background,
    handleAction = _ref.handleAction,
    tooltipText = _ref.tooltipText,
    confirmation = _ref.confirmation,
    confirmationTitle = _ref.confirmationTitle,
    confirmationMessage = _ref.confirmationMessage,
    handleDecline = _ref.handleDecline,
    confirmLabel = _ref.confirmLabel,
    declineLabel = _ref.declineLabel,
    actionButtonDirection = _ref.actionButtonDirection,
    testPrefix = _ref.testPrefix,
    dataGa = _ref.dataGa,
    disabled = _ref.disabled,
    visible = _ref.visible,
    currentRow = _ref.currentRow,
    selectedRows = _ref.selectedRows,
    disabledTooltipText = _ref.disabledTooltipText,
    iconColor = _ref.iconColor,
    _ref$flavour = _ref.flavour,
    flavour = _ref$flavour === void 0 ? "borderless" : _ref$flavour,
    CustomUIAction = _ref.CustomUIAction,
    label = _ref.label,
    _ref$TooltipComponent = _ref.TooltipComponent,
    TooltipComponent = _ref$TooltipComponent === void 0 ? Tooltip : _ref$TooltipComponent,
    ref = _ref.ref,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = useState(false),
    isConfirmationOpen = _useState[0],
    setConfirmationOpen = _useState[1];
  if (visible === false) return null;
  var onActionClicked = function onActionClicked(e) {
    e.stopPropagation();
    if (confirmation || CustomUIAction) {
      setConfirmationOpen(true);
      return;
    }
    handleAction();
  };
  var onActionDeclined = function onActionDeclined() {
    setConfirmationOpen(false);
    handleDecline == null || handleDecline();
  };
  var onActionConfirmed = function onActionConfirmed() {
    setConfirmationOpen(false);
    handleAction == null || handleAction();
  };
  var Component = label ? Button : IconButton;
  return /*#__PURE__*/React.createElement(React.Fragment, null, isConfirmationOpen && CustomUIAction && /*#__PURE__*/React.createElement(CustomUIAction, {
    handleAction: handleAction(),
    onClose: function onClose() {
      return setConfirmationOpen(false);
    },
    data: (currentRow == null ? void 0 : currentRow.original) || selectedRows
  }), isConfirmationOpen && !CustomUIAction && /*#__PURE__*/React.createElement(ConfirmationDialog, {
    actionButtonDirection: actionButtonDirection,
    declineLabel: declineLabel,
    confirmLabel: confirmLabel,
    title: typeof confirmationTitle === "function" ? confirmationTitle(currentRow == null ? void 0 : currentRow.original, selectedRows) : confirmationTitle,
    message: typeof confirmationMessage === "function" ? confirmationMessage(currentRow == null ? void 0 : currentRow.original, selectedRows) : confirmationMessage,
    handleDecline: onActionDeclined,
    handleConfirm: onActionConfirmed
  }), /*#__PURE__*/React.createElement(TooltipComponent, {
    content: disabled ? disabledTooltipText : tooltipText
  }, /*#__PURE__*/React.createElement(Flex, {
    ref: ref,
    alignItems: "center",
    justifyContent: "center",
    _hover: {
      background: disabled || label ? null : "borderSecondary"
    },
    cursor: disabled ? "auto" : "pointer",
    key: id,
    round: true,
    background: label ? null : background
  }, /*#__PURE__*/React.createElement(Component, _extends({
    iconSize: "small",
    "data-testid": "netdata-table-action-" + id + testPrefix,
    "data-ga": dataGa,
    disabled: disabled,
    onClick: onActionClicked,
    icon: icon,
    flavour: flavour,
    iconColor: iconColor,
    label: label,
    padding: [0.5]
  }, rest)))));
};
export default Action;