import _defineProperty from '@babel/runtime/helpers/defineProperty';
import 'core-js/modules/es.array.includes.js';
import 'core-js/modules/es.string.includes.js';
import React__default from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import { EditingStyle, DropLocation } from '../../../utils/DragAndDropUtils.js';
import { settings } from '../../../constants/Settings.js';
import ListTarget, { TargetSize } from './ListTarget.js';

var iotPrefix = 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.createElement("div", {
    role: "button",
    tabIndex: preventRowFocus ? -1 : 0,
    className: classnames("".concat(iotPrefix, "--list-item"), "".concat(iotPrefix, "--list-item__selectable"), _defineProperty({}, "".concat(iotPrefix, "--list-item__selected"), editingStyle ? false : selected), _defineProperty({}, "".concat(iotPrefix, "--list-item__large"), isLargeRow), _defineProperty({}, "".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.createElement("div", {
    className: classnames("".concat(iotPrefix, "--list-item"), _defineProperty(_defineProperty({}, "".concat(iotPrefix, "--list-item__large"), isLargeRow), "".concat(iotPrefix, "--list-item-editable"), editingStyle))
  }, children);
  if (editingStyle) {
    var canNest = editingStyle === EditingStyle.SingleNesting || editingStyle === EditingStyle.MultipleNesting;
    var allowedDropIds = renderDropTargets && getAllowedDropIds && getAllowedDropIds();
    var preventDrop = Array.isArray(allowedDropIds) && !allowedDropIds.includes(id);
    return /*#__PURE__*/React__default.createElement("div", {
      role: "listitem",
      className: classnames("".concat(iotPrefix, "--list-item-editable--drag-container"), _defineProperty({}, "".concat(iotPrefix, "--list-item-editable--dragging"), isDragging)),
      ref: function ref(instance) {
        if (connectDragSource) {
          connectDragSource(instance);
        }
      }
    }, renderDropTargets && !preventDrop && /*#__PURE__*/React__default.createElement("div", {
      className: classnames("".concat(iotPrefix, "--list-item-editable--drop-targets"), _defineProperty({}, "".concat(iotPrefix, "--list-item__large"), isLargeRow))
    },
    // Renders Nested location only if nesting is allowed

    canNest ? /*#__PURE__*/React__default.createElement(ListTarget, {
      id: id,
      targetPosition: DropLocation.Nested,
      targetSize: TargetSize.Full,
      itemWillMove: itemWillMove,
      onItemMoved: onItemMoved
    }) : null, /*#__PURE__*/React__default.createElement(ListTarget, {
      id: id,
      targetPosition: DropLocation.Above,
      itemWillMove: itemWillMove,
      targetSize: canNest ? TargetSize.Third : TargetSize.Half,
      onItemMoved: onItemMoved
    }), /*#__PURE__*/React__default.createElement(ListTarget, {
      id: id,
      targetPosition: DropLocation.Below,
      itemWillMove: itemWillMove,
      targetOverride: expanded ? DropLocation.Nested : null // If item is expanded then the bottom target will nest
      ,
      targetSize: canNest ? TargetSize.Third : TargetSize.Half,
      onItemMoved: onItemMoved
    })), body);
  }
  return body;
};
var ListItemWrapperProps = {
  id: PropTypes.string.isRequired,
  editingStyle: PropTypes.oneOf([EditingStyle.Single, EditingStyle.Multiple, EditingStyle.SingleNesting, EditingStyle.MultipleNesting]),
  connectDragSource: PropTypes.func.isRequired,
  disabled: PropTypes.bool,
  expanded: PropTypes.bool.isRequired,
  isLargeRow: PropTypes.bool.isRequired,
  isSelectable: PropTypes.bool.isRequired,
  isDragging: PropTypes.bool.isRequired,
  onSelect: PropTypes.func.isRequired,
  renderDropTargets: PropTypes.bool.isRequired,
  selected: PropTypes.bool.isRequired,
  children: PropTypes.node.isRequired,
  onItemMoved: PropTypes.func.isRequired,
  itemWillMove: PropTypes.func.isRequired,
  getAllowedDropIds: PropTypes.func,
  preventRowFocus: PropTypes.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;

export { ListItemWrapper$1 as default };
