'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('core-js/modules/es.array.includes.js');
require('core-js/modules/es.string.includes.js');
var React = require('react');
var classnames = require('classnames');
var PropTypes = require('prop-types');
var DragAndDropUtils = require('../../../utils/DragAndDropUtils.js');
var Settings = require('../../../constants/Settings.js');
var ListTarget = require('./ListTarget.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var React__default = /*#__PURE__*/_interopDefault(React);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);

var iotPrefix = Settings.settings.iotPrefix;
var ListItemWrapper = function ListItemWrapper(_ref) {
  var id = _ref.id,
    editingStyle = _ref.editingStyle,
    expanded = _ref.expanded,
    isSelectable = _ref.isSelectable,
    itemWillMove = _ref.itemWillMove,
    onItemMoved = _ref.onItemMoved,
    onSelect = _ref.onSelect,
    selected = _ref.selected,
    isDragging = _ref.isDragging,
    renderDropTargets = _ref.renderDropTargets,
    getAllowedDropIds = _ref.getAllowedDropIds,
    isLargeRow = _ref.isLargeRow,
    children = _ref.children,
    connectDragSource = _ref.connectDragSource,
    disabled = _ref.disabled,
    preventRowFocus = _ref.preventRowFocus;
  var body = isSelectable && !disabled ? /*#__PURE__*/React__default.default.createElement("div", {
    role: "button",
    tabIndex: preventRowFocus ? -1 : 0,
    className: classnames__default.default("".concat(iotPrefix, "--list-item"), "".concat(iotPrefix, "--list-item__selectable"), _defineProperty__default.default({}, "".concat(iotPrefix, "--list-item__selected"), editingStyle ? false : selected), _defineProperty__default.default({}, "".concat(iotPrefix, "--list-item__large"), isLargeRow), _defineProperty__default.default({}, "".concat(iotPrefix, "--list-item-editable"), editingStyle)),
    "data-testid": selected ? 'list-item__selected' : null,
    onKeyPress: function onKeyPress(_ref2) {
      var key = _ref2.key;
      return key === 'Enter' && onSelect(id);
    },
    onClick: function onClick() {
      onSelect(id);
    }
  }, children) : /*#__PURE__*/React__default.default.createElement("div", {
    className: classnames__default.default("".concat(iotPrefix, "--list-item"), _defineProperty__default.default(_defineProperty__default.default({}, "".concat(iotPrefix, "--list-item__large"), isLargeRow), "".concat(iotPrefix, "--list-item-editable"), editingStyle))
  }, children);
  if (editingStyle) {
    var canNest = editingStyle === DragAndDropUtils.EditingStyle.SingleNesting || editingStyle === DragAndDropUtils.EditingStyle.MultipleNesting;
    var allowedDropIds = renderDropTargets && getAllowedDropIds && getAllowedDropIds();
    var preventDrop = Array.isArray(allowedDropIds) && !allowedDropIds.includes(id);
    return /*#__PURE__*/React__default.default.createElement("div", {
      role: "listitem",
      className: classnames__default.default("".concat(iotPrefix, "--list-item-editable--drag-container"), _defineProperty__default.default({}, "".concat(iotPrefix, "--list-item-editable--dragging"), isDragging)),
      ref: function ref(instance) {
        if (connectDragSource) {
          connectDragSource(instance);
        }
      }
    }, renderDropTargets && !preventDrop && /*#__PURE__*/React__default.default.createElement("div", {
      className: classnames__default.default("".concat(iotPrefix, "--list-item-editable--drop-targets"), _defineProperty__default.default({}, "".concat(iotPrefix, "--list-item__large"), isLargeRow))
    },
    // Renders Nested location only if nesting is allowed

    canNest ? /*#__PURE__*/React__default.default.createElement(ListTarget.default, {
      id: id,
      targetPosition: DragAndDropUtils.DropLocation.Nested,
      targetSize: ListTarget.TargetSize.Full,
      itemWillMove: itemWillMove,
      onItemMoved: onItemMoved
    }) : null, /*#__PURE__*/React__default.default.createElement(ListTarget.default, {
      id: id,
      targetPosition: DragAndDropUtils.DropLocation.Above,
      itemWillMove: itemWillMove,
      targetSize: canNest ? ListTarget.TargetSize.Third : ListTarget.TargetSize.Half,
      onItemMoved: onItemMoved
    }), /*#__PURE__*/React__default.default.createElement(ListTarget.default, {
      id: id,
      targetPosition: DragAndDropUtils.DropLocation.Below,
      itemWillMove: itemWillMove,
      targetOverride: expanded ? DragAndDropUtils.DropLocation.Nested : null // If item is expanded then the bottom target will nest
      ,
      targetSize: canNest ? ListTarget.TargetSize.Third : ListTarget.TargetSize.Half,
      onItemMoved: onItemMoved
    })), body);
  }
  return body;
};
var ListItemWrapperProps = {
  id: PropTypes__default.default.string.isRequired,
  editingStyle: PropTypes__default.default.oneOf([DragAndDropUtils.EditingStyle.Single, DragAndDropUtils.EditingStyle.Multiple, DragAndDropUtils.EditingStyle.SingleNesting, DragAndDropUtils.EditingStyle.MultipleNesting]),
  connectDragSource: PropTypes__default.default.func.isRequired,
  disabled: PropTypes__default.default.bool,
  expanded: PropTypes__default.default.bool.isRequired,
  isLargeRow: PropTypes__default.default.bool.isRequired,
  isSelectable: PropTypes__default.default.bool.isRequired,
  isDragging: PropTypes__default.default.bool.isRequired,
  onSelect: PropTypes__default.default.func.isRequired,
  renderDropTargets: PropTypes__default.default.bool.isRequired,
  selected: PropTypes__default.default.bool.isRequired,
  children: PropTypes__default.default.node.isRequired,
  onItemMoved: PropTypes__default.default.func.isRequired,
  itemWillMove: PropTypes__default.default.func.isRequired,
  getAllowedDropIds: PropTypes__default.default.func,
  preventRowFocus: PropTypes__default.default.bool.isRequired
};
ListItemWrapper.propTypes = ListItemWrapperProps;
ListItemWrapper.defaultProps = {
  getAllowedDropIds: null,
  editingStyle: null,
  disabled: false
};
ListItemWrapper.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "ListItemWrapper",
  "props": {
    "getAllowedDropIds": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "editingStyle": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "enum",
        "value": [{
          "value": "EditingStyle.Single",
          "computed": true
        }, {
          "value": "EditingStyle.Multiple",
          "computed": true
        }, {
          "value": "EditingStyle.SingleNesting",
          "computed": true
        }, {
          "value": "EditingStyle.MultipleNesting",
          "computed": true
        }]
      },
      "required": false
    },
    "disabled": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "id": {
      "description": "",
      "type": {
        "name": "string"
      },
      "required": true
    },
    "connectDragSource": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "expanded": {
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "isLargeRow": {
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "isSelectable": {
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "isDragging": {
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "onSelect": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "renderDropTargets": {
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "selected": {
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "children": {
      "description": "",
      "type": {
        "name": "node"
      },
      "required": true
    },
    "onItemMoved": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "itemWillMove": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "preventRowFocus": {
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": true
    }
  }
};
var ListItemWrapper$1 = ListItemWrapper;

module.exports = ListItemWrapper$1;
