"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames2 = _interopRequireDefault(require("classnames"));

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

var _icon = _interopRequireDefault(require("../icon"));

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 _typeof(obj) { 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); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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

/**
 * Tag.Colorable
 */
var Colorable =
/*#__PURE__*/
function (_Component) {
  _inherits(Colorable, _Component);

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

    return _possibleConstructorReturn(this, _getPrototypeOf(Colorable).apply(this, arguments));
  }

  _createClass(Colorable, [{
    key: "renderIcon",
    value: function renderIcon(leftCls) {
      var icon = this.props.icon;

      if (icon) {
        if (typeof icon === 'string') {
          return _react["default"].createElement(_icon["default"], {
            type: icon,
            className: leftCls
          });
        } else {
          return _react["default"].cloneElement(icon, {
            className: leftCls
          });
        }
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          prefix = _this$props.prefix,
          className = _this$props.className,
          size = _this$props.size,
          onClose = _this$props.onClose,
          type = _this$props.type,
          color = _this$props.color,
          children = _this$props.children,
          icon = _this$props.icon,
          rtl = _this$props.rtl,
          value = _this$props.value,
          href = _this$props.href,
          target = _this$props.target,
          style = _this$props.style,
          others = _objectWithoutProperties(_this$props, ["prefix", "className", "size", "onClose", "type", "color", "children", "icon", "rtl", "value", "href", "target", "style"]);

      var classNames = (0, _classnames2["default"])("".concat(prefix, "color-tag"), "".concat(prefix, "color-tag-").concat(color), className, {
        pointer: type === 'closeable'
      }, {
        interactive: type === 'closeable' || type === 'link'
      }, _defineProperty({}, "".concat(prefix, "color-s-tag"), size === 'small'));
      var leftCls = size === 'small' ? "".concat(prefix, "color-s-tag-left-icon") : "".concat(prefix, "color-tag-left-icon");
      var rightCls = size === 'small' ? "".concat(prefix, "color-s-tag-right-icon") : "".concat(prefix, "color-tag-right-icon");
      var linkCls = size === 'small' ? "".concat(prefix, "color-s-tag-right-link-icon") : "".concat(prefix, "color-tag-right-link-icon");

      if (type !== 'link') {
        return _react["default"].createElement("span", _extends({
          className: classNames,
          dir: rtl ? 'rtl' : undefined,
          style: style
        }, _util.obj.pickAttrs(others)), this.renderIcon(leftCls), _react["default"].createElement("span", {
          className: "".concat(prefix, "color-tag-children")
        }, children), type === 'closeable' ? _react["default"].createElement(_icon["default"], {
          type: "remove",
          onClick: onClose ? function () {
            return onClose(value);
          } : function () {},
          className: rightCls
        }) : null);
      } else {
        return _react["default"].createElement("a", _extends({
          className: classNames,
          dir: rtl ? 'rtl' : undefined,
          href: href,
          target: target,
          style: style
        }, _util.obj.pickAttrs(others)), icon && _react["default"].createElement(_icon["default"], {
          type: icon,
          className: "".concat(prefix, "color-tag-left-icon")
        }), _react["default"].createElement("span", {
          className: "".concat(prefix, "color-tag-children")
        }, children), _react["default"].createElement(_icon["default"], {
          type: "chevron-right-s",
          className: linkCls
        }));
      }
    }
  }]);

  return Colorable;
}(_react.Component);

_defineProperty(Colorable, "propTypes", {
  /**
   * 标签类名前缀,提供给二次开发者用
   * @default next-
   */
  prefix: _propTypes["default"].string,

  /**
   * color 标签的颜色，可选值 'red'、'green'、'yellow'、'blue'、'gray'
   */
  color: _propTypes["default"].string,

  /**
   * size 标签尺寸
   */
  size: _propTypes["default"].oneOf(['small', 'medium']),

  /**
   * type 标签的类型，可选值 'normal'、'closeable'、'link'
   */
  type: _propTypes["default"].oneOf(['normal', 'closeable', 'link']),

  /**
   * icon 左侧图标
   */
  icon: _propTypes["default"].any,

  /**
   * value 表示 tag 的值
   */
  value: _propTypes["default"].any,

  /**
   * onClose 点击回调，只有当 type=closeable 时有
   */
  onClose: _propTypes["default"].func,
  className: _propTypes["default"].string,
  href: _propTypes["default"].string,
  target: _propTypes["default"].string,
  children: _propTypes["default"].any,
  rtl: _propTypes["default"].bool,
  style: _propTypes["default"].object
});

_defineProperty(Colorable, "defaultProps", {
  prefix: 'next-',
  size: 'small',
  color: 'blue',
  type: 'normal',
  target: '_self'
});

var _default = Colorable;
exports["default"] = _default;