import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _cloneDeep from "lodash/cloneDeep";
import _isEqual from "lodash/isEqual";
import React from "react";
import { get } from "dot-prop-immutable";
import { getPlugins } from "../../plugins";
var Node = "div";

var combineClassNames = function combineClassNames() {
  for (var _len = arguments.length, styles = new Array(_len), _key = 0; _key < _len; _key++) {
    styles[_key] = arguments[_key];
  }

  return styles.filter(function (s) {
    return s !== "" && s !== "css-0";
  }).join(" ");
};

var ElementRoot = /*#__PURE__*/function (_React$Component) {
  _inherits(ElementRoot, _React$Component);

  var _super = _createSuper(ElementRoot);

  function ElementRoot(props) {
    var _this;

    _classCallCheck(this, ElementRoot);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "stylePlugins", void 0);

    _defineProperty(_assertThisInitialized(_this), "attributePlugins", void 0);

    _this.stylePlugins = getPlugins("pb-render-page-element-style");
    _this.attributePlugins = getPlugins("pb-render-page-element-attributes");
    return _this;
  }

  _createClass(ElementRoot, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(props) {
      var _props$element$data;

      var equal = _isEqual(props.element.data, this.props.element.data);

      if (!equal && (_props$element$data = props.element.data) !== null && _props$element$data !== void 0 && _props$element$data.formItemSettings) {
        // 控件栅格属性作用在父元素上，栅格改变无需渲染组件
        if (props.element.id === this.props.element.id) {
          var dataBefore = _cloneDeep(props.element.data);

          var dataAfter = _cloneDeep(this.props.element.data);

          delete dataBefore.settings.cols;
          delete dataAfter.settings.cols;
          equal = _isEqual(dataBefore, dataAfter);
        }
      }

      if (equal) {
        var elementsBefore = props.element.elements;
        var elementsAfter = this.props.element.elements;
        equal = _isEqual(elementsBefore, elementsAfter);
      }

      return !equal || !_isEqual(props.elScrollWidth, this.props.elScrollWidth);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          element = _this$props.element,
          _this$props$style = _this$props.style,
          style = _this$props$style === void 0 ? {} : _this$props$style,
          children = _this$props.children,
          _this$props$className = _this$props.className,
          className = _this$props$className === void 0 ? null : _this$props$className;
      var shallowElement = {
        id: element.id,
        type: element.type,
        data: element.data
      };
      var finalStyle = this.stylePlugins.reduce(function (style, pl) {
        return pl.renderStyle({
          element: shallowElement,
          style: style
        });
      }, style);
      var attributes = this.attributePlugins.reduce(function (attributes, pl) {
        return pl.renderAttributes({
          element: shallowElement,
          attributes: attributes
        });
      }, {});
      var classNames = get(element, "data.settings.className", "");

      var getAllClasses = function getAllClasses() {
        for (var _len2 = arguments.length, extraClasses = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
          extraClasses[_key2] = arguments[_key2];
        }

        return [className].concat(extraClasses, _toConsumableArray(classNames.split(" "))).filter(function (v) {
          return v && v !== "css-0";
        }).join(" ");
      };

      if (typeof children === "function") {
        return children({
          getAllClasses: getAllClasses,
          combineClassNames: combineClassNames,
          elementStyle: finalStyle,
          elementAttributes: attributes,
          customClasses: classNames.split(" ")
        });
      }

      return /*#__PURE__*/React.createElement(Node, Object.assign({
        className: getAllClasses(),
        style: finalStyle
      }, attributes), children);
    }
  }]);

  return ElementRoot;
}(React.Component);

export { ElementRoot };
//# sourceMappingURL=ElementRoot.js.map