"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", "CustomComponent", "label", "TooltipComponent", "ref"];
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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; }
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,
    CustomComponent = _ref.CustomComponent,
    label = _ref.label,
    _ref$TooltipComponent = _ref.TooltipComponent,
    TooltipComponent = _ref$TooltipComponent === void 0 ? _tooltip["default"] : _ref$TooltipComponent,
    ref = _ref.ref,
    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 = CustomComponent || (label ? _button.Button : _button.IconButton);
  var componentProps = (0, _react.useMemo)(function () {
    return _objectSpread(_objectSpread({
      iconSize: "small",
      "data-testid": "netdata-table-action-" + id + testPrefix,
      "data-ga": dataGa,
      disabled: disabled,
      onClick: onActionClicked,
      icon: typeof icon === "function" ? icon(currentRow == null ? void 0 : currentRow.original) : icon,
      flavour: flavour,
      iconColor: iconColor,
      label: label,
      padding: [0.5]
    }, CustomComponent ? {
      data: (currentRow == null ? void 0 : currentRow.original) || selectedRows
    } : {}), rest);
  }, [id, testPrefix, dataGa, disabled, onActionClicked, icon, flavour, iconColor, label, currentRow == null ? void 0 : currentRow.original, selectedRows, CustomComponent, rest]);
  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, componentProps))));
};
var _default = exports["default"] = Action;