var _excluded = ["id", "handleAction", "isDisabled", "isVisible", "dataGa", "disabledTooltipText", "tooltipText"];
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; }
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); }
import React, { useMemo } from "react";
import Flex from "../../templates/flex";
import Action from "../components/action";
export var supportedRowActions = {
  "delete": {
    icon: "trashcan",
    confirmation: true,
    tooltipText: "Delete",
    confirmationTitle: "Delete Row",
    confirmationMessage: "You are about to delete a row, are you sure?",
    confirmLabel: "Yes",
    declineLabel: "No",
    actionButtonDirection: "reverse",
    disabledTooltipText: "Delete is disabled"
  },
  edit: {
    icon: "pencilOutline",
    confirmation: false,
    tooltipText: "Edit"
  },
  replace: {
    icon: "refresh",
    confirmation: true,
    tooltipText: "Replace",
    confirmationTitle: "Replace Row",
    confirmationMessage: "You are about to replace a row, are you sure you want to continue?",
    confirmLabel: "Yes",
    declineLabel: "No",
    actionButtonDirection: "reverse"
  },
  info: {
    icon: "information",
    confirmation: false,
    tooltipText: "Information"
  },
  toggleAlarm: {
    icon: "alarm_off",
    confirmation: false,
    tooltipText: "Turn of Alarms"
  },
  userSettings: {
    icon: "user",
    confirmation: false,
    tooltipText: "User Settings"
  },
  remove: {
    icon: "removeNode",
    confirmation: true,
    actionButtonDirection: "reverse",
    confirmLabel: "Yes",
    declineLabel: "No"
  },
  "goto": {
    icon: "nav_arrow_goto",
    confirmation: false,
    tooltipText: "Go to"
  }
};
export default (function (rowActions, _temp) {
  var _ref = _temp === void 0 ? {} : _temp,
    testPrefix = _ref.testPrefix,
    tableMeta = _ref.tableMeta;
  var availableRowActions = useMemo(function () {
    return Object.keys(rowActions).reduce(function (acc, key) {
      var defaultAction = supportedRowActions[key] || supportedRowActions["delete"];
      var currentAction = rowActions[key];
      acc.push(_objectSpread(_objectSpread({
        id: key
      }, defaultAction), currentAction));
      return acc;
    }, []);
  }, [rowActions]);
  if (availableRowActions.length < 1) return null;
  return useMemo(function () {
    return {
      id: "actions",
      enableHiding: false,
      enableResizing: false,
      header: "Actions",
      cell: function cell(_ref2) {
        var row = _ref2.row,
          table = _ref2.table;
        return /*#__PURE__*/React.createElement(Flex, {
          "data-testid": "action-cell",
          height: "100%",
          gap: 2,
          justifyContent: "end",
          onClick: function onClick(e) {
            return e.stopPropagation();
          }
        }, availableRowActions.map(function (_ref3) {
          var id = _ref3.id,
            _handleAction = _ref3.handleAction,
            isDisabled = _ref3.isDisabled,
            _ref3$isVisible = _ref3.isVisible,
            isVisible = _ref3$isVisible === void 0 ? true : _ref3$isVisible,
            dataGa = _ref3.dataGa,
            disabledTooltipText = _ref3.disabledTooltipText,
            tooltipText = _ref3.tooltipText,
            rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
          return /*#__PURE__*/React.createElement(Action, _extends({}, rest, {
            disabled: typeof isDisabled === "function" ? isDisabled(row.original) : isDisabled,
            visible: typeof isVisible === "function" ? isVisible(row.original) : isVisible,
            dataGa: typeof dataGa === "function" ? dataGa(row.original) : dataGa,
            disabledTooltipText: typeof disabledTooltipText === "function" ? disabledTooltipText(row.original) : disabledTooltipText,
            tooltipText: typeof tooltipText === "function" ? tooltipText(row.original) : tooltipText,
            key: id,
            id: id,
            handleAction: function handleAction() {
              return _handleAction(row.original, table);
            },
            testPrefix: testPrefix,
            currentRow: row
          }));
        }));
      },
      enableColumnFilter: false,
      enableSorting: false,
      tableMeta: tableMeta,
      size: availableRowActions.length * 35 < 60 ? 60 : availableRowActions.length * 35,
      meta: {
        cellStyles: {
          justifyContent: "end"
        }
      },
      notFlex: true
    };
  }, [availableRowActions]);
});