"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 _balloon = _interopRequireDefault(require("@alifd/next/lib/balloon"));
var _react = _interopRequireWildcard(require("react"));
var _formKeyType = require("../../_utils/formKeyType");
var _utils = require("../../_utils/utils");
var _locale = require("../../../src/locale");
require("./index.less");
var _comFn = require("./comFn");
var _renderByFormKey = _interopRequireDefault(require("../_components/renderByFormKey"));
var _treeContext = require("../cDataSourceList-setter/treeContext");
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; }
/**
 * 参数设置器
 * @param props
 * @returns
 */
var CTreeDataRelationSetter = function CTreeDataRelationSetter(props) {
  var _props$field2, _props$field2$extraPr;
  var disabledInput = props.disabledInput;
  var _useState = (0, _react.useState)((props === null || props === void 0 ? void 0 : props.value) || []),
    requestParamMappingData = _useState[0],
    setRequestParamMappingData = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    dataForm = _useState2[0],
    setDataForm = _useState2[1];
  var CTrCxt = (0, _react.useContext)(_treeContext.CTreeContext);
  (0, _react.useEffect)(function () {
    var list = (0, _comFn.getRelationListByProps)(props);
    setDataForm(list);
  }, []);
  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 ? requestParamMappingData.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]);
  function getOldArr() {
    return Object.assign([], requestParamMappingData);
  }
  function comChange(oldArr) {
    setRequestParamMappingData(oldArr);
    props.onChange(oldArr);
  }

  /**
   * 增
   */
  function addSelectOne() {
    var oldArr = getOldArr();
    oldArr.push({
      id: (0, _utils.uuid)(),
      name: "",
      value: "",
      formKey: _formKeyType.DataSourceType.currentPageType
    });
    comChange(oldArr);
  }

  /**
   * 删
   * @param index
   */
  function deleteArr(index) {
    var oldArr = getOldArr();
    oldArr.splice(index, 1);
    comChange(oldArr);
  }

  // 输入新字段
  function onInputChange(v, index, key) {
    var oldArr = getOldArr();
    oldArr[index][key] = v;
    comChange(oldArr);
  }
  function onRenderByFormKey(list, index) {
    var itemData = requestParamMappingData[index];
    if (itemData) {
      list.forEach(function (item) {
        var key = item.key,
          value = item.value;
        itemData[key] = value;
      });
      comChange([].concat(requestParamMappingData));
    }
  }
  function getTipRender(item) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        color: '#fff'
      }
    }, item.name + "(" + item.type + ")");
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "treeDataRelationBox"
  }, requestParamMappingData.length > 0 && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "sub_title"
  }, /*#__PURE__*/_react["default"].createElement("p", null, (0, _locale.t)("setter.params")), /*#__PURE__*/_react["default"].createElement("p", null, (0, _locale.t)("setter.source")), /*#__PURE__*/_react["default"].createElement("p", null, (0, _locale.t)("setter.mapField"))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "sub_box"
  }, requestParamMappingData.map(function (item, index) {
    if (CTrCxt.paramsOpen || !item.disabled) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        key: index,
        className: "addSubmitOneBox"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "tableItem"
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "itemOne tableItemValue"
      }, item.type && /*#__PURE__*/_react["default"].createElement(_balloon["default"], {
        v2: true,
        trigger: getTipRender(item),
        triggerType: "hover",
        closable: false
      }, item.type), !item.type && /*#__PURE__*/_react["default"].createElement(_input["default"], {
        disabled: item.disabled || disabledInput,
        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", {
        className: "itemOne itemRender"
      }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
        relationOption: dataForm,
        value: item,
        onChange: function onChange(data) {
          onRenderByFormKey(data, index);
        }
      }))), !(item.disabled || disabledInput) && /*#__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"
  }, 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(CTreeDataRelationSetter, this.props);
  };
  return _default;
}(_react.Component);