"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"] = exports.LocaleContext = void 0;

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

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

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

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

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

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

var _moment = _interopRequireDefault(require("moment"));

var _shared = require("choerodon-ui/shared");

var _locale = require("../modal/locale");

var _locale2 = require("../notification/locale");

var LocaleContext = (0, _shared.getContext)(_shared.Symbols.LocaleContext, {});
exports.LocaleContext = LocaleContext;

function setMomentLocale(locale) {
  if (locale && locale.locale) {
    _moment["default"].locale(locale.locale);
  } else {
    _moment["default"].locale('en');
  }
}

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

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

  function LocaleProvider() {
    (0, _classCallCheck2["default"])(this, LocaleProvider);
    return _super.apply(this, arguments);
  }

  (0, _createClass2["default"])(LocaleProvider, [{
    key: "getContextValue",
    value: function getContextValue() {
      var locale = this.props.locale;
      return {
        c7nLocale: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, locale), {}, {
          exist: true
        })
      };
    }
  }, {
    key: "componentWillMount",
    value: function componentWillMount() {
      var locale = this.props.locale;
      setMomentLocale(locale);
      this.componentDidUpdate();
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      var locale = this.props.locale;
      var nextLocale = nextProps.locale;

      if (locale !== nextLocale) {
        setMomentLocale(nextProps.locale);
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      var locale = this.props.locale;
      (0, _locale.changeConfirmLocale)(locale && locale.Modal);
      (0, _locale2.changeNoticeLocale)(locale && locale.Notification);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      (0, _locale.changeConfirmLocale)();
      (0, _locale2.changeNoticeLocale)();
    }
  }, {
    key: "render",
    value: function render() {
      var children = this.props.children;
      return /*#__PURE__*/_react["default"].createElement(LocaleContext.Provider, {
        value: this.getContextValue()
      }, children);
    }
  }]);
  return LocaleProvider;
}(_react.Component);

exports["default"] = LocaleProvider;
LocaleProvider.defaultProps = {
  locale: {}
};
//# sourceMappingURL=index.js.map
