"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _drawer = _interopRequireDefault(require("@alifd/next/lib/drawer"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _page = _interopRequireDefault(require("./page"));
var _data = require("./page/data");
var _type = require("./page/type");
var _context = require("./context");
var _utils = require("../../_utils/utils");
var _lodash = require("lodash");
var _service = require("./page/service");
var _locale = require("../../../src/locale");
var _copyTitle = _interopRequireDefault(require("../_components/copyTitle"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var PubSub = window.PubSub;
var CFlowEventSetter = function CFlowEventSetter(props) {
  var value = props.value,
    onChange = props.onChange,
    field = props.field;
  var flowEventService = new _service.FlowEventService();
  var _useState = (0, _react.useState)({}),
    flowContextData = _useState[0],
    setFlowContextData = _useState[1];
  var _useState2 = (0, _react.useState)({
      visible: false
    }),
    drawerConfig = _useState2[0],
    setDrawerConfig = _useState2[1];
  var flowEventRef = (0, _react.useRef)();
  var _useState3 = (0, _react.useState)('1'),
    flowDataFlag = _useState3[0],
    setFlowDataFlag = _useState3[1];
  var _useState4 = (0, _react.useState)(value || _data.data),
    flowData = _useState4[0],
    setFlowData = _useState4[1];
  (0, _react.useEffect)(function () {
    var _props$field, _props$field$extraPro, _props$field$extraPro2, _props$selected;
    if (flowData && flowData.menus.length === 0) {
      initFlowData(flowData);
    }
    var pluginId = (props === null || props === void 0 ? void 0 : (_props$field = props.field) === null || _props$field === void 0 ? void 0 : (_props$field$extraPro = _props$field.extraProps) === null || _props$field$extraPro === void 0 ? void 0 : (_props$field$extraPro2 = _props$field$extraPro.props) === null || _props$field$extraPro2 === void 0 ? void 0 : _props$field$extraPro2.pluginId) || -1;
    var projectId = (0, _utils.getLocationParamsByName)("pId");
    if (projectId) {
      projectId = (0, _lodash.toNumber)(projectId);
    }
    var componentKey = props === null || props === void 0 ? void 0 : (_props$selected = props.selected) === null || _props$selected === void 0 ? void 0 : _props$selected.propsData.componentKey;
    setFlowContextData({
      pluginId: pluginId,
      projectId: projectId,
      componentKey: componentKey
    });
  }, []);
  function initFlowData(data, flowDataFlag) {
    if (flowDataFlag === void 0) {
      flowDataFlag = '1';
    }
    var newFlowData = flowEventService.loadFlowNode(data);
    setFlowData(newFlowData);
    setFlowDataFlag(flowDataFlag);
  }
  function getFlowEventData() {
    var result = {
      dataFlag: false,
      noMenusData: {},
      menusData: {}
    };
    if (flowEventRef.current.save) {
      var newData = flowEventRef.current.save();
      var checkRes = flowEventService.checkAllNode(newData);
      if (checkRes.code === 200) {
        result.menusData = (0, _lodash.cloneDeep)(newData);
        result.noMenusData = flowEventService.getSaveFlowNode(newData);
        result.dataFlag = true;
      } else {
        _message["default"].error(checkRes.msg);
      }
    }
    return result;
  }
  function save() {
    var res = getFlowEventData();
    if (res.dataFlag) {
      setFlowData(res.menusData);
      var newData = res.noMenusData;
      onChange(newData);
      setDrawerConfig({
        visible: false
      });
      if (PubSub && flowContextData.pluginId) {
        var propsField = field === null || field === void 0 ? void 0 : field.parent;
        var key = _utils.SetterEventName.flowEventConfig + "-" + propsField.id;
        PubSub.publish(key, newData);
      }
    }
  }

  /**
  * 导出关系配置信息
  */
  function exportComputed() {
    var result = null;
    var res = getFlowEventData();
    if (res.dataFlag) {
      result = res.noMenusData;
    }
    return result;
  }

  /**
   * 导入关系配置信息
   */
  function importComputed(data) {
    if (data && data.id === _type.FlowEvent.root && data.type === _type.FlowEvent.start) {
      initFlowData(data, Math.random().toString());
    } else {
      _message["default"].error((0, _locale.t)("setter.importErrorTip"));
    }
  }
  var DrawerTitle = function DrawerTitle() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "draw-header-box"
    }, /*#__PURE__*/_react["default"].createElement("div", null, " ", (0, _locale.t)("setter.flowConfig")), /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: 'flex'
      }
    }, /*#__PURE__*/_react["default"].createElement(_copyTitle["default"], {
      getExportStrData: exportComputed,
      getImportStrData: importComputed
    }), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      type: "primary",
      onClick: function onClick() {
        save();
      },
      style: {
        marginLeft: "10px",
        marginRight: "10px"
      }
    }, (0, _locale.t)("setter.save")), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      onClick: function onClick() {
        setDrawerConfig({
          visible: false
        });
      }
    }, (0, _locale.t)("setter.cancel"))));
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "flow-btn"
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    type: "primary",
    onClick: function onClick() {
      setDrawerConfig({
        visible: true
      });
    }
  }, (0, _locale.t)("setter.setFlowConfig")), /*#__PURE__*/_react["default"].createElement(_drawer["default"], {
    className: "flowEventDrawer",
    closeMode: [],
    title: DrawerTitle(),
    width: "100%",
    placement: "right",
    visible: drawerConfig.visible
  }, /*#__PURE__*/_react["default"].createElement(_context.FlowContext.Provider, {
    value: flowContextData
  }, /*#__PURE__*/_react["default"].createElement(_page["default"], {
    key: flowDataFlag,
    flowData: flowData,
    ref: flowEventRef
  }))));
};
var _default = exports["default"] = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  (0, _inheritsLoose2["default"])(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(CFlowEventSetter, this.props);
  };
  return _default;
}(_react.Component);