"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _checkbox = require("../../checkbox");
var _radioButton = _interopRequireDefault(require("../../radio-button"));
var _flex = _interopRequireDefault(require("../../templates/flex"));
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; }
var width = 32;
var Header = function Header(_ref) {
  var table = _ref.table,
    testPrefix = _ref.testPrefix;
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], {
    width: 4
  }, /*#__PURE__*/_react["default"].createElement(_checkbox.Checkbox, {
    "data-testid": "netdata-table-header-checkbox" + testPrefix,
    checked: table.getIsAllRowsSelected(),
    indeterminate: table.getIsSomeRowsSelected(),
    onChange: function onChange(checked) {
      table.getToggleAllRowsSelectedHandler()({
        target: {
          checked: checked
        }
      });
    }
  }));
};
var Cell = function Cell(_ref2) {
  var _row$original, _row$original2, _row$original3, _row$original4, _row$original5;
  var row = _ref2.row,
    singleRowSelection = _ref2.singleRowSelection,
    testPrefix = _ref2.testPrefix;
  if (((_row$original = row.original) == null ? void 0 : _row$original.disabled) === "hidden") return null;
  if (singleRowSelection) return /*#__PURE__*/_react["default"].createElement(_radioButton["default"], {
    "data-testid": "netdata-table-cell-radio-button" + testPrefix,
    checked: !((_row$original2 = row.original) != null && _row$original2.disabled) && row.getIsSelected(),
    name: "netdata-table-row-radio-button-selection",
    onChange: function onChange(e) {
      e == null || e.stopPropagation == null || e.stopPropagation();
      row.getToggleSelectedHandler()({
        target: {
          checked: e.target.checked
        }
      });
    },
    disabled: ((_row$original3 = row.original) == null ? void 0 : _row$original3.disabled) || false
  });
  return /*#__PURE__*/_react["default"].createElement(_checkbox.Checkbox, {
    "data-testid": "netdata-table-cell-checkbox" + testPrefix,
    checked: !((_row$original4 = row.original) != null && _row$original4.disabled) && row.getIsSelected(),
    indeterminate: row.getIsSomeSelected(),
    onChange: function onChange(checked, e) {
      e == null || e.stopPropagation == null || e.stopPropagation();
      row.getToggleSelectedHandler()({
        target: {
          checked: checked
        }
      });
    },
    disabled: ((_row$original5 = row.original) == null ? void 0 : _row$original5.disabled) || false
  });
};
var _default = exports["default"] = function _default(enabled, _ref3) {
  var testPrefix = _ref3.testPrefix,
    tableMeta = _ref3.tableMeta,
    singleRowSelection = _ref3.singleRowSelection;
  return (0, _react.useMemo)(function () {
    return enabled ? {
      id: "checkbox",
      headerString: "Selection",
      enableHiding: false,
      enableResizing: false,
      header: function header(_ref4) {
        var table = _ref4.table;
        return !singleRowSelection ? /*#__PURE__*/_react["default"].createElement(Header, {
          table: table,
          testPrefix: testPrefix
        }) : null;
      },
      cell: function cell(_ref5) {
        var row = _ref5.row;
        return /*#__PURE__*/_react["default"].createElement(Cell, {
          row: row,
          singleRowSelection: singleRowSelection,
          testPrefix: testPrefix
        });
      },
      enableColumnFilter: false,
      enableSorting: false,
      size: width,
      maxSize: width,
      minSize: width,
      notFlex: true,
      tableMeta: tableMeta
    } : null;
  }, [enabled, testPrefix, tableMeta, singleRowSelection]);
};