"use strict";
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;
var _react = _interopRequireWildcard(require("react")),
  _propTypes = _interopRequireDefault(require("prop-types")),
  _amosTool = require("amos-tool"),
  _amosFramework = require("amos-framework"),
  _amosJsplumb = require("amos-jsplumb"),
  _rayEventpool = require("ray-eventpool"),
  _amosReactRouter = require("amos-react-router"),
  _ruleDesignService = require("./../../../services/ruleDesignService"),
  _StageHeader = _interopRequireDefault(require("./header/StageHeader")),
  _RuleScenePlumb = _interopRequireDefault(require("./RuleScenePlumb")),
  _EleCatalogPanel = _interopRequireDefault(require("./extra/EleCatalogPanel")),
  _stageConf = require("./modelConf/stageConf"),
  _addNode = require("./addNode");

function _interopRequireDefault(e) {
  return e && e.__esModule ? e : {
    default: e
  }
}

function _getRequireWildcardCache() {
  if ("function" != typeof WeakMap) return null;
  var e = new WeakMap;
  return _getRequireWildcardCache = function() {
    return e
  }, e
}

function _interopRequireWildcard(e) {
  if (e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var t = _getRequireWildcardCache();
  if (t && t.has(e)) return t.get(e);
  var o, n, r = {},
    i = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (o in e) Object.prototype.hasOwnProperty.call(e, o) && ((n = i ? Object.getOwnPropertyDescriptor(e, o) : null) && (n.get || n.set) ? Object.defineProperty(r, o, n) : r[o] = e[o]);
  return r.default = e, t && t.set(e, r), r
}

function _typeof(e) {
  return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e
  } : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
  })(e)
}

function _objectWithoutProperties(e, t) {
  if (null == e) return {};
  var o, n = _objectWithoutPropertiesLoose(e, t);
  if (Object.getOwnPropertySymbols)
    for (var r = Object.getOwnPropertySymbols(e), i = 0; i < r.length; i++) o = r[i], 0 <= t.indexOf(o) || Object.prototype.propertyIsEnumerable.call(e, o) && (n[o] = e[o]);
  return n
}

function _objectWithoutPropertiesLoose(e, t) {
  if (null == e) return {};
  for (var o, n = {}, r = Object.keys(e), i = 0; i < r.length; i++) o = r[i], 0 <= t.indexOf(o) || (n[o] = e[o]);
  return n
}

function ownKeys(t, e) {
  var o, n = Object.keys(t);
  return Object.getOwnPropertySymbols && (o = Object.getOwnPropertySymbols(t), e && (o = o.filter(function(e) {
    return Object.getOwnPropertyDescriptor(t, e).enumerable
  })), n.push.apply(n, o)), n
}

function _objectSpread(t) {
  for (var e = 1; e < arguments.length; e++) {
    var o = null != arguments[e] ? arguments[e] : {};
    e % 2 ? ownKeys(Object(o), !0).forEach(function(e) {
      _defineProperty(t, e, o[e])
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(t, Object.getOwnPropertyDescriptors(o)) : ownKeys(Object(o)).forEach(function(e) {
      Object.defineProperty(t, e, Object.getOwnPropertyDescriptor(o, e))
    })
  }
  return t
}

function _extends() {
  return (_extends = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var o, n = arguments[t];
      for (o in n) Object.prototype.hasOwnProperty.call(n, o) && (e[o] = n[o])
    }
    return e
  }).apply(this, arguments)
}

function _toConsumableArray(e) {
  return _arrayWithoutHoles(e) || _iterableToArray(e) || _unsupportedIterableToArray(e) || _nonIterableSpread()
}

function _nonIterableSpread() {
  throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}

function _unsupportedIterableToArray(e, t) {
  if (e) {
    if ("string" == typeof e) return _arrayLikeToArray(e, t);
    var o = Object.prototype.toString.call(e).slice(8, -1);
    return "Object" === o && e.constructor && (o = e.constructor.name), "Map" === o || "Set" === o ? Array.from(e) : "Arguments" === o || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o) ? _arrayLikeToArray(e, t) : void 0
  }
}

function _iterableToArray(e) {
  if ("undefined" != typeof Symbol && Symbol.iterator in Object(e)) return Array.from(e)
}

