"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _choice = _interopRequireDefault(require("./choice"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var onValueChange = function onValueChange(nodeProps, n, value) {
  var val = nodeProps.editor.value;
  var change = val.change();
  change.setNodeByKey(n.key, {
    data: _objectSpread(_objectSpread({}, value), {}, {
      index: n.data.get('index')
    })
  });
  nodeProps.editor.props.onChange(change, function () {
    nodeProps.editor.props.onEditingDone();
  });
};

exports.onValueChange = onValueChange;

var onRemoveResponse = function onRemoveResponse(nodeProps, value) {
  var val = nodeProps.editor.value;
  var change = val.change();
  var dragInTheBlank = val.document.findDescendant(function (n) {
    return n.data && n.data.get('index') === value.index;
  });
  change.setNodeByKey(dragInTheBlank.key, {
    data: {
      index: dragInTheBlank.data.get('index')
    }
  });
  nodeProps.editor.props.onChange(change, function () {
    nodeProps.editor.props.onEditingDone();
  });
};

exports.onRemoveResponse = onRemoveResponse;

var DragDrop = function DragDrop(props) {
  var attributes = props.attributes,
      data = props.data,
      n = props.n,
      nodeProps = props.nodeProps,
      opts = props.opts;
  var inTable = data.inTable;
  return /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, attributes, {
    style: {
      display: 'inline-flex',
      minHeight: '50px',
      minWidth: '178px',
      position: 'relative',
      margin: inTable ? '10px' : '0 10px',
      cursor: 'pointer'
    }
  }), /*#__PURE__*/_react["default"].createElement(_choice["default"], {
    n: n,
    dragKey: n.key,
    targetId: "0",
    value: data,
    duplicates: opts.options.duplicates,
    onChange: function onChange(value) {
      return onValueChange(nodeProps, n, value);
    },
    removeResponse: function removeResponse(value) {
      return onRemoveResponse(nodeProps, value);
    }
  }, nodeProps.children));
};

DragDrop.propTypes = {
  attributes: _propTypes["default"].object,
  data: _propTypes["default"].object,
  n: _propTypes["default"].object,
  nodeProps: _propTypes["default"].object,
  opts: _propTypes["default"].object
};
var _default = DragDrop;
exports["default"] = _default;
//# sourceMappingURL=index.js.map