import _extends from "@babel/runtime/helpers/extends";
import React, { createElement, useState } from 'react';
import { observer } from 'mobx-react';
import { EllipsisOutlined } from '@ant-design/icons';
import { Dropdown } from 'antd';
import { setters } from "../../shell";
import './settingFieldView.less';
function getSetter(setterName) {
  var setter = setters.getSetter(setterName);
  return setter ? setter.view : undefined;
}
export default observer(function SettingFieldView(props) {
  var _field$setters, _index$props;
  var field = props.field;
  var _useState = 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.createElement("div", {
    className: "vitis-setting-field-view"
  }, field.title && /*#__PURE__*/React.createElement("div", {
    className: "field-view-title"
  }, field.title), /*#__PURE__*/React.createElement("div", {
    className: "field-view-main"
  }, setter ? /*#__PURE__*/createElement(setter, _extends({}, 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.createElement("div", {
    className: "placeholder"
  }, "\u65E0\u53EF\u7528\u7684\u8BBE\u7F6E\u5668")), multiSetter && /*#__PURE__*/React.createElement(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.createElement(EllipsisOutlined, {
    className: "field-view-setter-toggle"
  })));
});