function _arrayWithoutHoles(e) {
  if (Array.isArray(e)) return _arrayLikeToArray(e)
}

function _arrayLikeToArray(e, t) {
  (null == t || t > e.length) && (t = e.length);
  for (var o = 0, n = new Array(t); o < t; o++) n[o] = e[o];
  return n
}

function _classCallCheck(e, t) {
  if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
}

function _defineProperties(e, t) {
  for (var o = 0; o < t.length; o++) {
    var n = t[o];
    n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(e, n.key, n)
  }
}

function _createClass(e, t, o) {
  return t && _defineProperties(e.prototype, t), o && _defineProperties(e, o), e
}

function _inherits(e, t) {
  if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function");
  e.prototype = Object.create(t && t.prototype, {
    constructor: {
      value: e,
      writable: !0,
      configurable: !0
    }
  }), t && _setPrototypeOf(e, t)
}

function _setPrototypeOf(e, t) {
  return (_setPrototypeOf = Object.setPrototypeOf || function(e, t) {
    return e.__proto__ = t, e
  })(e, t)
}

function _createSuper(o) {
  var n = _isNativeReflectConstruct();
  return function() {
    var e, t = _getPrototypeOf(o);
    return _possibleConstructorReturn(this, n ? (e = _getPrototypeOf(this).constructor, Reflect.construct(t, arguments, e)) : t.apply(this, arguments))
  }
}

function _possibleConstructorReturn(e, t) {
  return !t || "object" !== _typeof(t) && "function" != typeof t ? _assertThisInitialized(e) : t
}

function _assertThisInitialized(e) {
  if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  return e
}

function _isNativeReflectConstruct() {
  if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
  if (Reflect.construct.sham) return !1;
  if ("function" == typeof Proxy) return !0;
  try {
    return Date.prototype.toString.call(Reflect.construct(Date, [], function() {})), !0
  } catch (e) {
    return !1
  }
}

function _getPrototypeOf(e) {
  return (_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function(e) {
    return e.__proto__ || Object.getPrototypeOf(e)
  })(e)
}

