"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _tooltip = _interopRequireDefault(require("../../drops/tooltip"));
var _flex = _interopRequireDefault(require("../../templates/flex"));
var _confirmationDialog = require("../../confirmation-dialog");
var _button = require("../../button");
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"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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 (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
var Action = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, 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["default"] : _ref$TooltipComponent,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = (0, _react.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.Button : _button.IconButton;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, isConfirmationOpen && CustomUIAction && /*#__PURE__*/_react["default"].createElement(CustomUIAction, {
    handleAction: handleAction(),
    onClose: function onClose() {
      return setConfirmationOpen(false);
    },
    data: (currentRow == null ? void 0 : currentRow.original) || selectedRows
  }), isConfirmationOpen && !CustomUIAction && /*#__PURE__*/_react["default"].createElement(_confirmationDialog.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["default"].createElement(TooltipComponent, {
    content: disabled ? disabledTooltipText : tooltipText
  }, /*#__PURE__*/_react["default"].createElement(_flex["default"], {
    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["default"].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)))));
});
var _default = exports["default"] = Action;