"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _get3 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _tslib = require("tslib");

var _react = _interopRequireDefault(require("react"));

var _mobxReact = require("mobx-react");

var _mobx = require("mobx");

var _classnames = _interopRequireDefault(require("classnames"));

var _enum = require("../../../lib/progress/enum");

var _KeyCode = _interopRequireDefault(require("../../../lib/_util/KeyCode"));

var _debounce = _interopRequireDefault(require("lodash/debounce"));

var _isNil = _interopRequireDefault(require("lodash/isNil"));

var _TextArea2 = _interopRequireDefault(require("../text-area/TextArea"));

var _TextField = require("../text-field/TextField");

var _enum2 = require("../text-area/enum");

var _icon = _interopRequireDefault(require("../icon"));

var _IntlList = _interopRequireDefault(require("./IntlList"));

var _enum3 = require("./enum");

var _Modal = _interopRequireDefault(require("../modal/Modal"));

var _localeContext = _interopRequireWildcard(require("../locale-context"));

var _progress = _interopRequireDefault(require("../progress"));

var _enum4 = require("../core/enum");

var _message = _interopRequireDefault(require("../message"));

var _exception = _interopRequireDefault(require("../_util/exception"));

var _autobind = _interopRequireDefault(require("../_util/autobind"));

var _EventManager = require("../_util/EventManager");

var _isSame = _interopRequireDefault(require("../_util/isSame"));

var _isEmpty = _interopRequireDefault(require("../_util/isEmpty"));

var _singleton = require("../tooltip/singleton");

var _util = _interopRequireDefault(require("../overflow-tip/util"));

