"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactDnd = require("react-dnd");

var _formElements = _interopRequireDefault(require("../form-elements"));

var _ItemTypes = _interopRequireDefault(require("../ItemTypes"));

var _customElement = _interopRequireDefault(require("../form-elements/custom-element"));

var _registry = _interopRequireDefault(require("../stores/registry"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function getCustomElement(item, props) {
  if (!item.component || typeof item.component !== 'function') {
    item.component = _registry.default.get(item.key);

    if (!item.component) {
      console.error(`${item.element} was not registered`);
    }
  }

  return /*#__PURE__*/_react.default.createElement(_customElement.default, _extends({}, props, {
    mutable: false,
    key: `form_${item.id}`,
    data: item
  }));
}

function getElement(item, props) {
  if (!item) return null;
  const Element = item.custom ? () => getCustomElement(item, props) : _formElements.default[item.element || item.key];
  return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement(Element, _extends({}, props, {
    key: `form_${item.id}`,
    data: item
  })));
}

function getStyle(backgroundColor) {
  return {
    border: '1px solid rgba(0,0,0,0.2)',
    minHeight: '2rem',
    minWidth: '12rem',
    width: '100%',
    backgroundColor,
    padding: 0,
    float: 'left'
  };
}

function isContainer(item) {
  if (item.itemType !== _ItemTypes.default.CARD) {
    const {
      data
    } = item;

    if (data) {
      if (data.isContainer) {
        return true;
      }

      if (data.field_name) {
        return data.field_name.indexOf('_col_row') > -1;
      }
    }
  }

  return false;
}

const Dustbin = /*#__PURE__*/_react.default.forwardRef(({
  greedy,
  isOver,
  isOverCurrent,
  connectDropTarget,
  items,
  col,
  getDataById,
  ...rest
}, ref) => {
  const item = getDataById(items[col]);
  (0, _react.useImperativeHandle)(ref, () => ({
    onDrop: () =>
    /* dropped */
    {// const { data } = dropped;
      // console.log('onDrop', data);
    }
  }), []);
  let backgroundColor = 'rgba(0, 0, 0, .03)';

  if (isOverCurrent || isOver && greedy) {
    backgroundColor = 'darkgreen';
  }

  const element = getElement(item, rest); // console.log('accepts, canDrop', accepts, canDrop);

  return connectDropTarget( /*#__PURE__*/_react.default.createElement("div", {
    style: getStyle(backgroundColor)
  }, element));
});

var _default = (0, _reactDnd.DropTarget)(props => props.accepts, {
  drop(props, monitor, component) {
    if (!component) {
      return;
    }

    const item = monitor.getItem();

    if (!isContainer(item)) {
      component.onDrop(item);

      if (item.data && typeof props.setAsChild === 'function') {
        const isNew = !item.data.id;
        const data = isNew ? item.onCreate(item.data) : item.data;
        props.setAsChild(props.data, data, props.col);
      }
    }
  }

}, (connect, monitor) => ({
  connectDropTarget: connect.dropTarget(),
  isOver: monitor.isOver(),
  isOverCurrent: monitor.isOver({
    shallow: true
  }),
  canDrop: monitor.canDrop()
}))(Dustbin);

exports.default = _default;