function _typeof(obj) { "@babel/helpers - typeof"; 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); }

import _regeneratorRuntime from "@babel/runtime/regenerator";

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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

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

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

import * as React from 'react';
import _ from 'lodash';
import { Select } from 'mkui-fd';
import { getSelectValue, getSelectObject } from './util';
import { propTypes, defaultProps } from './PropTypes';
var Option = Select.Option;

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

  var _super = _createSuper(EnhancedSelect);

  /** *******************life cycle************************** */
  function EnhancedSelect(props) {
    var _this;

    _classCallCheck(this, EnhancedSelect);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "selectHandle", function (value) {
      var _this$props = _this.props,
          onChange = _this$props.onChange,
          multiple = _this$props.multiple;
      var dataSource = _this.state.dataSource;
      var newValue = getSelectObject(value, dataSource, multiple);
      onChange(newValue);
    });

    _defineProperty(_assertThisInitialized(_this), "fetch", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var _this$state, api, param, dataSource, data;

      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _this$state = _this.state, api = _this$state.api, param = _this$state.param, dataSource = _this$state.dataSource;

              if (!api) {
                _context.next = 7;
                break;
              }

              _context.next = 4;
              return api(_objectSpread({}, param));

            case 4:
              _context.t0 = _context.sent;
              _context.next = 8;
              break;

            case 7:
              _context.t0 = dataSource;

            case 8:
              data = _context.t0;

              _this.setState({
                dataSource: data,
                loading: false,
                isApiLoaded: true
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));

    _defineProperty(_assertThisInitialized(_this), "getRenderContext", function (dataSource) {
      return dataSource.map(function (d) {
        return /*#__PURE__*/React.createElement(Option, {
          key: d.code
        }, d.name);
      });
    });

    var _this$props2 = _this.props,
        _dataSource = _this$props2.dataSource,
        _api = _this$props2.api;
    _this.state = {
      loading: true,
      dataSource: _api ? [] : _dataSource,
      isApiLoaded: false
    };
    return _this;
  }

  _createClass(EnhancedSelect, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      // const { disabled, value } = this.state;
      // if (!disabled) {
      //   this.fetch();
      // } else {
      //   this.setDataSource(value);
      // }
      this.fetch();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var prevParam = prevProps.param,
          prevDataSource = prevProps.dataSource,
          prevDisabled = prevProps.disabled;
      var _this$props3 = this.props,
          param = _this$props3.param,
          dataSource = _this$props3.dataSource,
          disabled = _this$props3.disabled;
      var isApiLoaded = this.state.isApiLoaded;

      if (!_.isEqual(prevParam, param)) {
        this.fetch();
      }

      if (!_.isEqual(prevDataSource, dataSource)) {
        // eslint-disable-next-line react/no-did-update-set-state
        this.setState({
          dataSource: dataSource
        });
      }

      if (disabled !== prevDisabled && !disabled && !isApiLoaded) {
        this.fetch();
      }
    }
    /** *******************handle method************************** */

  }, {
    key: "render",
    value: function render() {
      var _this$state2 = this.state,
          dataSource = _this$state2.dataSource,
          loading = _this$state2.loading,
          value = _this$state2.value,
          disabled = _this$state2.disabled;
      var multiple = this.props.multiple;
      return /*#__PURE__*/React.createElement(Select // eslint-disable-next-line react/jsx-props-no-spreading
      , _extends({}, this.props, {
        loading: loading,
        disabled: disabled,
        onChange: this.selectHandle,
        value: getSelectValue(multiple, value, dataSource),
        mode: multiple && 'multiple'
      }), this.getRenderContext(dataSource));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props) {
      var multiple = props.multiple,
          param = props.param,
          value = props.value,
          api = props.api,
          disabled = props.disabled;
      return {
        param: param,
        value: multiple ? value : [value],
        api: api,
        disabled: disabled
      };
    }
  }]);

  return EnhancedSelect;
}(React.Component);

export { EnhancedSelect as default };
EnhancedSelect.propTypes = propTypes;
EnhancedSelect.defaultProps = defaultProps;
//# sourceMappingURL=EnhancedSelect.js.map