function _defineProperty(e, t, o) {
  return t in e ? Object.defineProperty(e, t, {
    value: o,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = o, e
}
var extraSize = {
    top: 90,
    left: 230
  },
  RuleStage = function() {
    _inherits(o, _react.Component);
    var t = _createSuper(o);

    function o(e) {
      var u;
      return _classCallCheck(this, o), _defineProperty(_assertThisInitialized(u = t.call(this, e)), "onAddConnection", function(e, t, o) {
        o = {
          id: e,
          sourceId: t,
          targetId: o
        };
        u.setState({
          connections: [].concat(_toConsumableArray(u.state.connections), [o])
        })
      }), _defineProperty(_assertThisInitialized(u), "onRemoveConnection", function(t, e) {
        u.setState({
          connections: u.state.connections.filter(function(e) {
            return e.id !== t
          })
        })
      }), _defineProperty(_assertThisInitialized(u), "onZoom", function(e) {
        u.setState({
          zoom: e
        })
      }), _defineProperty(_assertThisInitialized(u), "changeZoom", function(e) {
        var t = u.props.zoom;
        e ? t += .2 : t -= .2, u.props.onZoom(t)
      }), _defineProperty(_assertThisInitialized(u), "onRemoveNode", function(t) {
        var e = u.state.nodes.filter(function(e) {
          return e.id !== t
        });
        u.setState({
          nodes: e
        })
      }), _defineProperty(_assertThisInitialized(u), "onChange", function(e) {
        var t = e.nodes,
          e = e.connections;
        u.setState({
          nodes: t,
          connections: e
        })
      }), _defineProperty(_assertThisInitialized(u), "onSceneCreated", function(e) {
        u.plumbInstance = e
      }), _defineProperty(_assertThisInitialized(u), "onSceneClick", function(e) {
        u.setState({
          catalogVisible: !1,
          catalogOptions: {
            eleType: "",
            sourceId: "",
            position: {
              left: 0,
              top: 0
            },
            catalogs: []
          }
        })
      }), _defineProperty(_assertThisInitialized(u), "onSubGraphClosed", function() {
        u.setState({
          subGraphVisible: !1,
          subGraphOptions: {}
        })
      }), _defineProperty(_assertThisInitialized(u), "onConnectionDbClick", function(e, t) {
        console.log(e, t, e.getData());
        var o = e.source,
          t = e.target;
        (0, _stageConf.isStatusDragLink)(t) && u.props.onStatusConnLink(o, t, e.getData())
      }), _defineProperty(_assertThisInitialized(u), "initData", function() {
        var e = u.props.objId;
        e && (0, _ruleDesignService.initStageView)(e).then(function(e) {
          u.setState({
            connections: e.linkVoList,
            nodes: e.nodeVoList,
            ruleSubObj: e.rule
          })
        })
      }), _defineProperty(_assertThisInitialized(u), "reloadPlumb", function() {
        var e = u.props.objId;
        e && (0, _ruleDesignService.initStageView)(e).then(function(e) {
          u.setState({
            needDestroy: !1,
            connections: e.linkVoList,
            nodes: e.nodeVoList
          })
        })
      }), _defineProperty(_assertThisInitialized(u), "filterNodes", function(e) {
        var o = u.state.nodes,
          e = e.filter(function(t) {
            return !o.find(function(e) {
              return e.id === t.id
            })
          });
        return [].concat(_toConsumableArray(o), _toConsumableArray(e))
      }), _defineProperty(_assertThisInitialized(u), "filterConns", function(e) {
        var o = u.state.connections,
          e = e.filter(function(t) {
            return !o.find(function(e) {
              return e.sourceId === t.sourceId && e.targetId === t.targetId
            })
          });
        return [].concat(_toConsumableArray(o), _toConsumableArray(e))
      }), _defineProperty(_assertThisInitialized(u), "saveStage", function(e) {
        var t = u.state,
          o = t.nodes,
          t = t.connections;
        _amosJsplumb.graph.completePosition(u.plumbInstance, o), (0, _ruleDesignService.saveStageView)({
          id: e,
          nodeVoList: o,
          linkVoList: t
        }).then(function(e) {
          _amosFramework.message.success("保存成功！")
        }, function(e) {
          _amosFramework.message.danger(e || "保存失败!")
        })
      }), _defineProperty(_assertThisInitialized(u), "exitStage", function() {
        _amosReactRouter.browserHistory.goBack()
      }), _defineProperty(_assertThisInitialized(u), "addNode", function(e) {
        var t = e.labelValue,
          o = u.state,
          n = o.nodes,
          r = o.connections,
          e = o.position,
          o = o.statusId,
          o = (0, _addNode.addTrigerAndAction)({
            labelValue: t,
            position: e,
            statusId: o
          });
        (0, _ruleDesignService.addViewTrigger)(o), u.setState({
          nodes: [].concat(_toConsumableArray(n), _toConsumableArray(o.node)),
          connections: [].concat(_toConsumableArray(r), _toConsumableArray(o.conn)),
          visible: !1,
          eleType: "",
          statusId: ""
        })
      }), _defineProperty(_assertThisInitialized(u), "cancel", function(e) {
        u.setState({
          visible: !1
        })
      }), _defineProperty(_assertThisInitialized(u), "handleDrop", function(e) {
        (0, _amosJsplumb.stopEvent)(e);
        var t = _amosJsplumb.domTools.getNodeOffset(e),
          o = t.left,
          n = t.top,
          r = e.dataTransfer.getData("FlowChartNode"),
          i = r.split("-")[1],
          a = r.split("-")[2],
          t = (0, _stageConf.transferDropData)(r.split("-")[0]),
          r = t.nodeType,
          t = t.sourceId;
        (0, _stageConf.isDragEle)(r) ? t ? u.doDropAddNodeAndConn({
          nodeType: r,
          x: o,
          y: n,
          sourceId: t
        }) : u.doDropAddNode({
          nodeType: r,
          x: o,
          y: n,
          ruleNodeId: i,
          label: a
        }): "plumb-charts" === (e = e.target).className || (e = (0, _addNode.getEleNode)(e)) && u.doDropAddConn(t, e.id)
      }), _defineProperty(_assertThisInitialized(u), "doDropAddNode", function(e) {
        var t = e.nodeType,
          o = e.x,
          n = e.y,
          r = e.ruleNodeId,
          i = e.label,
          a = u.state.nodes,
          e = u.props.objId,
          i = i || "节点",
          n = {
            left: o,
            top: n
          },
          r = (0, _addNode.commonAddNode)({
            eleType: t,
            labelValue: i,
            position: n,
            ruleNodeId: r
          });
        _extends(r, {
          ruleId: e
        }), (0, _ruleDesignService.addViewNode)(r), u.setState({
          catalogVisible: !1,
          nodes: [].concat(_toConsumableArray(a), [r])
        })
      }), _defineProperty(_assertThisInitialized(u), "doDropAddNodeAndConn", function(e) {
        var t = e.nodeType,
          o = e.x,
          n = e.y,
          r = e.sourceId,
          i = u.state,
          a = i.nodes,
          s = i.connections,
          e = u.props.objId,
          i = _stageConf.locale[t] || "节点",
          n = {
            left: o,
            top: n
          },
          n = (0, _addNode.commonAddNodeAndConn)({
            eleType: t,
            labelValue: i,
            position: n,
            sourceId: r
          });
        (0, _ruleDesignService.addViewNode)(_objectSpread(_objectSpread({}, n.node), {}, {
          subObjId: e,
          sourceId: r
        })), u.setState({
          catalogVisible: !1,
          nodes: [].concat(_toConsumableArray(a), [n.node]),
          connections: [].concat(_toConsumableArray(s), [n.conn])
        })
      }), _defineProperty(_assertThisInitialized(u), "doDropAddConn", function(e, t) {
        var o = u.props.objId;
        (0, _ruleDesignService.addViewLink)({
          oid: o,
          sourceId: e,
          targetId: t
        }).then(function(e) {
          u.setState({
            needDestroy: !0,
            catalogVisible: !1
          }, function() {
            return u.reloadPlumb()
          })
        }, function(e) {
          _amosFramework.message.danger(e || "添加链路失败！")
        })
      }), _defineProperty(_assertThisInitialized(u), "doPreAddEle", function(e) {
        var t = e.type,
          o = e.x,
          e = e.y;
        _amosTool.utils.isEmpty(t) || u.setState({
          visible: !0,
          eleType: t,
          position: {
            left: o,
            top: e
          }
        })
      }), _defineProperty(_assertThisInitialized(u), "beforeDrop", function(e) {
        e.targetId = e.dropEndpoint.elementId, e.targetId === e.sourceId || u.jspInstance.getConnections({
          source: e.sourceId,
          target: e.targetId
        }).length
      }), _defineProperty(_assertThisInitialized(u), "updateGraph", function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {};
        u.setState(_objectSpread({}, e))
      }), _defineProperty(_assertThisInitialized(u), "updateGraphWithDestroy", function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {};
        u.setState({
          destroy: !0
        }, function() {
          u.setState(_objectSpread({}, e))
        })
      }), _defineProperty(_assertThisInitialized(u), "onDropHandler", function(o, n, r) {
        var e = u.state.nodes;
        e.map(function(e) {
          var t;
          e.id === o && (t = {
            left: n,
            top: r
          }, e.config.style = t)
        }), u.setState({
          nodes: e
        })
      }), u.state = {
        connections: [],
        nodes: [],
        zoom: 1,
        visible: !1,
        catalogVisible: !1,
        eleType: "",
        statusId: "",
        position: {
          left: 0,
          top: 0
        },
        ruleSubObj: {},
        needDestroy: !1,
        catalogOptions: {},
        subGraphVisible: !1,
        subGraphOptions: {}
      }, u
    }
    return _createClass(o, [{
      key: "componentWillMount",
      value: function() {}
    }, {
      key: "componentDidMount",
      value: function() {
        var r = this;
        this.initData(), this.pubsub_eleadd = _rayEventpool.PubSub.subscribe(_stageConf.ELE_ADD_TOPIC.eleAdd, function(e, t) {
          var o = t.eleType,
            n = t.statusId,
            t = t.position;
          _amosTool.utils.isEmpty(o) || r.setState({
            visible: !0,
            eleType: o,
            statusId: n,
            position: t
          })
        }), this.pubsub_eledel = _rayEventpool.PubSub.subscribe(_stageConf.ELE_DEL_TOPIC.eleDel, function(e, t) {
          t = t.eleId;
          _amosTool.utils.isEmpty(t) || (0, _ruleDesignService.delViewNode)(t).then(function(e) {
            _amosFramework.message.success("删除成功！"), r.setState({
              needDestroy: !0
            }, function() {
              return r.reloadPlumb()
            })
          }, function(e) {
            _amosFramework.message.danger(e || "删除失败")
          })
        }), this.pubsub_catalog = _rayEventpool.PubSub.subscribe(_stageConf.ELE_CATALOG_TOPIC.NORMAL, function(e, t) {
          var o = t.catalogOptions,
            t = t.catalogVisible;
          _amosTool.utils.isEmpty(o) || r.setState({
            catalogVisible: t,
            catalogOptions: o
          })
        }), this.pubsub_subgraph = _rayEventpool.PubSub.subscribe(_stageConf.ELE_ENTRY_TOPIC.eleEntry, function(e, t) {
          var o = t.subGraphOptions,
            t = t.subGraphVisible;
          _amosTool.utils.isEmpty(o) || r.setState({
            subGraphOptions: o,
            subGraphVisible: t
          })
        })
      }
    }, {
      key: "componentWillReceiveProps",
      value: function(e) {
        var t = this;
        e.needReloadStage && (this.props.toggleReloadStage(!1), this.setState({
          needDestroy: !0
        }, function() {
          return t.reloadPlumb()
        }))
      }
    }, {
      key: "shouldComponentUpdate",
      value: function(e, t) {
        return _amosFramework.PureRenderMixin.shouldComponentUpdate.apply(this, e, t)
      }
    }, {
      key: "componentWillUnmount",
      value: function() {
        _rayEventpool.PubSub.unsubscribe(this.pubsub_eleadd), _rayEventpool.PubSub.unsubscribe(this.pubsub_eledel), _rayEventpool.PubSub.unsubscribe(this.pubsub_catalog), _rayEventpool.PubSub.unsubscribe(this.pubsub_subgraph)
      }
    }, {
      key: "render",
      value: function() {
        var e = this.state,
          t = e.zoom,
          o = e.nodes,
          n = e.connections,
          r = e.catalogOptions,
          i = e.catalogVisible,
          a = e.ruleSubObj,
          s = e.needDestroy,
          u = {
            nodes: o,
            connections: n
          },
          l = {
            onDrop: this.onDropHandler
          },
          e = this.props,
          o = e.objId,
          n = _objectWithoutProperties(e, ["objId"]),
          e = _amosJsplumb.tools.omit(n, ["updateGraph", "updateGraphWithDestroy"]),
          n = {
            dblclick: this.onConnectionDbClick,
            connection: _amosJsplumb.handlers.onConnectionEventHandler,
            beforeDrop: this.beforeDrop
          };
        return _react.default.createElement("div", {
          className: "rule-stage"
        }, _react.default.createElement(_StageHeader.default, {
          saveStage: this.saveStage,
          exitStage: this.exitStage,
          ruleSubObj: a,
          objId: o
        }), _react.default.createElement(_RuleScenePlumb.default, {
          handleDrop: this.handleDrop,
          onAddConnection: this.onAddConnection,
          onRemoveConnection: this.onRemoveConnection,
          onRemoveNode: this.onRemoveNode,
          dataSource: u,
          newRestProps: e,
          onPlumbChange: this.onChange,
          nodeProps: l,
          onSceneCreated: this.onSceneCreated,
          onPlumbClick: this.onSceneClick,
          eventListeners: n,
          zoom: t,
          onZoom: this.onZoom,
          destroy: s
        }), _react.default.createElement(_EleCatalogPanel.default, _extends({
          visible: i
        }, r)))
      }
    }]), o
  }();
_defineProperty(RuleStage, "propTypes", {
  objId: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
  needReloadStage: _propTypes.default.bool,
  toggleReloadStage: _propTypes.default.func,
  onStatusConnLink: _propTypes.default.func
}), RuleStage.displayName = "RuleStage";
var _default = RuleStage;
exports.default = RuleStage, module.exports = exports.default;
