import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _isEqual from "lodash/isEqual";
import React from "react";
import { connect } from "../../../redux";
import Element from "../../../components/Element";
import { getElement } from "../../../selectors";
import { ResizeArea, Resizer, resizeHandler } from "../../../components/Resize";
import { judgeIfBlockElement } from "../../../../utils/blockUtil";
import DropChild from "../../../components/DropChild";

var Document = function Document(props) {
  var buildMode = props.buildMode,
      element = props.element,
      resizeStart = props.resizeStart,
      resizeStop = props.resizeStop,
      updateElement = props.updateElement;
  var _element$data = element.data;
  _element$data = _element$data === void 0 ? {} : _element$data;
  var _element$data$classNa = _element$data.className,
      className = _element$data$classNa === void 0 ? '' : _element$data$classNa,
      type = element.type,
      elements = element.elements;

  if (buildMode === 'H5App') {
    return /*#__PURE__*/React.createElement("div", {
      className: "webiny-pb-page-document h-full ".concat(judgeIfBlockElement(element) ? 'webiny-pb-page-is-block' : '', " ").concat(className)
    }, element.elements.map(function (el) {
      return /*#__PURE__*/React.createElement(Element, {
        key: el.id,
        id: el.id
      });
    }));
  }

  if (judgeIfBlockElement(element)) {
    return /*#__PURE__*/React.createElement("div", {
      className: "webiny-pb-page-is-block ".concat(className)
    }, element.elements.map(function (el) {
      return /*#__PURE__*/React.createElement(Element, {
        key: el.id,
        id: el.id
      });
    }));
  }

  var _resizeHandler = resizeHandler({
    element: element,
    resizeStart: resizeStart,
    resizeStop: resizeStop,
    updateElement: updateElement
  }),
      refChildren = _resizeHandler.refChildren,
      resizing = _resizeHandler.resizing,
      restResizProps = _objectWithoutProperties(_resizeHandler, ["refChildren", "resizing"]);

  return /*#__PURE__*/React.createElement("div", {
    className: "webiny-pb-page-document h-full ".concat(className)
  }, /*#__PURE__*/React.createElement(ResizeArea, {
    refChildren: refChildren,
    resizing: resizing
  }), elements.map(function (child, index) {
    var last = index === elements.length - 1;
    return /*#__PURE__*/React.createElement(Resizer, Object.assign({
      key: child.id,
      id: child.id,
      index: index,
      resizing: resizing
    }, restResizProps), /*#__PURE__*/React.createElement(DropChild, {
      childId: child.id,
      type: type,
      id: element.id,
      index: index,
      count: elements.length,
      last: last
    }));
  }));
};

var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    updateElement: function updateElement(payload) {
      dispatch({
        type: 'appBuilder/updateAndFlattenElement',
        payload: payload
      });
    },
    resizeStart: function resizeStart() {
      dispatch({
        type: 'ui/resizeStart'
      });
    },
    resizeStop: function resizeStop() {
      dispatch({
        type: 'ui/resizeStop'
      });
    }
  };
};

export default connect(function (state, props) {
  var element = getElement(state, props.element.id);
  return {
    element: _objectSpread(_objectSpread({}, element), {}, {
      elements: element.elements.map(function (id) {
        return getElement(state, id);
      })
    })
  };
}, mapDispatchToProps, null, {
  areStatePropsEqual: _isEqual
})(Document);
//# sourceMappingURL=Document.js.map