var IntlField = /*#__PURE__*/function (_TextArea) {
  (0, _inherits2["default"])(IntlField, _TextArea);

  var _super = (0, _createSuper2["default"])(IntlField);

  function IntlField(props, context) {
    var _this;

    (0, _classCallCheck2["default"])(this, IntlField);
    _this = _super.call(this, props, context);
    _this.openModal = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var _this$props, modalProps, maxLengths, type, rows, cols, resize, _assertThisInitialize, record, lang, name, element, maxLength, supports, maxLengthList;

      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (_this.modal) {
                _context.next = 24;
                break;
              }

              _this$props = _this.props, modalProps = _this$props.modalProps, maxLengths = _this$props.maxLengths, type = _this$props.type, rows = _this$props.rows, cols = _this$props.cols, resize = _this$props.resize;
              _assertThisInitialize = (0, _assertThisInitialized2["default"])(_this), record = _assertThisInitialize.record, lang = _assertThisInitialize.lang, name = _assertThisInitialize.name, element = _assertThisInitialize.element;
              maxLength = _this.getProp('maxLength');
              supports = _localeContext["default"].supports;
              maxLengthList = {};
              Object.keys(supports).map(function (key) {
                maxLengthList[key] = maxLengths && (key !== lang || (0, _isNil["default"])(maxLength)) ? maxLengths[key] || maxLength : maxLength;
                return null;
              });

              if (!(record && name)) {
                _context.next = 23;
                break;
              }

              _this.setLoading(true);

              _context.prev = 9;

              if (element && !(0, _isSame["default"])(_this.getValue(), element.value)) {
                _this.syncValueOnBlur(element.value);
              }

              _context.next = 13;
              return record.tls(name);

            case 13:
              _context.next = 19;
              break;

            case 15:
              _context.prev = 15;
              _context.t0 = _context["catch"](9);

              _message["default"].error((0, _exception["default"])(_context.t0));

              return _context.abrupt("return");

            case 19:
              _context.prev = 19;

              _this.setLoading(false);

              return _context.finish(19);

            case 22:
              _this.storeLocales(record, name);

            case 23:
              _this.modal = _Modal["default"].open((0, _objectSpread2["default"])({
                title: (0, _localeContext.$l)('IntlField', 'modal_title'),
                children: /*#__PURE__*/_react["default"].createElement(_IntlList["default"], {
                  readOnly: _this.readOnly,
                  disabled: _this.disabled,
                  record: record,
                  name: name,
                  lang: lang,
                  maxLengths: maxLengthList,
                  type: type,
                  rows: rows,
                  cols: cols,
                  resize: resize,
                  getConfig: _this.getContextConfig
                }),
                onClose: _this.handleIntlListClose,
                onOk: _this.handleIntlListOk,
                onCancel: _this.handleIntlListCancel,
                okButton: !_this.disabled && !_this.readOnly,
                destroyOnClose: true
              }, modalProps));

            case 24:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[9, 15, 19, 22]]);
    }));
    _this.handleIntlListClose = /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              delete _this.modal;

              if (!_this.props.displayOutput) {
                _this.focus();
              }

            case 2:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));
    var suffixCls = _this.props.displayOutput ? 'output' : _this.props.type !== _enum3.IntlType.multipleLine ? 'input' : 'textarea';
    _this.prefixCls = _this.getContextProPrefixCls(suffixCls, props.prefixCls);
    return _this;
  }

  (0, _createClass2["default"])(IntlField, [{
    key: "range",
    get: function get() {
      return false;
    }
  }, {
    key: "readOnly",
    get: function get() {
      if (this.props.displayOutput) {
        return true;
      }

      return this.isReadOnly();
    }
  }, {
    key: "resize",
    get: function get() {
      if (this.props.displayOutput) {
        return _enum2.ResizeType.none;
      }

      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "resize", this);
    }
  }, {
    key: "autoSize",
    get: function get() {
      if (this.props.displayOutput) {
        return false;
      }

      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "autoSize", this);
    }
  }, {
    key: "border",
    get: function get() {
      if (this.props.displayOutput) {
        return false;
      }

      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "border", this);
    }
  }, {
    key: "saveSuffixRef",
    value: function saveSuffixRef(node) {
      this.suffixRef = node;
    }
  }, {
    key: "getEditorTextInfo",
    value: function getEditorTextInfo(rangeTarget) {
      var superText = (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "getEditorTextInfo", this).call(this, rangeTarget);
      return this.props.displayOutput ? (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, superText), {}, {
        placeholder: undefined
      }) : superText;
    }
  }, {
    key: "getPlaceholders",
    value: function getPlaceholders() {
      if (this.props.displayOutput) {
        return [];
      }

      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "getPlaceholders", this).call(this);
    }
  }, {
    key: "isEditable",
    value: function isEditable() {
      if (this.props.displayOutput) {
        return false;
      }

      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "isEditable", this).call(this);
    }
  }, {
    key: "setLoading",
    value: function setLoading(loading) {
      this.loading = loading;
    }
  }, {
    key: "handleIntlListOk",
    value: function () {
      var _handleIntlListOk = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
        var supports, languages, record, name, field, tlsKey;
        return _regenerator["default"].wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                supports = _localeContext["default"].supports;
                languages = Object.keys(supports);
                record = this.record, name = this.name, field = this.field;

                if (!(record && field)) {
                  _context3.next = 8;
                  break;
                }

                tlsKey = this.getContextConfig('tlsKey');
                _context3.next = 7;
                return Promise.all(languages.map(function (language) {
                  var intlField = record.dataSet.getField("".concat(tlsKey, ".").concat(name, ".").concat(language));
                  return intlField ? intlField.checkValidity(record) : true;
                }));

              case 7:
                return _context3.abrupt("return", _context3.sent.every(Boolean));

              case 8:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      function handleIntlListOk() {
        return _handleIntlListOk.apply(this, arguments);
      }

      return handleIntlListOk;
    }()
  }, {
    key: "handleIntlListCancel",
    value: function () {
      var _handleIntlListCancel = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
        var record, locales;
        return _regenerator["default"].wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                if (!(this.disabled || this.readOnly)) {
                  _context4.next = 2;
                  break;
                }

                return _context4.abrupt("return");

              case 2:
                record = this.record, locales = this.locales;

                if (record && locales) {
                  record.set(locales);
                }

              case 4:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

      function handleIntlListCancel() {
        return _handleIntlListCancel.apply(this, arguments);
      }

      return handleIntlListCancel;
    }()
  }, {
    key: "handleKeyDown",
    value: function handleKeyDown(e) {
      if (e.keyCode === _KeyCode["default"].DOWN && this.props.type !== _enum3.IntlType.multipleLine) {
        (0, _EventManager.stopEvent)(e);
        this.openModal();
      }

      (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "handleKeyDown", this).call(this, e);
    }
  }, {
    key: "handleBlur",
    value: function handleBlur(e) {
      if (this.modal) {
        e.preventDefault();
      }

      (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "handleBlur", this).call(this, e);
    }
  }, {
    key: "storeLocales",
    value: function storeLocales(record, name) {
      var tlsKey = "".concat(this.getContextConfig('tlsKey'), ".").concat(name);
      var tls = record.get(tlsKey);
      var locales = (0, _defineProperty2["default"])({}, name, record.get(name));

      if (tls) {
        var defaultLang = _localeContext["default"].locale.lang,
            supports = _localeContext["default"].supports;
        Object.keys(supports).forEach(function (lang) {
          var value = tls[lang];

          if (lang === defaultLang) {
            locales["".concat(tlsKey, ".").concat(lang)] = locales[name];
          } else {
            locales["".concat(tlsKey, ".").concat(lang)] = value;
          }
        });
      }

      this.locales = locales;
    }
  }, {
    key: "getOmitPropsKeys",
    value: function getOmitPropsKeys() {
      if (this.props.type === _enum3.IntlType.multipleLine && !this.props.displayOutput) {
        return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "getOmitPropsKeys", this).call(this).concat(['type', 'displayOutput']);
      }

      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "getOmitPropsKeys", this).call(this).concat(['cols', 'rows', 'wrap', 'resize', 'autoSize', 'onResize', 'type', 'displayOutput']);
    }
  }, {
    key: "getOtherProps",
    value: function getOtherProps() {
      if (this.props.type === _enum3.IntlType.multipleLine) {
        return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "getOtherProps", this).call(this);
      }

      return _TextField.TextField.prototype.getOtherProps.call(this);
    }
  }, {
    key: "getWrapperClassNames",
    value: function getWrapperClassNames() {
      var _get2;

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

      return (_get2 = (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "getWrapperClassNames", this)).call.apply(_get2, [this, "".concat(this.prefixCls, "-intl")].concat(args));
    }
  }, {
    key: "getSuffix",
    value: function getSuffix() {
      var suffix = this.props.suffix;
      return this.wrapperSuffix(this.loading ? /*#__PURE__*/_react["default"].createElement(_progress["default"], {
        size: _enum4.Size.small,
        type: _enum.ProgressType.loading
      }) : suffix || /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        className: "".concat(this.prefixCls, "-intl"),
        type: "language"
      }), {
        onClick: (0, _debounce["default"])(this.openModal, 200)
      });
    }
  }, {
    key: "handleEnterDown",
    value: function handleEnterDown(e) {
      if (this.props.type === _enum3.IntlType.multipleLine) {
        (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "handleEnterDown", this).call(this, e);
      } else {
        _TextField.TextField.prototype.handleEnterDown.call(this, e);
      }
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps, nextContext) {
      if (nextProps.type !== this.props.type) {
        var suffixCls = nextProps.type !== _enum3.IntlType.multipleLine ? 'input' : 'textarea';
        this.prefixCls = this.getContextProPrefixCls(suffixCls, nextProps.prefixCls);
      }

      (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "componentWillReceiveProps", this).call(this, nextProps, nextContext);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.modal) {
        this.modal.close();
      }
    }
  }, {
    key: "renderWrapper",
    value: function renderWrapper() {
      if (this.props.displayOutput) {
        return this.renderOutput();
      }

      if (this.props.type === _enum3.IntlType.multipleLine) {
        return (0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "renderWrapper", this).call(this);
      }

      return this.renderGroup();
    }
  }, {
    key: "showTooltip",
    value: function showTooltip(e) {
      if ((0, _get3["default"])((0, _getPrototypeOf2["default"])(IntlField.prototype), "showTooltip", this).call(this, e)) {
        return true;
      }

      if (this.props.displayOutput) {
        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;

        if (element && !(field && field.get('multiLine', this.record)) && (tooltip === _enum4.Tooltip.always || tooltip === _enum4.Tooltip.overflow && (0, _util["default"])(element))) {
          var title = this.getRenderedValue();

          if (title) {
            (0, _singleton.show)(element, {
              title: title,
              placement: getTooltipPlacement('output') || 'right',
              theme: getTooltipTheme('output')
            });
            return true;
          }
        }
      }

      return false;
    }
  }, {
    key: "renderOutput",
    value: function renderOutput() {
      var result = this.getRenderedValue();
      var text = (0, _isEmpty["default"])(result) || (0, _mobx.isArrayLike)(result) && !result.length ? this.getContextConfig('renderEmpty')('Output') : result;
      var floatLabel = this.renderFloatLabel();
      var suffix = this.getSuffix();
      var className = (0, _classnames["default"])("".concat(this.prefixCls, "-intl-wrapper"));

      if (floatLabel) {
        return /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, this.getWrapperProps()), floatLabel, /*#__PURE__*/_react["default"].createElement("span", {
          className: className
        }, /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, this.getOtherProps()), text), suffix));
      }

      return /*#__PURE__*/_react["default"].createElement("span", {
        className: className
      }, /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, this.getMergedProps()), text), suffix);
    }
  }]);
  return IntlField;
}(_TextArea2["default"]);

