"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 _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _cTreeDataRelationSetter = _interopRequireDefault(require("../cTreeDataRelation-setter"));
var _lodash = require("lodash");
var _utils = require("../../_utils/utils");
var _utils2 = require("../cBehavior-setter/config/utils");
require("./index.less");
var _locale = require("../../../src/locale");
var _treeContext = require("../cDataSourceList-setter/treeContext");
var _formKeyType = require("../../_utils/formKeyType");
var _boxSlotComponent = _interopRequireDefault(require("./boxSlotComponent"));
var _renderByFormKey = _interopRequireDefault(require("./renderByFormKey"));
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 TupleRender = function TupleRender(props) {
  var onChange = props.onChange,
    components = props.components,
    data = props.data,
    addable = props.addable;
  var CTrCxt = (0, _react.useContext)(_treeContext.CTreeContext);
  var _useState = (0, _react.useState)({
      visible: false,
      data: {
        originType: _formKeyType.TupleOriginType["default"],
        defaultData: [],
        customData: (0, _utils2.getInitStateKey)(_formKeyType.DataSourceType.defaultType, '')
      }
    }),
    dialogConfig = _useState[0],
    setDialogConfig = _useState[1];
  function closeDialog() {
    setDialogConfig((0, _extends2["default"])({}, dialogConfig, {
      visible: false
    }));
  }
  function handleOk() {
    var listData = [{
      key: "components",
      value: components
    }, {
      key: "data",
      value: dialogConfig.data || {}
    }, {
      key: "addable",
      value: addable || 0
    }];
    onChange(listData);
    closeDialog();
  }
  function onChangeTuple(e, index) {
    var oldDefaultData = (0, _lodash.cloneDeep)(dialogConfig.data.defaultData || []);
    var newDefaultData = oldDefaultData.map(function (item, i) {
      if (i === index) {
        return e;
      }
      return item;
    });
    setDialogConfig((0, _extends2["default"])({}, dialogConfig, {
      data: (0, _extends2["default"])({}, dialogConfig.data, {
        defaultData: newDefaultData
      })
    }));
  }
  function addSelectOne() {
    var base = (0, _lodash.cloneDeep)(components).map(function (item) {
      item.id = (0, _utils.uuid)();
      return item;
    });
    if (dialogConfig.data.defaultData) {
      var newData = (0, _lodash.cloneDeep)(dialogConfig.data.defaultData).concat([base]);
      setDialogConfig((0, _extends2["default"])({}, dialogConfig, {
        data: (0, _extends2["default"])({}, dialogConfig.data, {
          defaultData: newData
        })
      }));
    }
  }
  function removeData(index) {
    if (dialogConfig.data.defaultData) {
      var _data = (0, _lodash.cloneDeep)(dialogConfig.data.defaultData);
      _data.splice(index, 1);
      setDialogConfig((0, _extends2["default"])({}, dialogConfig, {
        data: (0, _extends2["default"])({}, dialogConfig.data, {
          defaultData: _data
        })
      }));
    }
  }
  function isShowAddBtn() {
    return CTrCxt.isShowAddBtn && (components === null || components === void 0 ? void 0 : components.length) > 0 && dialogConfig.data.originType === _formKeyType.TupleOriginType["default"] && (addable === 0 || dialogConfig.data.defaultData.length < addable);
  }
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    disabled: components.length === 0,
    onClick: function onClick() {
      var newData = (0, _utils.compatibleTupleData)(data);
      setDialogConfig({
        visible: true,
        data: (0, _lodash.cloneDeep)(newData)
      });
    }
  }, (0, _locale.t)("setter.setTuple")), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    className: "CRenderBox-dialog",
    title: (0, _locale.t)("setter.setTuple"),
    footerAlign: "right",
    height: 650,
    visible: dialogConfig.visible,
    onClose: closeDialog,
    footer: [/*#__PURE__*/_react["default"].createElement(_button["default"], {
      onClick: handleOk,
      type: "primary"
    }, (0, _locale.t)('setter.ok')), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      style: {
        marginLeft: '10px'
      },
      onClick: closeDialog
    }, (0, _locale.t)('setter.cancel'))]
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.tupleOriginType")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    dataSource: (0, _utils.getTupleOriginOptions)(),
    value: dialogConfig.data.originType,
    onChange: function onChange(v) {
      setDialogConfig((0, _extends2["default"])({}, dialogConfig, {
        data: (0, _extends2["default"])({}, dialogConfig.data, {
          originType: v
        })
      }));
    }
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "tupleBox"
  }, dialogConfig.data.originType === _formKeyType.TupleOriginType["default"] && dialogConfig.data.defaultData.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "tupleBoxItem"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "tupleBoxChildren"
    }, /*#__PURE__*/_react["default"].createElement(_cTreeDataRelationSetter["default"], (0, _extends2["default"])({}, props, {
      key: (0, _utils2.getNewKeyByApiParams)(item),
      value: item,
      maxAddNum: -1,
      onChange: function onChange(e) {
        onChangeTuple(e, index);
      }
    }))));
  }), dialogConfig.data.originType === _formKeyType.TupleOriginType.customData && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.tupleOriginCustomData")
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: (0, _formKeyType.getRelationList)(),
    value: dialogConfig.data.customData,
    onChange: function onChange(v) {
      (0, _utils.handleRenderByFormKeyData)(v, function (newItem) {
        var newData = (0, _extends2["default"])({}, dialogConfig.data.customData, newItem);
        setDialogConfig((0, _extends2["default"])({}, dialogConfig, {
          data: (0, _extends2["default"])({}, dialogConfig.data, {
            customData: newData
          })
        }));
      });
    }
  }))), isShowAddBtn() && /*#__PURE__*/_react["default"].createElement("div", {
    className: "tupleSubBtn"
  }, /*#__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(TupleRender, this.props);
  };
  return _default;
}(_react.Component);