import _createClass from '@babel/runtime/helpers/createClass';
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
import _inherits from '@babel/runtime/helpers/inherits';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.symbol.description.js';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.array.join.js';
import 'core-js/modules/es.array.map.js';
import 'core-js/modules/es.reflect.construct.js';
import React__default, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import { settings } from '../../constants/Settings.js';
import Button from '../Button/Button.js';
import { handleSpecificKeyDown } from '../../utils/componentUtilityFunctions.js';
import { keyboardKeys } from '../../constants/KeyCodeConstants.js';

function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var prefix = settings.prefix,
  iotPrefix = settings.iotPrefix;
var ResourceList = /*#__PURE__*/function (_Component) {
  function ResourceList(props) {
    var _this;
    _classCallCheck(this, ResourceList);
    _this = _callSuper(this, ResourceList, [props]);
    _defineProperty(_this, "render", function () {
      var _this$props = _this.props,
        design = _this$props.design,
        data = _this$props.data,
        customAction = _this$props.customAction,
        currentItemId = _this$props.currentItemId,
        onRowClick = _this$props.onRowClick,
        extraContent = _this$props.extraContent,
        testId = _this$props.testId;
      var checkboxCell = /*#__PURE__*/React__default.createElement("div", {
        className: "".concat(prefix, "--structured-list-td"),
        style: {
          verticalAlign: 'middle'
        }
      }, /*#__PURE__*/React__default.createElement("svg", {
        className: "".concat(prefix, "--structured-list-svg"),
        width: "16",
        height: "16",
        viewBox: "0 0 16 16"
      }, /*#__PURE__*/React__default.createElement("path", {
        d: "M8 16A8 8 0 1 1 8 0a8 8 0 0 1 0 16zm3.646-10.854L6.75 10.043 4.354 7.646l-.708.708 3.104 3.103 5.604-5.603-.708-.708z",
        fillRule: "evenodd"
      })));
      var CustomActionContent = function CustomActionContent(_ref) {
        var rowId = _ref.rowId;
        return /*#__PURE__*/React__default.createElement("div", {
          className: "".concat(prefix, "--structured-list-td ").concat(prefix, "--structured-list-content--nowrap"),
          role: "presentation"
        }, /*#__PURE__*/React__default.createElement(Button, {
          kind: "ghost",
          renderIcon: customAction.icon,
          onClick: function onClick() {
            return customAction.onClick(rowId);
          },
          testId: "".concat(testId, "-custom-action-").concat(rowId)
        }, customAction.label));
      };
      var listContent = data.map(function (_ref2, idx) {
        var id = _ref2.id,
          title = _ref2.title,
          description = _ref2.description;
        var activeItem = id === currentItemId;
        return (
          /*#__PURE__*/
          // eslint-disable-next-line jsx-a11y/label-has-for, jsx-a11y/label-has-associated-control, jsx-a11y/no-noninteractive-element-interactions
          React__default.createElement("label", {
            key: id,
            "aria-label": title,
            className: "".concat(activeItem ? "".concat(prefix, "--structured-list-row--selected") : '', " ").concat(prefix, "--structured-list-row"),
            tabIndex: customAction ? -1 : idx,
            onClick: onRowClick ? function () {
              return onRowClick(id);
            } : null,
            onKeyDown: onRowClick ? handleSpecificKeyDown([keyboardKeys.ENTER], function () {
              return onRowClick(id);
            }) : undefined,
            "data-testid": "".concat(testId, "-row-").concat(id)
          }, extraContent ? /*#__PURE__*/React__default.createElement("div", {
            className: "".concat(prefix, "--structured-list-td")
          }, extraContent[idx]) : undefined, design === 'inline' ? /*#__PURE__*/React__default.createElement(Fragment, null, /*#__PURE__*/React__default.createElement("div", {
            className: "".concat(prefix, "--structured-list-td")
          }, /*#__PURE__*/React__default.createElement("strong", null, title), /*#__PURE__*/React__default.createElement("div", {
            className: "".concat(iotPrefix, "--resource-list__inline-div ").concat(prefix, "--structured-list-td")
          }, description))) : /*#__PURE__*/React__default.createElement(Fragment, null, /*#__PURE__*/React__default.createElement("div", {
            className: "".concat(prefix, "--structured-list-td ").concat(prefix, "--structured-list-content--nowrap")
          }, /*#__PURE__*/React__default.createElement("strong", null, title)), /*#__PURE__*/React__default.createElement("div", {
            className: "".concat(prefix, "--structured-list-td")
          }, description)), customAction ? /*#__PURE__*/React__default.createElement(CustomActionContent, {
            rowId: id
          }) : /*#__PURE__*/React__default.createElement(Fragment, null, /*#__PURE__*/React__default.createElement("input", {
            tabIndex: -1,
            className: "".concat(prefix, "--structured-list-input"),
            value: title,
            type: "radio",
            title: title,
            checked: activeItem,
            readOnly: true
          }), checkboxCell))
        );
      });
      return /*#__PURE__*/React__default.createElement("section", {
        "data-testid": testId,
        className: ["".concat(iotPrefix, "--resource-list"), "".concat(prefix, "--structured-list"), "".concat(prefix, "--structured-list--border"), "".concat(prefix, "--structured-list--selection")].join(' ')
      }, /*#__PURE__*/React__default.createElement("div", {
        className: "".concat(prefix, "--structured-list-tbody")
      }, listContent));
    });
    _this.state = {};
    return _this;
  }
  _inherits(ResourceList, _Component);
  return _createClass(ResourceList);
}(Component);
_defineProperty(ResourceList, "propTypes", {
  /** Component row design */
  design: PropTypes.oneOf(['normal', 'inline']),
  /** Array of data - table content */
  data: PropTypes.arrayOf(PropTypes.shape({
    id: PropTypes.string.isRequired,
    title: PropTypes.string.isRequired,
    description: PropTypes.string.isRequired
  })).isRequired,
  /** Callback for when row is clicked */
  onRowClick: PropTypes.func,
  /** Object for custom action column */
  customAction: PropTypes.shape({
    onClick: PropTypes.func.isRequired,
    label: PropTypes.string.isRequired,
    icon: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
  }),
  /** Current item selected id */
  currentItemId: PropTypes.string,
  /** Extra content element */
  extraContent: PropTypes.arrayOf(PropTypes.node),
  testId: PropTypes.string
});
_defineProperty(ResourceList, "defaultProps", {
  design: 'normal',
  onRowClick: function onRowClick() {},
  currentItemId: null,
  extraContent: null,
  customAction: null,
  testId: 'resource-list'
});
ResourceList.displayName = 'ResourceList';
ResourceList.__docgenInfo = {
  "description": "",
  "methods": [{
    "name": "render",
    "docblock": null,
    "modifiers": [],
    "params": [],
    "returns": null
  }],
  "displayName": "ResourceList",
  "props": {
    "design": {
      "defaultValue": {
        "value": "'normal'",
        "computed": false
      },
      "description": "Component row design",
      "type": {
        "name": "enum",
        "value": [{
          "value": "'normal'",
          "computed": false
        }, {
          "value": "'inline'",
          "computed": false
        }]
      },
      "required": false
    },
    "onRowClick": {
      "defaultValue": {
        "value": "() => {}",
        "computed": false
      },
      "description": "Callback for when row is clicked",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "currentItemId": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Current item selected id",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "extraContent": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Extra content element",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "node"
        }
      },
      "required": false
    },
    "customAction": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Object for custom action column",
      "type": {
        "name": "shape",
        "value": {
          "onClick": {
            "name": "func",
            "required": true
          },
          "label": {
            "name": "string",
            "required": true
          },
          "icon": {
            "name": "union",
            "value": [{
              "name": "func"
            }, {
              "name": "object"
            }],
            "required": false
          }
        }
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'resource-list'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "data": {
      "description": "Array of data - table content",
      "type": {
        "name": "arrayOf",
        "value": {
          "name": "shape",
          "value": {
            "id": {
              "name": "string",
              "required": true
            },
            "title": {
              "name": "string",
              "required": true
            },
            "description": {
              "name": "string",
              "required": true
            }
          }
        }
      },
      "required": true
    }
  }
};
var ResourceList$1 = ResourceList;

export { ResourceList$1 as default };
