import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Drawer from "@alifd/next/es/drawer";
import _Button from "@alifd/next/es/button";
import _Message from "@alifd/next/es/message";
import React, { Component, useEffect, useRef, useState } from "react";
import "./index.less";
import FlowPage from "./page";
import { data } from "./page/data";
import { FlowEvent } from "./page/type";
import { FlowContext } from "./context";
import { SetterEventName, getLocationParamsByName } from "../../_utils/utils";
import { cloneDeep, toNumber } from "lodash";
import { FlowEventService } from "./page/service";
import { t } from "../../../src/locale";
import CopyTitle from "../_components/copyTitle";
var PubSub = window.PubSub;
var CFlowEventSetter = function CFlowEventSetter(props) {
  var value = props.value,
    onChange = props.onChange,
    field = props.field;
  var flowEventService = new FlowEventService();
  var _useState = useState({}),
    flowContextData = _useState[0],
    setFlowContextData = _useState[1];
  var _useState2 = useState({
      visible: false
    }),
    drawerConfig = _useState2[0],
    setDrawerConfig = _useState2[1];
  var flowEventRef = useRef();
  var _useState3 = useState('1'),
    flowDataFlag = _useState3[0],
    setFlowDataFlag = _useState3[1];
  var _useState4 = useState(value || data),
    flowData = _useState4[0],
    setFlowData = _useState4[1];
  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 = getLocationParamsByName("pId");
    if (projectId) {
      projectId = 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 = cloneDeep(newData);
        result.noMenusData = flowEventService.getSaveFlowNode(newData);
        result.dataFlag = true;
      } else {
        _Message.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 = 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 === FlowEvent.root && data.type === FlowEvent.start) {
      initFlowData(data, Math.random().toString());
    } else {
      _Message.error(t("setter.importErrorTip"));
    }
  }
  var DrawerTitle = function DrawerTitle() {
    return /*#__PURE__*/React.createElement("div", {
      className: "draw-header-box"
    }, /*#__PURE__*/React.createElement("div", null, " ", t("setter.flowConfig")), /*#__PURE__*/React.createElement("div", {
      style: {
        display: 'flex'
      }
    }, /*#__PURE__*/React.createElement(CopyTitle, {
      getExportStrData: exportComputed,
      getImportStrData: importComputed
    }), /*#__PURE__*/React.createElement(_Button, {
      type: "primary",
      onClick: function onClick() {
        save();
      },
      style: {
        marginLeft: "10px",
        marginRight: "10px"
      }
    }, t("setter.save")), /*#__PURE__*/React.createElement(_Button, {
      onClick: function onClick() {
        setDrawerConfig({
          visible: false
        });
      }
    }, t("setter.cancel"))));
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "flow-btn"
  }, /*#__PURE__*/React.createElement(_Button, {
    type: "primary",
    onClick: function onClick() {
      setDrawerConfig({
        visible: true
      });
    }
  }, t("setter.setFlowConfig")), /*#__PURE__*/React.createElement(_Drawer, {
    className: "flowEventDrawer",
    closeMode: [],
    title: DrawerTitle(),
    width: "100%",
    placement: "right",
    visible: drawerConfig.visible
  }, /*#__PURE__*/React.createElement(FlowContext.Provider, {
    value: flowContextData
  }, /*#__PURE__*/React.createElement(FlowPage, {
    key: flowDataFlag,
    flowData: flowData,
    ref: flowEventRef
  }))));
};
var _default = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  _inheritsLoose(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/React.createElement(CFlowEventSetter, this.props);
  };
  return _default;
}(Component);
export { _default as default };