"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = compositeDecorator;

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

var _mobx = require("../mobx");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }

var __extends = void 0 && (void 0).__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (b.hasOwnProperty(p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

function compositeDecorator(WrappedComponent) {
  var Wrapper =
  /** @class */
  function (_super) {
    __extends(Wrapper, _super);

    function Wrapper() {
      var _this = _super !== null && _super.apply(this, arguments) || this;

      _this.state = {
        clickedFloor: ''
      }; // 复合组件点击事件

      _this.onCompositeCompClick = function (event) {
        // 触发左侧楼层的滚动
        if (document.getElementById("card" + _this.props.nodeData.id)) {
          document.getElementById("card" + _this.props.nodeData.id).scrollIntoView({
            behavior: 'smooth'
          });
        } // 这里必须阻止事件冒泡，然后再触发自定义的事件


        event.preventDefault();
        event.stopPropagation(); // 触发点击组件事件

        var evt = new Event('component-click', {
          bubbles: false
        });
        evt.data = _this.props.nodeData;
        window.document.dispatchEvent(evt);
      };

      _this.getStyle = function () {
        var style = {
          border: 'none'
        }; // 处理复合组件的选中状态

        if (window.magicDesign.mode === 'designer') {
          style.border = '1px solid transparent';

          if (_this.props.nodeData.id === _this.state.clickedFloor) {
            style.border = '1px dashed red';
          }
        } // 处理复合组件的下边距


        if (Wrapper.type !== 'DIVIDER') {
          var rate = window.magicDesign.device === 'pc' ? 1.5 : 1;
          style.marginBottom = Number(window.magicDesign.compSpacing) * rate + "px";
        }

        return style;
      };

      return _this;
    }

    Wrapper.prototype.componentDidMount = function () {
      var _this = this;

      this.distroy = (0, _mobx.autorun)(function () {
        _this.setState({
          clickedFloor: _mobx.store.clickedFloor
        });
      });
    };

    Wrapper.prototype.componentWillUnmount = function () {
      this.distroy();
    };

    Wrapper.prototype.render = function () {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "composite_wrap",
        onClick: this.onCompositeCompClick,
        style: this.getStyle()
      }, /*#__PURE__*/_react["default"].createElement(WrappedComponent, this.props));
    };

    Wrapper.getDefaultJSON = WrappedComponent.getDefaultJSON;
    Wrapper.type = WrappedComponent.type;
    return Wrapper;
  }(_react.Component);

  return Wrapper;
}