"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 _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _type = require("../../type");
var _boxSlotComponent = _interopRequireDefault(require("../../../../_components/boxSlotComponent"));
var _locale = require("../../../../../../src/locale");
var _utils = require("../../utils");
var _context = require("../../../context");
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 EventFlowSetterPanel = function EventFlowSetterPanel(props) {
  var FlowCtx = (0, _react.useContext)(_context.FlowContext);
  var current = props.current,
    onChange = props.onChange;
  var _useState = (0, _react.useState)(current.config || {
      type: _type.FlowEventSelectType.current,
      flowComponentKey: '',
      parentFlowEventNameList: []
    }),
    extendFlowEvent = _useState[0],
    setExtendFlowEvent = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    flowEventList = _useState2[0],
    setFlowEventList = _useState2[1];
  var flowEventSelectOptions = (0, _utils.getFlowEventOptions)();
  (0, _react.useEffect)(function () {
    var _current$config;
    var list = (0, _utils.getFlowEventOptionsBySchema)(FlowCtx.componentKey);
    setFlowEventList(list);

    // 兼容之前数据
    if (((_current$config = current.config) === null || _current$config === void 0 ? void 0 : _current$config.type) === undefined) {
      setExtendFlowEvent((0, _extends2["default"])({}, extendFlowEvent, {
        type: _type.FlowEventSelectType.current,
        parentFlowEventNameList: []
      }));
    }
  }, []);
  var handleChange = function handleChange(e) {
    setExtendFlowEvent((0, _extends2["default"])({}, extendFlowEvent, {
      flowComponentKey: e
    }));
    var obj = (0, _extends2["default"])({}, current, {
      config: (0, _extends2["default"])({}, current.config, {
        flowComponentKey: e
      })
    });
    onChange(obj);
  };
  var handleFlowEventType = function handleFlowEventType(e) {
    setExtendFlowEvent((0, _extends2["default"])({}, extendFlowEvent, {
      type: e
    }));
    var obj = (0, _extends2["default"])({}, current, {
      config: (0, _extends2["default"])({}, current.config, {
        type: e
      })
    });
    onChange(obj);
  };
  var handleFlowEventName = function handleFlowEventName(e, index) {
    var oldList = getOldArr();
    oldList[index] = e;
    updatedParentFlowEventList(oldList);
  };
  var deleteArr = function deleteArr(index) {
    var oldList = getOldArr();
    oldList.splice(index, 1);
    updatedParentFlowEventList(oldList);
  };
  var addSelectOne = function addSelectOne() {
    var oldList = getOldArr();
    oldList.push("");
    updatedParentFlowEventList(oldList);
  };
  function updatedParentFlowEventList(list) {
    setExtendFlowEvent((0, _extends2["default"])({}, extendFlowEvent, {
      parentFlowEventNameList: list
    }));
    var obj = (0, _extends2["default"])({}, current, {
      config: (0, _extends2["default"])({}, current.config, {
        parentFlowEventNameList: list
      })
    });
    onChange(obj);
  }
  function getOldArr() {
    return Object.assign([], extendFlowEvent.parentFlowEventNameList);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "condition-box"
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.flowEventOrigin")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: extendFlowEvent.type,
    dataSource: flowEventSelectOptions,
    onChange: function onChange(e) {
      handleFlowEventType(e);
    }
  })), extendFlowEvent.type === _type.FlowEventSelectType.current && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.flowMenusEventFlow")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: extendFlowEvent.flowComponentKey,
    dataSource: flowEventList,
    onChange: function onChange(e) {
      handleChange(e);
    }
  })), extendFlowEvent.type === _type.FlowEventSelectType.parent && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "parentFlowEventNameListCenter"
  }, extendFlowEvent.parentFlowEventNameList.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "parentFlowEventListItem"
    }, /*#__PURE__*/_react["default"].createElement("div", null, (0, _locale.t)("setter.parentFlowEventNameList")), /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        width: '368px'
      }
    }, /*#__PURE__*/_react["default"].createElement(_input["default"], {
      value: item,
      style: {
        width: 100 + "%"
      },
      placeholder: (0, _locale.t)("setter.inputPlaceHolder"),
      onChange: function onChange(v) {
        handleFlowEventName(v, index);
      }
    })), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
      className: "one_btn"
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: "ashbin",
      size: "small",
      onClick: function onClick() {
        deleteArr(index);
      }
    }))));
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "sub_btn"
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    text: true,
    type: "primary",
    onClick: addSelectOne
  }, /*#__PURE__*/_react["default"].createElement("span", null, (0, _locale.t)("setter.addData"))))));
};
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(EventFlowSetterPanel, this.props);
  };
  return _default;
}(_react.Component);