"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 _react = _interopRequireWildcard(require("react"));
var _locale = require("../../../src/locale");
var _ = require("..");
var _data = require("../cFlowEvent-setter/page/data");
require("./index.less");
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 CParentFlowEventListSetter = function CParentFlowEventListSetter(props) {
  var _props$field2, _props$field2$extraPr;
  var value = props.value,
    onChange = props.onChange;
  var _useState = (0, _react.useState)(value || []),
    parentFlowEventList = _useState[0],
    setParentFlowEventList = _useState[1];
  function getOldArr() {
    return Object.assign([], parentFlowEventList);
  }
  var isShowOneAdd = (0, _react.useCallback)(function () {
    var _props$field, _props$field$extraPro;
    var result = true;
    // 有无数量限制
    var num = (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$extraPro.maxAddNum) || props.maxAddNum;
    if (num !== undefined) {
      result = num > -1 ? parentFlowEventList.length < num : false;
    }
    return result;
  }, [props === null || props === void 0 ? void 0 : (_props$field2 = props.field) === null || _props$field2 === void 0 ? void 0 : (_props$field2$extraPr = _props$field2.extraProps) === null || _props$field2$extraPr === void 0 ? void 0 : _props$field2$extraPr.maxAddNum, props.maxAddNum]);

  /**
   * 删
   * @param index
   */
  function deleteArr(index) {
    var oldArr = getOldArr();
    oldArr.splice(index, 1);
    comChange(oldArr);
  }
  /**
   * 增
  */
  function addSelectOne() {
    var oldArr = getOldArr();
    oldArr.push({
      name: "",
      config: _data.data
    });
    comChange(oldArr);
  }
  function onInputChange(v, index, key) {
    var oldArr = getOldArr();
    oldArr[index][key] = v;
    comChange(oldArr);
  }
  function comChange(oldArr) {
    setParentFlowEventList(oldArr);
    onChange(oldArr);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "parentFlowEventListSetter"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "parentFlowEventListSetterContent"
  }, parentFlowEventList.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "parentFlowEventListItem"
    }, /*#__PURE__*/_react["default"].createElement("div", null, index === 0 && /*#__PURE__*/_react["default"].createElement("p", null, (0, _locale.t)("setter.parentHeaderName")), /*#__PURE__*/_react["default"].createElement(_input["default"], {
      value: item.name,
      style: {
        width: 100 + "%"
      },
      placeholder: (0, _locale.t)("setter.inputPlaceHolder"),
      onChange: function onChange(v) {
        onInputChange(v, index, "name");
      }
    })), /*#__PURE__*/_react["default"].createElement("div", null, index === 0 && /*#__PURE__*/_react["default"].createElement("p", null, (0, _locale.t)("setter.parentFlowEvent")), /*#__PURE__*/_react["default"].createElement(_.CFlowEventSetter, {
      value: item.config,
      onChange: function onChange(v) {
        onInputChange(v, index, "config");
      }
    })), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        marginTop: index === 0 ? "27px" : ''
      },
      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"
  }, isShowOneAdd() && /*#__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(CParentFlowEventListSetter, this.props);
  };
  return _default;
}(_react.Component);