"use strict";

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

var _zhCn = _interopRequireDefault(require("../locale/zh-cn.js"));

var _util = require("../util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 *
 * @param {Object|Boolean} input
 * @returns {Object} typeof obj.open === 'boolean'
 */
var parseBoundary = function parseBoundary(input) {
  var obj;

  if (input === undefined || input === null) {
    return {};
  } else if (typeof input === 'boolean') {
    obj = {
      open: input
    };
  } else {
    obj = _objectSpread({
      open: true
    }, input);
  }

  return obj;
};

function getContextProps(props, context, displayName) {
  var prefix = props.prefix,
      locale = props.locale,
      pure = props.pure,
      rtl = props.rtl,
      errorBoundary = props.errorBoundary,
      scriptUrl = props.scriptUrl;
  var nextPrefix = context.nextPrefix,
      nextLocale = context.nextLocale,
      nextPure = context.nextPure,
      nextWarning = context.nextWarning,
      nextRtl = context.nextRtl,
      nextErrorBoundary = context.nextErrorBoundary,
      nextScriptUrl = context.nextScriptUrl;
  var newPrefix = prefix || nextPrefix;
  var localeFromContext;

  if (nextLocale) {
    localeFromContext = nextLocale[displayName];

    if (localeFromContext) {
      localeFromContext.momentLocale = nextLocale.momentLocale;
    } else {
      localeFromContext = {
        momentLocale: 'zh-cn'
      };
    }
  }

  var newLocale;

  if (locale) {
    newLocale = _util.obj.deepMerge({}, _zhCn["default"][displayName], localeFromContext, locale);
  } else if (localeFromContext) {
    newLocale = _util.obj.deepMerge({}, _zhCn["default"][displayName], localeFromContext);
  }

  var newPure = typeof pure === 'boolean' ? pure : nextPure;
  var newRtl = typeof rtl === 'boolean' ? rtl : nextRtl; // ProtoType of [nextE|e]rrorBoundary can be one of [boolean, object]
  // but typeof newErrorBoundary === 'object'
  // newErrorBoundary should always have the key 'open', which indicates ErrorBoundary on or off

  var newErrorBoundary = _objectSpread({}, parseBoundary(nextErrorBoundary), {}, parseBoundary(errorBoundary));

  if (!('open' in newErrorBoundary)) {
    newErrorBoundary.open = false;
  }

  return {
    prefix: newPrefix,
    locale: newLocale,
    pure: newPure,
    rtl: newRtl,
    warning: nextWarning,
    errorBoundary: newErrorBoundary,
    scriptUrl: scriptUrl || nextScriptUrl
  };
}