"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

var _vue = _interopRequireWildcard(require("vue"));

var _wave = _interopRequireDefault(require("../_util/wave"));

var _LoadingOutlined = _interopRequireDefault(require("@ant-design/icons-vue/LoadingOutlined"));

var _buttonTypes = _interopRequireDefault(require("./buttonTypes"));

var _propsUtil = require("../_util/props-util");

var _configProvider = require("../config-provider");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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; }

// eslint-disable-next-line no-console
var rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
var isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
var props = (0, _buttonTypes.default)();
var _default2 = {
  name: 'AButton',
  inheritAttrs: false,
  __ANT_BUTTON: true,
  props: props,
  setup: function setup() {
    return {
      configProvider: (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps)
    };
  },
  data: function data() {
    this.children = [];
    this.iconCom = undefined;
    return {
      sizeMap: {
        large: 'lg',
        small: 'sm'
      },
      sLoading: !!this.loading,
      hasTwoCNChar: false
    };
  },
  watch: {
    loading: function loading(val, preVal) {
      var _this = this;

      if (preVal && typeof preVal !== 'boolean') {
        clearTimeout(this.delayTimeout);
      }

      if (val && typeof val !== 'boolean' && val.delay) {
        this.delayTimeout = setTimeout(function () {
          _this.sLoading = !!val;
        }, val.delay);
      } else {
        this.sLoading = !!val;
      }
    }
  },
  mounted: function mounted() {
    this.fixTwoCNChar();
  },
  updated: function updated() {
    this.fixTwoCNChar();
  },
  beforeUnmount: function beforeUnmount() {
    if (this.delayTimeout) {
      clearTimeout(this.delayTimeout);
    }
  },
  methods: {
    getClasses: function getClasses() {
      var _ref;

      var customizePrefixCls = this.prefixCls,
          type = this.type,
          shape = this.shape,
          size = this.size,
          hasTwoCNChar = this.hasTwoCNChar,
          sLoading = this.sLoading,
          ghost = this.ghost,
          block = this.block,
          $attrs = this.$attrs;
      var getPrefixCls = this.configProvider.getPrefixCls;
      var prefixCls = getPrefixCls('btn', customizePrefixCls);
      var autoInsertSpace = this.configProvider.autoInsertSpaceInButton !== false; // large => lg
      // small => sm

      var sizeCls = '';

      switch (size) {
        case 'large':
          sizeCls = 'lg';
          break;

        case 'small':
          sizeCls = 'sm';
          break;

        default:
          break;
      }

      var iconType = sLoading ? 'loading' : this.iconCom;
      return _ref = {}, _defineProperty(_ref, $attrs.class, $attrs.class), _defineProperty(_ref, "".concat(prefixCls), true), _defineProperty(_ref, "".concat(prefixCls, "-").concat(type), type), _defineProperty(_ref, "".concat(prefixCls, "-").concat(shape), shape), _defineProperty(_ref, "".concat(prefixCls, "-").concat(sizeCls), sizeCls), _defineProperty(_ref, "".concat(prefixCls, "-icon-only"), this.children.length === 0 && iconType), _defineProperty(_ref, "".concat(prefixCls, "-loading"), sLoading), _defineProperty(_ref, "".concat(prefixCls, "-background-ghost"), ghost || type === 'ghost'), _defineProperty(_ref, "".concat(prefixCls, "-two-chinese-chars"), hasTwoCNChar && autoInsertSpace), _defineProperty(_ref, "".concat(prefixCls, "-block"), block), _ref;
    },
    fixTwoCNChar: function fixTwoCNChar() {
      // Fix for HOC usage like <FormatMessage />
      var node = this.$refs.buttonNode;

      if (!node) {
        return;
      }

      var buttonText = node.textContent;

      if (this.isNeedInserted() && isTwoCNChar(buttonText)) {
        if (!this.hasTwoCNChar) {
          this.hasTwoCNChar = true;
        }
      } else if (this.hasTwoCNChar) {
        this.hasTwoCNChar = false;
      }
    },
    handleClick: function handleClick(event) {
      var sLoading = this.$data.sLoading;

      if (sLoading) {
        return;
      }

      this.$emit('click', event);
    },
    insertSpace: function insertSpace(child, needInserted) {
      var SPACE = needInserted ? ' ' : '';

      if (child.type === _vue.Text) {
        var text = child.children.trim();

        if (isTwoCNChar(text)) {
          text = text.split('').join(SPACE);
        }

        return _vue.createVNode("span", null, [text]);
      }

      return child;
    },
    isNeedInserted: function isNeedInserted() {
      var iconCom = this.iconCom,
          type = this.type;
      return this.children.length === 1 && !iconCom && type !== 'link';
    }
  },
  render: function render() {
    var _this2 = this;

    this.iconCom = (0, _propsUtil.getComponent)(this, 'icon');
    var type = this.type,
        htmlType = this.htmlType,
        iconCom = this.iconCom,
        disabled = this.disabled,
        handleClick = this.handleClick,
        sLoading = this.sLoading,
        $attrs = this.$attrs;
    var children = (0, _propsUtil.getSlot)(this);
    this.children = children;
    var classes = this.getClasses();

    var buttonProps = _objectSpread(_objectSpread({}, $attrs), {}, {
      disabled: disabled,
      class: classes,
      onClick: handleClick
    });

    var iconNode = sLoading ? _vue.createVNode(_LoadingOutlined.default, null, null) : iconCom;
    var autoInsertSpace = this.configProvider.autoInsertSpaceInButton !== false;
    var kids = children.map(function (child) {
      return _this2.insertSpace(child, _this2.isNeedInserted() && autoInsertSpace);
    });

    if ($attrs.href !== undefined) {
      return _vue.createVNode("a", _vue.mergeProps(buttonProps, {
        "ref": "buttonNode"
      }), [iconNode, kids]);
    }

    var buttonNode = _vue.createVNode("button", _vue.mergeProps(buttonProps, {
      "ref": "buttonNode",
      "type": htmlType || 'button'
    }), [iconNode, kids]);

    if (type === 'link') {
      return buttonNode;
    }

    return _vue.createVNode(_wave.default, {
      "ref": "wave"
    }, {
      default: function _default() {
        return [buttonNode];
      }
    });
  }
};
exports.default = _default2;