import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Button from "@alifd/next/es/button";
import _Icon from "@alifd/next/es/icon";
import _Input from "@alifd/next/es/input";
import React, { Component, useCallback, useState } from "react";
import { t } from "../../../src/locale";
import { CFlowEventSetter } from "..";
import { data as FlowData } from "../cFlowEvent-setter/page/data";
import "./index.less";
var CParentFlowEventListSetter = function CParentFlowEventListSetter(props) {
  var _props$field2, _props$field2$extraPr;
  var value = props.value,
    onChange = props.onChange;
  var _useState = useState(value || []),
    parentFlowEventList = _useState[0],
    setParentFlowEventList = _useState[1];
  function getOldArr() {
    return Object.assign([], parentFlowEventList);
  }
  var isShowOneAdd = 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: FlowData
    });
    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.createElement("div", {
    className: "parentFlowEventListSetter"
  }, /*#__PURE__*/React.createElement("div", {
    className: "parentFlowEventListSetterContent"
  }, parentFlowEventList.map(function (item, index) {
    return /*#__PURE__*/React.createElement("div", {
      className: "parentFlowEventListItem"
    }, /*#__PURE__*/React.createElement("div", null, index === 0 && /*#__PURE__*/React.createElement("p", null, t("setter.parentHeaderName")), /*#__PURE__*/React.createElement(_Input, {
      value: item.name,
      style: {
        width: 100 + "%"
      },
      placeholder: t("setter.inputPlaceHolder"),
      onChange: function onChange(v) {
        onInputChange(v, index, "name");
      }
    })), /*#__PURE__*/React.createElement("div", null, index === 0 && /*#__PURE__*/React.createElement("p", null, t("setter.parentFlowEvent")), /*#__PURE__*/React.createElement(CFlowEventSetter, {
      value: item.config,
      onChange: function onChange(v) {
        onInputChange(v, index, "config");
      }
    })), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
      style: {
        marginTop: index === 0 ? "27px" : ''
      },
      className: "one_btn"
    }, /*#__PURE__*/React.createElement(_Icon, {
      type: "ashbin",
      size: "small",
      onClick: function onClick() {
        deleteArr(index);
      }
    }))));
  })), /*#__PURE__*/React.createElement("div", {
    className: "sub_btn"
  }, isShowOneAdd() && /*#__PURE__*/React.createElement(_Button, {
    text: true,
    type: "primary",
    onClick: addSelectOne
  }, /*#__PURE__*/React.createElement("span", null, t("setter.addData")))));
};
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(CParentFlowEventListSetter, this.props);
  };
  return _default;
}(Component);
export { _default as default };