import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { u as useViewHandler } from './index-bbdfb97a.js';
import 'react';
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
import ShelfDeleteButton from './index-78fc8323.js';
import { observer } from 'mobx-react-lite';
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
import 'react-dynamic-multi-render';
import '@crystaldesign/diva-core';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/regenerator';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '@babel/runtime/helpers/toConsumableArray';
import '@crystaldesign/diva-utils';
import 'mobx';
import '@crystaldesign/web-planner-logic';
import 'classnames';
import 'qrcode.react';
import '@babel/runtime/helpers/typeof';
import 'flat';
import 'react-switch';
import 'react-countup';

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; }
var GridList = function GridList(_ref) {
  var _item$shelfPlannerGri;
  var item = _ref.item,
    height = _ref.height;
    _ref.width;
    var direction = _ref.direction;
  var grid = (_item$shelfPlannerGri = item.shelfPlannerGrids) === null || _item$shelfPlannerGri === void 0 ? void 0 : _item$shelfPlannerGri[0];
  var viewHandler = useViewHandler();
  if (!grid) return /*#__PURE__*/jsx(Fragment, {});
  var itemHeight = height / grid.Cells.length;
  var cells = [];
  var lastInUse = -1;
  var onBoardNameAdded = false;
  for (var i = 0; i < grid.Cells.length; i++) {
    var cell = grid.Cells[i];
    if (cell.InUse == -1) {
      cells.splice(0, 0, {
        heightCount: 1,
        inUse: cell.InUse,
        index: cell.Index,
        type: undefined,
        isDisabled: cell.IsDisabled,
        isRequired: cell.IsRequired,
        onlyPlaceHolder: cell.OnlyPlaceHolder,
        noMove: cell.NoMove,
        noDelete: cell.NoDelete
      });
    } else if (lastInUse != cell.InUse && lastInUse != cell.OnBoard) {
      var _cell$Article, _cell$Article2;
      lastInUse = cell.InUse;
      onBoardNameAdded = false;
      cells.splice(0, 0, {
        heightCount: 1,
        inUse: cell.InUse,
        index: cell.Index,
        type: cell.Type,
        isDisabled: cell.IsDisabled,
        isRequired: cell.IsRequired,
        articleText: (_cell$Article = cell.Article) === null || _cell$Article === void 0 ? void 0 : _cell$Article.name,
        onlyPlaceHolder: cell.OnlyPlaceHolder,
        noMove: cell.NoMove,
        noDelete: cell.NoDelete,
        basketId: (_cell$Article2 = cell.Article) === null || _cell$Article2 === void 0 ? void 0 : _cell$Article2.basketId
      });
    } else {
      var foundCell = cells.find(function (cell) {
        return cell.inUse == lastInUse;
      });
      if (foundCell) {
        if (lastInUse === cell.OnBoard && !onBoardNameAdded) {
          var _cell$Article3;
          onBoardNameAdded = true;
          foundCell.articleText = ((_cell$Article3 = cell.Article) === null || _cell$Article3 === void 0 ? void 0 : _cell$Article3.name) + '<br/>&<br/>' + foundCell.articleText;
          foundCell.onBoard = true;
        }
        foundCell.heightCount++;
      }
    }
  }
  var onDragEnd = function onDragEnd(result) {
    if (!result.destination) return;
    var sourceIndex = cells[result.source.index].index;
    var destinationIndex = cells[result.destination.index].index;
    if (sourceIndex != destinationIndex) viewHandler.paletteViewHandler.shelfItemMoved(grid, sourceIndex, destinationIndex);
  };
  var getItemStyle = function getItemStyle(isDragging, draggableStyle, item, showNumber) {
    if (direction == 'horizontal') {
      return getItemStyleHorizontal(isDragging, draggableStyle, item, showNumber);
    } else {
      return getItemStyleVertical(isDragging, draggableStyle, item);
    }
  };
  var getItemStyleVertical = function getItemStyleVertical(isDragging, draggableStyle, item) {
    return _objectSpread({
      userSelect: 'none',
      width: 'calc(' + 100 * (item.heightCount / grid.Cells.length) + '% - 1px)',
      wordBreak: 'break-word',
      height: '100%',
      marginRight: '1px',
      background: item.isRequired ? '#FFEBEB' : item.inUse == -1 ? 'none' : isDragging ? 'grey' : 'lightgrey',
      backgroundImage: item.isDisabled && 'linear-gradient(45deg, #ffffff  25%, #c7c7c7 25%, #c7c7c7 50%, #ffffff  50%, #ffffff  75%, #c7c7c7 75%, #c7c7c7 100%)',
      backgroundSize: item.isDisabled && '28.28px 28.28px',
      "float": 'left',
      borderRight: item.inUse == -1 ? '1px solid lightgrey' : 'none',
      boxSizing: 'border-box'
    }, draggableStyle);
  };
  var getItemStyleHorizontal = function getItemStyleHorizontal(isDragging, draggableStyle, item, showNumber) {
    return _objectSpread({
      userSelect: 'none',
      height: itemHeight * item.heightCount - 1,
      marginBottom: '1px',
      fontSize: item.inUse === -1 ? '12px' : undefined,
      lineHeight: item.inUse === -1 ? '2px' : undefined,
      background: item.isRequired ? '#FFEBEB' : item.inUse === -1 ? 'none' : isDragging ? 'grey' : 'lightgrey',
      backgroundImage: item.isDisabled && 'linear-gradient(45deg, #ffffff  25%, #c7c7c7 25%, #c7c7c7 50%, #ffffff  50%, #ffffff  75%, #c7c7c7 75%, #c7c7c7 100%)',
      backgroundSize: item.isDisabled && '28.28px 28.28px',
      borderBottom: item.inUse == -1 ? showNumber ? '1px solid #B5B5B5' : '1px solid lightgrey' : 'none',
      boxSizing: 'border-box'
    }, draggableStyle);
  };
  var getTextStyle = function getTextStyle() {
    if (direction == 'vertikal') {
      return {
        maxHeight: 'calc(100% - 22px)',
        overflow: 'auto'
      };
    }
    return {};
  };
  return /*#__PURE__*/jsx(DragDropContext, {
    onDragEnd: onDragEnd,
    children: /*#__PURE__*/jsx(Droppable, {
      droppableId: "droppable",
      direction: direction == 'vertikal' ? 'horizontal' : 'vertical',
      children: function children(provided, snapshot) {
        return /*#__PURE__*/jsxs("div", _objectSpread(_objectSpread({}, provided.droppableProps), {}, {
          ref: provided.innerRef,
          style: {
            width: '100%',
            height: '100%'
          },
          children: [cells.map(function (item, index) {
            return /*#__PURE__*/jsx(Draggable, {
              index: index,
              isDragDisabled: item.inUse == -1 || item.isDisabled || item.noMove,
              draggableId: index.toString(),
              children: function children(provided, snapshot) {
                return /*#__PURE__*/jsxs("div", _objectSpread(_objectSpread(_objectSpread({
                  onClick: function onClick() {
                    viewHandler.paletteViewHandler.setSelectedShelfAccessoire(item.basketId);
                    viewHandler.paletteViewHandler.clickAddAccessorieOnShelfPlanner(grid, item.index);
                  },
                  ref: provided.innerRef
                }, provided.draggableProps), provided.dragHandleProps), {}, {
                  style: getItemStyle(snapshot.isDragging, provided.draggableProps.style, item, item.index % 5 === 0 && !!index),
                  children: [!!item.articleText && /*#__PURE__*/jsx("div", {
                    style: getTextStyle(),
                    dangerouslySetInnerHTML: {
                      __html: item.articleText
                    }
                  }), item.inUse == -1 && item.index % 5 === 0 && !!index && direction == 'horizontal' && item.index, (item.inUse > -1 && !item.noDelete && !item.isDisabled || item.onBoard) && /*#__PURE__*/jsx(ShelfDeleteButton, {
                    grid: grid,
                    index: item.inUse
                  })]
                }));
              }
            }, index);
          }), provided.placeholder]
        }));
      }
    })
  });
};
var GridList$1 = observer(GridList);

export { GridList$1 as default };
