"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"] = metaDecorator;

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 __());
  };
}();

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

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

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

      _this.state = {
        clickedMeta: '',
        hoveredMeta: ''
      }; // 元组件点击事件

      _this.onMetaClick = function (event) {
        var _a, _b, _c;

        if (window.magicDesign.mode === 'renderer') {
          return;
        } // 组织冒泡


        event.preventDefault();
        event.stopPropagation();
        var parentId = (_a = _this.props.data) === null || _a === void 0 ? void 0 : _a.parentId;
        var metaId = (_b = _this.props.data) === null || _b === void 0 ? void 0 : _b.id;
        var floorId = null; // group中的元组件

        if (parentId) {
          floorId = _mobx.store.getFloorIdByGroupId(parentId);
        } else {
          // 复合组件中的元组件
          floorId = _mobx.store.getFloorIdByMetaId(metaId);
        }

        _mobx.store.setState({
          clickedMeta: (_c = _this.props.data) === null || _c === void 0 ? void 0 : _c.id,
          clickedGroup: parentId,
          clickedFloor: floorId
        });
      };

      _this.onMouseEnter = function () {
        var _a;

        if (window.magicDesign.mode === 'renderer') {
          return;
        }

        _mobx.store.setState({
          hoveredMeta: (_a = _this.props.data) === null || _a === void 0 ? void 0 : _a.id
        });
      };

      _this.onMouseLeave = function () {
        if (window.magicDesign.mode === 'renderer') {
          return;
        }

        _mobx.store.setState({
          hoveredMeta: ''
        });
      };

      _this.isDesignerActive = function () {
        var data = _this.props.data;
        var _a = _this.state,
            hoveredMeta = _a.hoveredMeta,
            clickedMeta = _a.clickedMeta;
        var isHover = (data === null || data === void 0 ? void 0 : data.id) === hoveredMeta;
        var isClick = (data === null || data === void 0 ? void 0 : data.id) === clickedMeta;

        if (window.magicDesign.mode === 'designer' && (isHover || isClick)) {
          return true;
        }

        return false;
      };

      _this.getStyle = function () {
        var _a = _this.state,
            hoveredMeta = _a.hoveredMeta,
            clickedMeta = _a.clickedMeta;
        var data = _this.props.data;
        var isHover = (data === null || data === void 0 ? void 0 : data.id) === hoveredMeta;
        var isClick = (data === null || data === void 0 ? void 0 : data.id) === clickedMeta;
        var style = {
          boxSizing: 'border-box',
          height: '100%'
        };

        if (window.magicDesign.mode === 'renderer') {
          return style;
        } else {
          style.border = isHover || isClick ? '1px solid #2F54EB' : '1px solid transparent';
          style.backgroundColor = isHover ? 'rgba(47,84,235,0.2)' : '';
        }

        return style;
      };

      return _this;
    }

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

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

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

    Wrapper.prototype.render = function () {
      var _a = this.props,
          className = _a.className,
          style = _a.style,
          rest = __rest(_a, ["className", "style"]);

      var hoverStyle = this.getStyle();
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: className,
        style: style
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "meta_wrap",
        onClick: this.onMetaClick,
        onMouseEnter: this.onMouseEnter,
        onMouseLeave: this.onMouseLeave,
        style: hoverStyle
      }, /*#__PURE__*/_react["default"].createElement(WrappedComponent, rest)));
    };

    return Wrapper;
  }(_react.Component);

  return Wrapper;
}