IntlField.displayName = 'IntlField';
IntlField.defaultProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _TextArea2["default"].defaultProps), {}, {
  rows: 3,
  resize: _enum2.ResizeType.vertical,
  type: _enum3.IntlType.singleLine
});
(0, _tslib.__decorate)([_mobx.observable], IntlField.prototype, "loading", void 0);
(0, _tslib.__decorate)([_mobx.computed], IntlField.prototype, "readOnly", null);
(0, _tslib.__decorate)([_autobind["default"], _mobx.action], IntlField.prototype, "saveSuffixRef", null);
(0, _tslib.__decorate)([_mobx.action], IntlField.prototype, "setLoading", null);
(0, _tslib.__decorate)([_autobind["default"]], IntlField.prototype, "handleIntlListOk", null);
(0, _tslib.__decorate)([_autobind["default"]], IntlField.prototype, "handleIntlListCancel", null);
(0, _tslib.__decorate)([_autobind["default"]], IntlField.prototype, "handleKeyDown", null);
(0, _tslib.__decorate)([_autobind["default"]], IntlField.prototype, "handleBlur", null);
IntlField = (0, _tslib.__decorate)([_mobxReact.observer], IntlField);
var _default = IntlField;
exports["default"] = _default;
//# sourceMappingURL=IntlField.js.map
