import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _get from "@babel/runtime/helpers/get";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import { __decorate } from "tslib";
import React from 'react';
import { action, toJS } from 'mobx';
import { observer } from 'mobx-react';
import noop from 'lodash/noop';
import { FormField } from '../field/FormField';
import { ShowHelp } from '../field/enum';
import autobind from '../_util/autobind';
import { ViewMode } from './enum';
import { $l } from '../locale-context';
import { LabelLayout } from '../form/enum';
import { Tooltip as TextTooltip } from '../core/enum';
import isOverflow from '../overflow-tip/util';
import { show } from '../tooltip/singleton';
export var Radio = /*#__PURE__*/function (_FormField) {
  _inherits(Radio, _FormField);

  var _super = _createSuper(Radio);

  function Radio() {
    var _this;

    _classCallCheck(this, Radio);

    _this = _super.apply(this, arguments);
    _this.type = 'radio';
    return _this;
  }

  _createClass(Radio, [{
    key: "defaultValidationMessages",
    get: function get() {
      var label = this.getProp('label');
      return {
        valueMissing: $l('Radio', label ? 'value_missing' : 'value_missing_no_label', {
          label: label
        })
      };
    }
  }, {
    key: "checkedValue",
    get: function get() {
      var _this$props$value = this.props.value,
          value = _this$props$value === void 0 ? 'on' : _this$props$value;
      return value;
    }
  }, {
    key: "saveLabelRef",
    value: function saveLabelRef(node) {
      this.labelRef = node;
    }
  }, {
    key: "getControlled",
    value: function getControlled(props) {
      return props.checked !== undefined;
    }
  }, {
    key: "getOmitPropsKeys",
    value: function getOmitPropsKeys() {
      return _get(_getPrototypeOf(Radio.prototype), "getOmitPropsKeys", this).call(this).concat(['value', 'readOnly', 'mode']);
    }
  }, {
    key: "getOtherProps",
    value: function getOtherProps() {
      var otherProps = _get(_getPrototypeOf(Radio.prototype), "getOtherProps", this).call(this);

      otherProps.type = this.type;
      otherProps.onMouseDown = this.handleMouseDown;
      otherProps.onClick = otherProps.onChange;
      otherProps.onChange = noop;
      return otherProps;
    }
  }, {
    key: "showTooltip",
    value: function showTooltip(e) {
      if (_get(_getPrototypeOf(Radio.prototype), "showTooltip", this).call(this, e)) {
        return true;
      }

      var labelTooltip = this.labelTooltip,
          labelRef = this.labelRef;

      if (labelRef && (labelTooltip === TextTooltip.always || labelTooltip === TextTooltip.overflow && isOverflow(labelRef))) {
        var labelText = this.getLabelText();

        if (labelText) {
          var _this$context = this.context,
              getTooltipTheme = _this$context.getTooltipTheme,
              getTooltipPlacement = _this$context.getTooltipPlacement;
          show(labelRef, {
            title: labelText,
            theme: getTooltipTheme('label'),
            placement: getTooltipPlacement('label')
          });
          return true;
        }
      }

      return false;
    }
  }, {
    key: "renderWrapper",
    value: function renderWrapper() {
      var checked = this.isChecked();
      var floatLabel = _get(_getPrototypeOf(Radio.prototype), "hasFloatLabel", this) ? this.renderSwitchFloatLabel() : undefined;
      var tooltipHelp = this.showHelp === ShowHelp.tooltip ? this.renderTooltipHelp() : undefined;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("label", _extends({
        key: "wrapper"
      }, this.getWrapperProps()), /*#__PURE__*/React.createElement("input", _extends({}, this.getOtherProps(), {
        checked: checked,
        value: this.checkedValue
      })), this.renderInner(), this.getTextNode(!!tooltipHelp), tooltipHelp, this.renderFloatLabel()), floatLabel);
    }
    /**
     * 解决form 在float的时候没有表头的问题
     * 也可以在需要不在组件内部展现label的时候使用
     */

  }, {
    key: "renderSwitchFloatLabel",
    value: function renderSwitchFloatLabel() {
      return undefined;
    }
  }, {
    key: "renderInner",
    value: function renderInner() {
      return /*#__PURE__*/React.createElement("span", {
        className: "".concat(this.prefixCls, "-inner")
      });
    }
  }, {
    key: "renderTooltipHelp",
    value: function renderTooltipHelp() {
      return undefined;
    }
    /**
     * 当使用label代替children时，不需要展示float label
     *
     * @readonly
     * @memberof Radio
     */

  }, {
    key: "hasFloatLabel",
    get: function get() {
      return this.getLabelChildren() ? false : _get(_getPrototypeOf(Radio.prototype), "hasFloatLabel", this);
    }
    /**
     * 没有children时，使用label替代children
     *
     * @returns {ReactNode} label
     * @memberof Radio
     */

  }, {
    key: "getLabelChildren",
    value: function getLabelChildren() {
      var labelLayout = this.labelLayout;
      return labelLayout && ![LabelLayout.horizontal, LabelLayout.vertical, LabelLayout.none].includes(labelLayout) && this.getLabel();
    }
  }, {
    key: "getChildrenText",
    value: function getChildrenText() {
      return toJS(this.props.children);
    }
  }, {
    key: "getLabelText",
    value: function getLabelText() {
      return this.getChildrenText() || this.getLabelChildren();
    }
  }, {
    key: "getTextNode",
    value: function getTextNode(hasTooltipHelp) {
      var prefixCls = this.prefixCls;
      var text = this.getLabelText();

      if (text) {
        return /*#__PURE__*/React.createElement("span", {
          ref: this.saveLabelRef,
          className: "".concat(prefixCls, "-label"),
          style: hasTooltipHelp ? {
            flex: 'none'
          } : {}
        }, text);
      }
    }
  }, {
    key: "getWrapperClassNames",
    value: function getWrapperClassNames() {
      var _get2, _ref;

      var prefixCls = this.prefixCls,
          mode = this.props.mode;

      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      return (_get2 = _get(_getPrototypeOf(Radio.prototype), "getWrapperClassNames", this)).call.apply(_get2, [this, (_ref = {}, _defineProperty(_ref, "".concat(prefixCls, "-button"), mode === ViewMode.button), _defineProperty(_ref, "".concat(prefixCls, "-checked"), this.isChecked()), _ref)].concat(args));
    }
  }, {
    key: "isChecked",
    value: function isChecked() {
      var checked = this.props.checked;
      var name = this.name,
          dataSet = this.dataSet,
          checkedValue = this.checkedValue;

      if (dataSet && name) {
        return this.getDataSetValue() === checkedValue;
      }

      return checked;
    }
  }, {
    key: "handleMouseDown",
    value: function handleMouseDown(e) {
      // e.stopPropagation();
      var onMouseDown = this.props.onMouseDown;

      if (typeof onMouseDown === 'function') {
        onMouseDown(e);
      }
    }
  }, {
    key: "handleChange",
    value: function handleChange(e) {
      var _this$props$onClick = this.props.onClick,
          onClick = _this$props$onClick === void 0 ? noop : _this$props$onClick;
      var checked = e.target.checked;
      onClick(e);
      this.setChecked(checked);
    }
  }, {
    key: "setChecked",
    value: function setChecked(checked) {
      if (checked) {
        this.setValue(this.checkedValue);
      }
    }
  }, {
    key: "getOldValue",
    value: function getOldValue() {
      return this.isChecked() ? this.checkedValue : undefined;
    }
  }]);

  return Radio;
}(FormField);
Radio.displayName = 'Radio';
Radio.defaultProps = _objectSpread(_objectSpread({}, FormField.defaultProps), {}, {
  suffixCls: 'radio'
}); // eslint-disable-next-line camelcase

Radio.__PRO_RADIO = true; // eslint-disable-next-line camelcase

Radio.__IS_IN_CELL_EDITOR = true;

__decorate([autobind], Radio.prototype, "saveLabelRef", null);

__decorate([autobind], Radio.prototype, "handleMouseDown", null);

__decorate([autobind], Radio.prototype, "handleChange", null);

__decorate([action], Radio.prototype, "setChecked", null);

var ObserverRadio = /*#__PURE__*/function (_Radio) {
  _inherits(ObserverRadio, _Radio);

  var _super2 = _createSuper(ObserverRadio);

  function ObserverRadio() {
    _classCallCheck(this, ObserverRadio);

    return _super2.apply(this, arguments);
  }

  return _createClass(ObserverRadio);
}(Radio);

ObserverRadio.defaultProps = Radio.defaultProps; // eslint-disable-next-line camelcase

ObserverRadio.__PRO_RADIO = true; // eslint-disable-next-line camelcase

ObserverRadio.__IS_IN_CELL_EDITOR = true;
ObserverRadio = __decorate([observer], ObserverRadio);
export default ObserverRadio;
//# sourceMappingURL=Radio.js.map
