"use strict";

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

require("maycur-antd/lib/icon/style/css");

var _icon = _interopRequireDefault(require("maycur-antd/lib/icon"));

require("maycur-antd/lib/input/style/css");

var _input = _interopRequireDefault(require("maycur-antd/lib/input"));

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

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

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

require("rc-trigger/assets/index.css");

var _rcTrigger = _interopRequireDefault(require("rc-trigger"));

var _utils = _interopRequireDefault(require("../utils/utils"));

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

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

var prefix = _utils["default"].prefixCls;

var MkSelect =
/*#__PURE__*/
function (_Component) {
  _inherits(MkSelect, _Component);

  function MkSelect(props) {
    var _this;

    _classCallCheck(this, MkSelect);

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

    _this.onFocus = function (e) {
      var onFocus = _this.props.onFocus;
      onFocus && onFocus();
    };

    _this.onChange = function (option) {
      var _this$props = _this.props,
          onChange = _this$props.onChange,
          popupVisible = _this$props.popupVisible;
      onChange && onChange(option);

      if (!popupVisible) {
        _this.setState({
          popupVisible: false
        });
      }
    };

    _this.onPopupVisibleChange = function (open) {
      var _this$props2 = _this.props,
          onPopupVisibleChange = _this$props2.onPopupVisibleChange,
          popupVisible = _this$props2.popupVisible,
          disabled = _this$props2.disabled;

      if (popupVisible) {
        return;
      }

      if (open && !disabled) {
        _this.setState({
          popupVisible: true
        });
      } else {
        _this.setState({
          popupVisible: false
        });
      }

      onPopupVisibleChange && onPopupVisibleChange();
    };

    _this.getTargetTop = function (targetTop) {
      _this.targetTop = targetTop || 0;
    };

    _this.state = {
      popupVisible: false
    };
    return _this;
  }

  _createClass(MkSelect, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props3 = this.props,
          popupVisible = _this$props3.popupVisible,
          defaultPopupVisible = _this$props3.defaultPopupVisible;

      if (popupVisible || defaultPopupVisible) {
        this.setState({
          popupVisible: true
        });
      }

      if (this.$innerRef) {
        this.dropdown_width = this.$innerRef.clientWidth;
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames2,
          _classNames3,
          _classNames4,
          _this2 = this;

      var _this$props4 = this.props,
          children = _this$props4.children,
          placeholder = _this$props4.placeholder,
          disabled = _this$props4.disabled,
          style = _this$props4.style,
          dropdownStyle = _this$props4.dropdownStyle,
          actionVisible = _this$props4.actionVisible,
          iconClass = _this$props4.iconClass,
          onBlur = _this$props4.onBlur,
          onClick = _this$props4.onClick,
          onMouseEnter = _this$props4.onMouseEnter,
          onMouseLeave = _this$props4.onMouseLeave,
          popupClassName = _this$props4.popupClassName,
          destroyPopupOnHide = _this$props4.destroyPopupOnHide,
          getPopupClassNameFromAlign = _this$props4.getPopupClassNameFromAlign,
          action = _this$props4.action,
          mouseEnterDelay = _this$props4.mouseEnterDelay,
          mouseLeaveDelay = _this$props4.mouseLeaveDelay,
          popupStyle = _this$props4.popupStyle,
          prefixCls = _this$props4.prefixCls,
          popupTransitionName = _this$props4.popupTransitionName,
          defaultPopupVisible = _this$props4.defaultPopupVisible,
          popupAlign = _this$props4.popupAlign,
          popupPlacement = _this$props4.popupPlacement,
          getPopupContainer = _this$props4.getPopupContainer,
          className = _this$props4.className,
          value = _this$props4.value,
          category = _this$props4.category;
      var popupVisible = this.state.popupVisible;
      var selectClass = (0, _classnames["default"])("".concat(prefix, "-select-dropdown"), _defineProperty({}, className, className));
      var inputClass = (0, _classnames["default"])("".concat(prefix, "-select-field"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefix, "-select-focus"), popupVisible), _defineProperty(_classNames2, "".concat(prefix, "-select-extra"), iconClass), _classNames2));
      var extraClass = (0, _classnames["default"])("".concat(prefix, "-extra-icon"), (_classNames3 = {}, _defineProperty(_classNames3, iconClass, iconClass), _defineProperty(_classNames3, "".concat(prefix, "-extra-disabled"), disabled), _classNames3));
      var actionClass = (0, _classnames["default"])('fm fm-angle-down', "".concat(prefix, "-action"), (_classNames4 = {}, _defineProperty(_classNames4, "".concat(prefix, "-action-open"), popupVisible), _defineProperty(_classNames4, "".concat(prefix, "-action-close"), !popupVisible), _defineProperty(_classNames4, "".concat(prefix, "-with-right"), category), _classNames4));
      var builtinPlacements = {
        left: {
          points: ['cr', 'cl']
        },
        right: {
          points: ['cl', 'cr']
        },
        top: {
          points: ['bc', 'tc']
        },
        bottom: {
          points: ['tc', 'bc']
        },
        topLeft: {
          points: ['bl', 'tl']
        },
        topRight: {
          points: ['br', 'tr']
        },
        bottomRight: {
          points: ['tr', 'br']
        },
        bottomLeft: {
          points: ['tl', 'bl']
        }
      };

      var innerTrigger = _react["default"].createElement("div", {
        className: selectClass,
        style: _objectSpread({
          width: this.dropdown_width,
          minWidth: '400px'
        }, dropdownStyle),
        ref: function ref(_ref) {
          if (_ref) {
            _this2.target = _ref;
            _ref.scrollTop = _this2.targetTop - 10;
          }
        }
      }, _react["default"].Children.map(children, function (o) {
        if (o && o.type) {
          if (typeof o.type === 'function') {
            return _react["default"].cloneElement(o, {
              select__value: value,
              onChange: _this2.onChange,
              getTargetTop: _this2.getTargetTop
            });
          } else {
            console.error("please use OptGroup or Option instead of ".concat(o.type, ", otherwise, option would not be able to work normally."));
          }
        }

        return o;
      }));

      return _react["default"].createElement(_rcTrigger["default"], {
        popupVisible: popupVisible,
        builtinPlacements: builtinPlacements,
        popup: innerTrigger,
        onPopupVisibleChange: this.onPopupVisibleChange,
        popupClassName: popupClassName,
        destroyPopupOnHide: destroyPopupOnHide,
        getPopupClassNameFromAlign: getPopupClassNameFromAlign,
        action: action,
        mouseEnterDelay: mouseEnterDelay,
        mouseLeaveDelay: mouseLeaveDelay,
        popupStyle: popupStyle,
        prefixCls: prefixCls,
        popupTransitionName: popupTransitionName,
        defaultPopupVisible: defaultPopupVisible,
        popupAlign: popupAlign,
        getPopupContainer: getPopupContainer,
        popupPlacement: popupPlacement
      }, _react["default"].createElement("div", {
        className: "".concat(prefix, "-select"),
        style: style,
        onClick: onClick,
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave,
        ref: function ref(_ref2) {
          return _this2.$innerRef = _ref2;
        }
      }, iconClass && _react["default"].createElement("i", {
        className: extraClass,
        onClick: this.onFocus
      }), _react["default"].createElement(_input["default"], {
        className: inputClass,
        placeholder: placeholder,
        disabled: disabled,
        value: value.name || '',
        onFocus: this.onFocus,
        onChange: this.onChange,
        onBlur: onBlur,
        readOnly: true,
        category: category
      }), actionVisible && _react["default"].createElement(_icon["default"], {
        type: "down",
        className: actionClass,
        onClick: this.onFocus
      })));
    }
  }]);

  return MkSelect;
}(_react.Component);

MkSelect.defaultProps = {
  action: ['click'],
  popupPlacement: 'bottomLeft',
  actionVisible: true,
  destroyPopupOnHide: true,
  value: {}
};
MkSelect.propTypes = {
  placeholder: _propTypes["default"].string,
  iconClass: _propTypes["default"].string,
  action: _propTypes["default"].array,
  popupPlacement: _propTypes["default"].string
};
var _default = MkSelect;
exports["default"] = _default;