"use strict";

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

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

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

var _pop = _interopRequireDefault(require("../../components-v1/pop"));

var _Option = _interopRequireDefault(require("./Option"));

require("./index.css");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : 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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

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

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

  var _super = _createSuper(index);

  function index(p) {
    var _this;

    _classCallCheck(this, index);

    _this = _super.call(this, p);

    _initialiseProps.call(_assertThisInitialized(_this));

    var value = _this.props.value;

    if (value != undefined) {
      _this.controled = true;
      _this.state = {
        focus: false
      };
      return _possibleConstructorReturn(_this);
    }

    _this.state = {
      value: _this.props.defaultValue === undefined ? "" : _this.props.defaultValue,
      focus: false
    };
    return _this;
  }

  _createClass(index, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(newProps) {
      this.setWrapperScrollTop(newProps);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          className = _this$props.className,
          disabled = _this$props.disabled,
          type = _this$props.type,
          containerStyle = _this$props.containerStyle,
          container = _this$props.container,
          title = _this$props.title;
      var style = this.props.maxHeight ? {
        overflow: "auto",
        maxHeight: this.props.maxHeight
      } : {};
      return /*#__PURE__*/_react["default"].createElement("div", _extends({
        className: type == "old" ? "tita-common-select-wrapper-o" : "tita-common-select-wrapper",
        style: containerStyle
      }, this.getHideEventHandler()), /*#__PURE__*/_react["default"].createElement("div", _extends({
        ref: "selector",
        style: _objectSpread(_objectSpread({}, this.props.style || {}), {
          backgroundColor: disabled ? "#f5f5f5" : this.props.dis ? "#4C5D68" : "#ffffff",
          cursor: disabled ? "not-allowed" : "pointer"
        }),
        className: "tita-common-select ".concat(this.state.focus ? "" : "tita-common-select-active")
      }, this.getShowEventHandler()), /*#__PURE__*/_react["default"].createElement("div", {
        className: "tita-select-text"
      }, title || this.getText()), /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(type == "old" ? "tu-icon-sort-up" : "tu-icon-arrow-up", " common-select-arrow")
      })), /*#__PURE__*/_react["default"].createElement(_pop["default"], {
        ref: "options",
        blurToHide: this.props.toggle !== "hover",
        transitionName: "none-transition",
        onBeforeHide: this.handleBlur,
        container: container
      }, /*#__PURE__*/_react["default"].createElement("ul", {
        className: className,
        ref: "titaCommonCelectorWrapper",
        style: _objectSpread(_objectSpread({}, {
          minWidth: this.props.style.width
        }), style),
        onClick: function onClick(e) {
          e.stopPropagation();
          e.nativeEvent.stopImmediatePropagation();
        },
        onMouseDown: function onMouseDown(e) {
          e.stopPropagation();
          e.nativeEvent.stopImmediatePropagation();
        }
      }, this.getChildren())));
    }
  }]);

  return index;
}(_react.Component);

exports["default"] = index;
index.defaultProps = {
  style: {
    width: "100px !important",
    // NOTE IE11 下 width auto 不展示
    height: "26px"
  },
  toggle: "click",
  data: [],
  disabled: false,
  placeHolder: "",
  wrapperHeight: 300,
  adjustHeight: 54
};

var _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this.setWrapperScrollTop = function (newProps) {
    var value = newProps.value || -1;
    var activeTimeIndex = newProps.data.findIndex(function (item) {
      return (item.value || item) == value;
    });
    var scrollTop = activeTimeIndex * 32 >= _this2.props.wrapperHeight ? activeTimeIndex * 32 - _this2.props.wrapperHeight + _this2.props.adjustHeight : 0; // @ts-ignore

    if (_this2.refs.titaCommonCelectorWrapper) {
      _this2.refs.titaCommonCelectorWrapper.scrollTop = scrollTop;
    }
  };

  this.getShowEventHandler = function () {
    var handler = {};

    switch (_this2.props.toggle) {
      case "click":
        {
          handler.onClick = _this2.showOptions;
          break;
        }

      case "hover":
        {
          handler.onMouseOver = _this2.showOptions;
          break;
        }

      case "mousedonwn":
        {
          handler.onMouseDown = _this2.showOptions;
        }

      case "mouseup":
        {
          handler.onMouseUp = _this2.showOptions;
        }

      default:
        {
          handler.onClick = _this2.showOptions;
        }
    }

    return handler;
  };

  this.getHideEventHandler = function () {
    var handler = {};

    switch (_this2.props.toggle) {
      case "mouseup":
      case "hover":
        {
          handler.onMouseLeave = _this2.hideOptions;
          break;
        }
    }

    return handler;
  };

  this.getChildren = function () {
    var _this2$props$style = _this2.props.style,
        height = _this2$props$style.height,
        width = _this2$props$style.width;
    var _this2$props = _this2.props,
        valueField = _this2$props.valueField,
        textField = _this2$props.textField,
        value = _this2$props.value;
    return _this2.props.data.map(function (item, index) {
      var text = textField ? item[textField] : item;
      var value = valueField ? item[valueField] : item;
      var trigger = {
        click: "onClick",
        mousedonwn: "onMouseDown",
        mouseup: "onMouseUp"
      };
      return /*#__PURE__*/_react["default"].createElement(_Option["default"], {
        className: _this2.props.value == value ? "active" : "",
        style: {
          width: width,
          height: height
        },
        key: value,
        value: value,
        select: item,
        onSelect: _this2.handleSelect,
        triggerMethod: trigger[_this2.props.toggle]
      }, text);
    });
  };

  this.handleSelect = function (value) {
    if (!_this2.controled) {
      _this2.setState({
        value: value
      });
    }

    _this2.hideOptions();

    var onSelect = _this2.props.onSelect;
    onSelect && onSelect(value);
  };

  this.getSelected = function () {
    var valueField = _this2.props.valueField;
    if (!valueField) return _this2.getValue();
    return _this2.props.data.filter(function (item) {
      return item[valueField] == _this2.getValue();
    })[0];
  };

  this.getValue = function () {
    return _this2.controled ? _this2.props.value : _this2.state.value;
  };

  this.getText = function () {
    var _this2$props2 = _this2.props,
        textField = _this2$props2.textField,
        placeHolder = _this2$props2.placeHolder;

    var selected = _this2.getSelected();

    if (!selected) return placeHolder;
    return textField ? selected[textField] : selected;
  };

  this.showOptions = function () {
    if (_this2.props.disabled) return;

    _this2.refs.options.show(_this2.refs.selector);

    _this2.setState({
      focus: true
    });
  };

  this.hideOptions = function () {
    _this2.refs.options.hide();
  };

  this.handleBlur = function () {
    _this2.setState({
      focus: false
    });
  };

  this.getToggle = function (type) {
    var eventName = "onClick";
    var toggle = {};

    if (_this2.props.toggle == "hover") {
      eventName = "onMouseOver";
      toggle.onMouseLeave = _this2.hideOptions;
    }

    if (_this2.props.toggle == "mousedonwn") {
      eventName = "onMouseDown", toggle.onMouseDown = _this2.hideOptions;
    }

    if (_this2.props.toggle == "mouseup") {
      eventName = "onMouseUp", toggle.onMouseUp = _this2.hideOptions;
    }

    toggle[eventName] = _this2.showOptions;
    return toggle;
  };
};