import _objectSpread from "@babel/runtime/helpers/objectSpread2";
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 { observer } from 'mobx-react';
import { isArrayLike } from 'mobx';
import isPlainObject from 'lodash/isPlainObject';
import isNil from 'lodash/isNil';
import { FormField } from '../field/FormField';
import autobind from '../_util/autobind';
import { Tooltip as TextTooltip } from '../core/enum';
import { processFieldValue, renderMultiLine as _renderMultiLine, toRangeValue } from '../field/utils';
import isEmpty from '../_util/isEmpty';
import isOverflow from '../overflow-tip/util';
import { show } from '../tooltip/singleton';
import { defaultOutputRenderer } from './utils';

var Output = /*#__PURE__*/function (_FormField) {
  _inherits(Output, _FormField);

  var _super = _createSuper(Output);

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

    return _super.apply(this, arguments);
  }

  _createClass(Output, [{
    key: "isEditable",
    value: function isEditable() {
      return false;
    }
  }, {
    key: "useFocusedClassName",
    value: function useFocusedClassName() {
      return false;
    }
  }, {
    key: "handleChange",
    value: function handleChange() {// noop
    }
  }, {
    key: "getOmitPropsKeys",
    value: function getOmitPropsKeys() {
      return _get(_getPrototypeOf(Output.prototype), "getOmitPropsKeys", this).call(this).concat(['name', 'renderEmpty']);
    }
  }, {
    key: "getOtherPropsExcludeOutput",
    value: function getOtherPropsExcludeOutput(otherProps) {
      return otherProps;
    }
  }, {
    key: "getWrapperClassNamesExcludeOutput",
    value: function getWrapperClassNamesExcludeOutput() {
      return undefined;
    }
  }, {
    key: "getObservablePropsExcludeOutput",
    value: function getObservablePropsExcludeOutput() {
      return undefined;
    }
  }, {
    key: "getValueKey",
    value: function getValueKey(value) {
      if (isArrayLike(value)) {
        return value.map(this.getValueKey, this).join(',');
      }

      return this.processValue(value);
    }
  }, {
    key: "processValue",
    value: function processValue(value) {
      var _this = this;

      if (!isNil(value)) {
        var text = isPlainObject(value) ? value : _get(_getPrototypeOf(Output.prototype), "processValue", this).call(this, value);
        var field = this.field;
        return processFieldValue(text, field, {
          getProp: function getProp(name) {
            return _this.getProp(name);
          },
          getValue: function getValue() {
            return _this.getValue();
          },
          lang: this.lang,
          getDisplayProp: function getDisplayProp(name) {
            return _this.getDisplayProp(name);
          }
        }, true, this.record, this.getContextConfig);
      }

      return '';
    }
  }, {
    key: "defaultRenderer",
    value: function defaultRenderer(renderProps) {
      return defaultOutputRenderer(renderProps);
    }
    /**
     * 多行单元格渲染
     */

  }, {
    key: "renderMultiLine",
    value: function renderMultiLine(field) {
      var name = this.name,
          record = this.record,
          dataSet = this.dataSet,
          prefixCls = this.prefixCls,
          renderer = this.props.renderer;

      var _renderMultiLine2 = _renderMultiLine({
        name: name,
        field: field,
        record: record,
        dataSet: dataSet,
        prefixCls: prefixCls,
        renderer: renderer,
        renderValidationResult: this.renderValidationResult,
        isValidationMessageHidden: this.isValidationMessageHidden,
        processValue: this.processValue.bind(this),
        tooltip: this.props.tooltip,
        labelTooltip: this.labelTooltip
      }),
          lines = _renderMultiLine2.lines,
          multipleValidateMessageLength = _renderMultiLine2.multipleValidateMessageLength;

      this.multipleValidateMessageLength = multipleValidateMessageLength;
      return lines;
    }
  }, {
    key: "getRenderedValue",
    value: function getRenderedValue() {
      if (this.multiple) {
        var _this$renderMultipleV = this.renderMultipleValues(true),
            tags = _this$renderMultipleV.tags;

        return tags;
      }

      if (this.range) {
        return this.renderRangeValue(toRangeValue(this.getValue(), this.range));
      }
      /**
       * 多行单元格渲染
       */


      var field = this.field;

      if (field && field.get('multiLine', this.record)) {
        return this.renderMultiLine(field);
      }

      return this.processRenderer(this.getValue());
    }
  }, {
    key: "getTooltipTitle",
    value: function getTooltipTitle() {
      var _this2 = this;

      if (this.multiple) {
        var values = this.getValues();
        var repeats = new Map();
        var texts = values.map(function (v) {
          var key = _this2.getValueKey(v);

          var repeat = repeats.get(key) || 0;

          var text = _this2.processText(_this2.processValue(v));

          repeats.set(key, repeat + 1);

          if (!isNil(text)) {
            return text;
          }

          return undefined;
        });
        return texts.join(typeof this.multiple === 'string' ? this.multiple : '、');
      }

      return this.getRenderedValue();
    }
  }, {
    key: "showTooltip",
    value: function showTooltip(e) {
      if (_get(_getPrototypeOf(Output.prototype), "showTooltip", this).call(this, e)) {
        return true;
      }

      var _this$context = this.context,
          getTooltip = _this$context.getTooltip,
          getTooltipTheme = _this$context.getTooltipTheme,
          getTooltipPlacement = _this$context.getTooltipPlacement;
      var _this$props$tooltip = this.props.tooltip,
          tooltip = _this$props$tooltip === void 0 ? getTooltip('output') : _this$props$tooltip;
      var element = this.element,
          field = this.field;

      var _this$renderMultipleV2 = this.renderMultipleValues(true),
          isOverflowMaxTagCount = _this$renderMultipleV2.isOverflowMaxTagCount;

      var title = this.getTooltipTitle();
      var placement = getTooltipPlacement('output') || 'right';
      var theme = getTooltipTheme('output');

      if (element && !(field && field.get('multiLine', this.record)) && (tooltip === TextTooltip.always || tooltip === TextTooltip.overflow && (!this.multiple && isOverflow(element) || this.multiple && isOverflowMaxTagCount))) {
        if (title) {
          show(element, {
            title: title,
            placement: placement,
            theme: theme
          });
          return true;
        }
      }

      return false;
    }
  }, {
    key: "renderWrapper",
    value: function renderWrapper() {
      var result = this.getRenderedValue();
      var renderEmpty = this.props.renderEmpty;
      var text = isEmpty(result) || isArrayLike(result) && !result.length ? renderEmpty ? renderEmpty() : this.getContextConfig('renderEmpty')('Output') : result;
      var floatLabel = this.renderFloatLabel();
      return floatLabel ? /*#__PURE__*/React.createElement("span", _extends({}, this.getWrapperProps()), floatLabel, /*#__PURE__*/React.createElement("span", _extends({}, this.getOtherProps()), text)) : /*#__PURE__*/React.createElement("span", _extends({}, this.getMergedProps()), text);
    }
  }]);

  return Output;
}(FormField);

Output.displayName = 'Output'; // eslint-disable-next-line camelcase

Output.__PRO_OUTPUT = true;
Output.defaultProps = _objectSpread(_objectSpread({}, FormField.defaultProps), {}, {
  suffixCls: 'output'
});

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

__decorate([autobind], Output.prototype, "getValueKey", null);

__decorate([autobind], Output.prototype, "defaultRenderer", null);

Output = __decorate([observer], Output);
export default Output;
//# sourceMappingURL=Output.js.map
