import * as _vue from "vue";

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(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(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; }

import { reactive, provide } from 'vue';
import PropTypes from '../_util/vue-types';
import { getComponent, getSlot } from '../_util/props-util';
import defaultRenderEmpty from './renderEmpty';
import LocaleProvider, { ANT_MARK } from '../locale-provider';
import LocaleReceiver from '../locale-provider/LocaleReceiver';

function getWatch() {
  var keys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var watch = {};
  keys.forEach(function (k) {
    watch[k] = function (value) {
      this.configProvider[k] = value;
    };
  });
  return watch;
}

var ConfigProvider = {
  name: 'AConfigProvider',
  props: {
    getPopupContainer: PropTypes.func,
    prefixCls: PropTypes.string,
    renderEmpty: PropTypes.func,
    csp: PropTypes.object,
    autoInsertSpaceInButton: PropTypes.bool,
    locale: PropTypes.object,
    pageHeader: PropTypes.object,
    transformCellText: PropTypes.func
  },
  created: function created() {
    this.configProvider = reactive(_objectSpread(_objectSpread({}, this.$props), {}, {
      getPrefixCls: this.getPrefixCls,
      renderEmpty: this.renderEmptyComponent
    }));
    provide('configProvider', this.configProvider);
  },
  watch: _objectSpread({}, getWatch(['prefixCls', 'csp', 'autoInsertSpaceInButton', 'locale', 'pageHeader', 'transformCellText'])),
  methods: {
    renderEmptyComponent: function renderEmptyComponent(name) {
      var renderEmpty = getComponent(this, 'renderEmpty', {}, false) || defaultRenderEmpty;
      return renderEmpty(name);
    },
    getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) {
      var _this$$props$prefixCl = this.$props.prefixCls,
          prefixCls = _this$$props$prefixCl === void 0 ? 'ant' : _this$$props$prefixCl;
      if (customizePrefixCls) return customizePrefixCls;
      return suffixCls ? "".concat(prefixCls, "-").concat(suffixCls) : prefixCls;
    },
    renderProvider: function renderProvider(legacyLocale) {
      var _this = this;

      return _vue.createVNode(LocaleProvider, {
        "locale": this.locale || legacyLocale,
        "_ANT_MARK__": ANT_MARK
      }, {
        default: function _default() {
          return [getSlot(_this)];
        }
      });
    }
  },
  render: function render() {
    var _this2 = this;

    return _vue.createVNode(LocaleReceiver, {
      "children": function children(_, __, legacyLocale) {
        return _this2.renderProvider(legacyLocale);
      }
    }, null);
  }
};
export var ConfigConsumerProps = {
  getPrefixCls: function getPrefixCls(suffixCls, customizePrefixCls) {
    if (customizePrefixCls) return customizePrefixCls;
    return "ant-".concat(suffixCls);
  },
  renderEmpty: defaultRenderEmpty
};
/* istanbul ignore next */

ConfigProvider.install = function (app) {
  app.component(ConfigProvider.name, ConfigProvider);
};

export default ConfigProvider;