"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

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

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

var _title = _interopRequireWildcard(require("../title"));

var _context = _interopRequireDefault(require("../context"));

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

var Context = /*#__PURE__*/_react["default"].createContext(null);

var Item = /*#__PURE__*/function (_Component) {
  (0, _inheritsLoose2["default"])(Item, _Component);

  function Item(props) {
    return _Component.call(this, props) || this;
  }

  var _proto = Item.prototype;

  _proto.render = function render() {
    var _this$props = this.props,
        className = _this$props.className,
        children = _this$props.children,
        dataKey = _this$props.dataKey;
    var _this$context = this.context,
        activeKey = _this$context.activeKey,
        updateActiveKey = _this$context.updateActiveKey;
    var options = (0, _extends2["default"])({}, this.props);
    delete options.children;
    return /*#__PURE__*/_react["default"].createElement(_title["default"], (0, _extends2["default"])({}, options, {
      className: (0, _classnames["default"])(className, {
        selected: activeKey === dataKey
      }),
      onClick: function onClick() {
        updateActiveKey(dataKey);
      }
    }), children);
  };

  return Item;
}(_react.Component);

Item.contextType = Context;

var SingleSelect = /*#__PURE__*/function (_Component2) {
  (0, _inheritsLoose2["default"])(SingleSelect, _Component2);

  function SingleSelect(props) {
    var _this;

    _this = _Component2.call(this, props) || this;
    _this.state = {
      activeKey: _this.props.defaultActiveKey || _this.props.activeKey
    };
    return _this;
  }

  var _proto2 = SingleSelect.prototype;

  _proto2.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
    var _this$props2 = this.props,
        activeKey = _this$props2.activeKey,
        onChange = _this$props2.onChange;

    if (prevProps.activeKey !== activeKey) {
      this.setState({
        activeKey: activeKey
      }, function () {
        onChange(activeKey);
      });
    }
  };

  _proto2.render = function render() {
    var _this2 = this;

    var _cssPrefix = this.context._cssPrefix;
    var _this$props3 = this.props,
        className = _this$props3.className,
        children = _this$props3.children,
        align = _this$props3.align,
        direction = _this$props3.direction,
        onChange = _this$props3.onChange;
    var activeKey = this.state.activeKey;
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])(_cssPrefix + "single-select-wrapper", _cssPrefix + "single-select-" + direction + "-" + align, className) // @ts-ignore
      ,
      style: _title.FLEX_STYLE[direction][align]
    }, /*#__PURE__*/_react["default"].createElement(Context.Provider, {
      value: {
        activeKey: activeKey,
        updateActiveKey: function updateActiveKey(key) {
          _this2.setState({
            activeKey: key
          }, function () {
            onChange(key);
          });
        }
      }
    }, _react.Children.map(children, function (child, i) {
      return (// @ts-ignore

        /*#__PURE__*/
        (0, _react.isValidElement)(child) ? /*#__PURE__*/(0, _react.cloneElement)(child, {
          dataKey: child.key || i
        }) : child
      );
    })));
  };

  return SingleSelect;
}(_react.Component);

SingleSelect.Item = void 0;
SingleSelect.Item = Item; // @ts-ignore

SingleSelect.defaultProps = {
  className: null,
  align: 'middle',
  direction: 'row',
  onChange: function onChange() {}
};
SingleSelect.contextType = _context["default"];
var _default = SingleSelect;
exports["default"] = _default;