"use strict";

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

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

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

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

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

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

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

var _balloon = _interopRequireDefault(require("../balloon"));

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

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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

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

var Tooltip = _balloon["default"].Tooltip;

var Star =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Star, _React$Component);

  function Star(props) {
    var _this;

    _classCallCheck(this, Star);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Star).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onChange", function (e) {
      var starred = !_this.state.starred;

      if (!('starred' in _this.props)) {
        _this.setState({
          starred: starred
        });
      }

      _this.props.onChange(starred, e);

      _this.props.onClick && _this.props.onClick(e);
    });

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
      if (e.keyCode === _util.KEYCODE.ENTER || e.keyCode === _util.KEYCODE.SPACE) {
        _this.onChange(e);
      }

      _this.props.onKeyDown && _this.props.onKeyDown(e);
    });

    var _starred = 'starred' in props ? props.starred : props.defaultStarred;

    _this.state = {
      starred: typeof _starred === 'undefined' ? '' : _starred
    };
    return _this;
  }

  _createClass(Star, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('starred' in nextProps) {
        var starred = nextProps.starred;

        if (starred === undefined) {
          starred = false;
        }

        this.setState({
          starred: starred
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props = this.props,
          prefix = _this$props.prefix,
          className = _this$props.className,
          style = _this$props.style,
          text = _this$props.text,
          starredText = _this$props.starredText,
          align = _this$props.align,
          others = _objectWithoutProperties(_this$props, ["prefix", "className", "style", "text", "starredText", "align"]);

      var starred = this.state.starred;
      var type = starred ? 'star-fill' : 'starstroke';
      var iconCls = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "star"), true), _defineProperty(_classNames, "".concat(prefix, "starred"), starred), _defineProperty(_classNames, className, className), _classNames));

      var trigger = _react["default"].createElement("span", _extends({
        role: "star",
        tabIndex: "0",
        className: "".concat(prefix, "star-wrapper"),
        onClick: this.onChange,
        onKeyDown: this.onKeyDown
      }, _util.obj.pickAttrs(others)), _react["default"].createElement(_icon["default"], {
        type: type,
        className: iconCls
      }));

      if (text) {
        return _react["default"].createElement(Tooltip, {
          trigger: trigger,
          align: align
        }, starred ? starredText : text);
      } else {
        return trigger;
      }
    }
  }]);

  return Star;
}(_react["default"].Component);

_defineProperty(Star, "propTypes", {
  prefix: _propTypes["default"].string,

  /**
   * 自定义类名
   */
  className: _propTypes["default"].string,

  /**
   * 自定义内敛样式
   */
  style: _propTypes["default"].object,

  /**
   * 是否收藏(针对受控组件)
   */
  starred: _propTypes["default"].bool,

  /**
   * 星标默认值(针对非受控组件)
   */
  defaultStarred: _propTypes["default"].bool,

  /**
   * starred 为 false 的时候弹出文字
   */
  text: _propTypes["default"].string,

  /**
   * starred 为 true 的时候弹出文字
   */
  starredText: _propTypes["default"].string,

  /**
   * 与 tooltip 的 align 属性一致
   */
  align: _propTypes["default"].string,

  /**
   * 星标状态改变时触发此事件
   * @param {Boolean} starred
   * @param {Event} e DOM事件对象
   */
  onChange: _propTypes["default"].func,

  /**
   * 鼠标点击事件
   * @param {Event} e DOM事件对象
   */
  onClick: _propTypes["default"].func,

  /**
   * 键盘按键事件
   * @param {Event} e DOM事件对象
   */
  onKeyDown: _propTypes["default"].func
});

_defineProperty(Star, "defaultProps", {
  prefix: 'next-',
  onChange: function onChange() {},
  defaultStarred: false,
  align: 't'
});

var _default = _configProvider["default"].config(Star);

exports["default"] = _default;