import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { Component } from 'react';
import moment from 'moment';
import { getContext, Symbols } from 'choerodon-ui/shared';
import { changeConfirmLocale } from '../modal/locale';
import { changeNoticeLocale } from '../notification/locale';
export var LocaleContext = getContext(Symbols.LocaleContext, {});

function setMomentLocale(locale) {
  if (locale && locale.locale) {
    moment.locale(locale.locale);
  } else {
    moment.locale('en');
  }
}

var LocaleProvider = /*#__PURE__*/function (_Component) {
  _inherits(LocaleProvider, _Component);

  var _super = _createSuper(LocaleProvider);

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

    return _super.apply(this, arguments);
  }

  _createClass(LocaleProvider, [{
    key: "getContextValue",
    value: function getContextValue() {
      var locale = this.props.locale;
      return {
        c7nLocale: _objectSpread(_objectSpread({}, 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;
      changeConfirmLocale(locale && locale.Modal);
      changeNoticeLocale(locale && locale.Notification);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      changeConfirmLocale();
      changeNoticeLocale();
    }
  }, {
    key: "render",
    value: function render() {
      var children = this.props.children;
      return /*#__PURE__*/React.createElement(LocaleContext.Provider, {
        value: this.getContextValue()
      }, children);
    }
  }]);

  return LocaleProvider;
}(Component);

export { LocaleProvider as default };
LocaleProvider.defaultProps = {
  locale: {}
};
//# sourceMappingURL=index.js.map
