"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 _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _react = _interopRequireWildcard(require("react"));
require("./index.less");
var _boxSlotComponent = _interopRequireDefault(require("../_components/boxSlotComponent"));
var _formKeyType = require("../../_utils/formKeyType");
var _ = require("..");
var _util = require("./util");
var _locale = require("../../../src/locale");
var _renderByFormKey = _interopRequireDefault(require("../_components/renderByFormKey"));
var _utils = require("../../_utils/utils");
var _utils2 = require("../cBehavior-setter/config/utils");
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 CLoopDataSetter = function CLoopDataSetter(props) {
  var value = props.value,
    field = props.field,
    onChange = props.onChange;
  var _useState = (0, _react.useState)([]),
    options = _useState[0],
    setOptions = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    customOption = _useState2[0],
    setCustomOption = _useState2[1];
  var _useState3 = (0, _react.useState)(value === null || value === void 0 ? void 0 : value.type),
    type = _useState3[0],
    setType = _useState3[1];
  var _useState4 = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.dataSourceList) || []),
    dataSourceList = _useState4[0],
    setDataSourceList = _useState4[1];
  var _useState5 = (0, _react.useState)(value === null || value === void 0 ? void 0 : value.loopValue),
    loopValue = _useState5[0],
    setLoopValue = _useState5[1];
  var _useState6 = (0, _react.useState)(value === null || value === void 0 ? void 0 : value.loopKey),
    loopKey = _useState6[0],
    setLoopKey = _useState6[1];
  var _useState7 = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.loopAppointData) || (0, _utils2.getInitStateKey)(_formKeyType.DataSourceType.defaultType, '')),
    loopAppointData = _useState7[0],
    setLoopAppointData = _useState7[1];
  (0, _react.useEffect)(function () {
    var options = (0, _util.getLoopRelationList)(field.node);
    var customData = (0, _formKeyType.getRelationList)();
    setOptions(options);
    setCustomOption(customData);
  }, []);
  (0, _react.useEffect)(function () {
    var obj = {
      type: type,
      dataSourceList: dataSourceList,
      loopValue: loopValue,
      loopKey: loopKey,
      loopAppointData: loopAppointData
    };
    onChange(obj);
  }, [type, dataSourceList, loopValue, loopKey, loopAppointData]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "cLoopSetterBox"
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.dataType")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    hasClear: true,
    value: type,
    dataSource: options,
    style: {
      width: "100%"
    },
    onChange: function onChange(e) {
      setType(e);
    }
  })), type !== undefined && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.dataKey")
  }, /*#__PURE__*/_react["default"].createElement(_input["default"], {
    hasClear: true,
    value: loopKey,
    style: {
      width: "100%"
    },
    placeholder: (0, _locale.t)("setter.dataKeyHolder"),
    onChange: function onChange(e) {
      setLoopKey(e);
    }
  }))), type === _formKeyType.DataSourceType.dataSourceType && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_.CDataSourceListSetter, {
    value: dataSourceList,
    maxAddNum: 1,
    onChange: function onChange(e) {
      setDataSourceList(e);
    }
  })), type === _formKeyType.DataSourceType.loopDataType && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.loopField")
  }, /*#__PURE__*/_react["default"].createElement(_input["default"], {
    hasClear: true,
    value: loopValue,
    style: {
      width: "100%"
    },
    placeholder: (0, _locale.t)("setter.loopPlaceHolder"),
    onChange: function onChange(e) {
      setLoopValue(e);
    }
  }))), type === _formKeyType.DataSourceType.loopCustomDataType && /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: "" + (0, _locale.t)("setter.loopCustomOrigin")
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: customOption,
    value: loopAppointData,
    onChange: function onChange(v) {
      (0, _utils.handleRenderByFormKeyData)(v, function (newItem) {
        var newData = (0, _extends2["default"])({}, loopAppointData, newItem);
        setLoopAppointData(newData);
      });
    }
  }))));
};
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(CLoopDataSetter, this.props);
  };
  return _default;
}(_react.Component);