"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _mobxReact = require("mobx-react");
var _icons = require("@ant-design/icons");
var _antd = require("antd");
var _shell = require("../../shell");
require("./settingFieldView.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 && Object.prototype.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; }
function getSetter(setterName) {
  var setter = _shell.setters.getSetter(setterName);
  return setter ? setter.view : undefined;
}
var _default = exports["default"] = (0, _mobxReact.observer)(function SettingFieldView(props) {
  var _field$setters, _index$props;
  var field = props.field;
  var _useState = (0, _react.useState)(0),
    index = _useState[0],
    setIndex = _useState[1];
  var setter = field.setters ? getSetter(field.setters[index].name) : undefined;
  var val = field.getValue();
  var multiSetter = !!((_field$setters = field.setters) !== null && _field$setters !== void 0 && _field$setters.length) && field.setters.length > 1;
  var onChangeMenu = function onChangeMenu(_ref) {
    var _newIndex$props;
    var key = _ref.key;
    var newIndex = field.setters.findIndex(function (item) {
      return item.name === key;
    });
    setIndex(newIndex);
    field.setValue((_newIndex$props = field.setters[newIndex].props) === null || _newIndex$props === void 0 ? void 0 : _newIndex$props.defaultValue);
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "vitis-setting-field-view"
  }, field.title && /*#__PURE__*/_react["default"].createElement("div", {
    className: "field-view-title"
  }, field.title), /*#__PURE__*/_react["default"].createElement("div", {
    className: "field-view-main"
  }, setter ? /*#__PURE__*/(0, _react.createElement)(setter, (0, _extends2["default"])({}, field.setters[0].props, {
    field: field,
    value: val !== undefined ? val : (_index$props = field.setters[index].props) === null || _index$props === void 0 ? void 0 : _index$props.defaultValue,
    onChange: field.setValue,
    key: field.id
  })) : /*#__PURE__*/_react["default"].createElement("div", {
    className: "placeholder"
  }, "\u65E0\u53EF\u7528\u7684\u8BBE\u7F6E\u5668")), multiSetter && /*#__PURE__*/_react["default"].createElement(_antd.Dropdown, {
    placement: "topRight",
    menu: {
      items: field.setters.map(function (item) {
        return {
          key: item.name,
          label: item.name
        };
      }),
      onClick: onChangeMenu,
      activeKey: field.setters[index].name
    }
  }, /*#__PURE__*/_react["default"].createElement(_icons.EllipsisOutlined, {
    className: "field-view-setter-toggle"
  })));
});