"use strict";

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

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

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

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

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

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

var _reactSelect = _interopRequireDefault(require("react-select"));

var _async = _interopRequireDefault(require("react-select/async"));

var _asyncCreatable = _interopRequireDefault(require("react-select/async-creatable"));

var _creatable = _interopRequireDefault(require("react-select/creatable"));

var _Form = require("../Form");

var _styles = _interopRequireDefault(require("./styles"));

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 __jsx = _react["default"].createElement;

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) { (0, _defineProperty2["default"])(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; }

var renderSelect = function renderSelect(props, ref) {
  var loadOptions = props.loadOptions,
      onCreateOption = props.onCreateOption,
      id = props.id,
      rest = (0, _objectWithoutProperties2["default"])(props, ["loadOptions", "onCreateOption", "id"]);

  if (loadOptions && onCreateOption) {
    return __jsx(_asyncCreatable["default"], (0, _extends2["default"])({
      inputId: props.id,
      loadOptions: loadOptions,
      onCreateOption: onCreateOption,
      ref: ref
    }, rest));
  }

  if (loadOptions) {
    return __jsx(_async["default"], (0, _extends2["default"])({
      inputId: props.id,
      loadOptions: loadOptions,
      ref: ref
    }, rest));
  }

  if (onCreateOption) {
    return __jsx(_creatable["default"], (0, _extends2["default"])({
      inputId: props.id,
      onCreateOption: onCreateOption,
      ref: ref
    }, rest));
  }

  return __jsx(_reactSelect["default"], (0, _extends2["default"])({
    inputId: props.id,
    ref: ref
  }, rest, {
    visible: true,
    open: true
  }));
};

var Select = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useFormField = (0, _Form.useFormField)(props),
      formOnChange = _useFormField.onChange,
      initialSelectValue = _useFormField.value;

  var _props$size = props.size,
      size = _props$size === void 0 ? 'md' : _props$size,
      _props$options = props.options,
      options = _props$options === void 0 ? [] : _props$options,
      _props$border = props.border,
      border = _props$border === void 0 ? 'full' : _props$border; // optionally allow custom onChange event along with passed Form onChange

  var hasOnChange = props.onChange || formOnChange && typeof formOnChange === 'function';

  var onChange = function onChange(e) {
    if (formOnChange && typeof formOnChange === 'function') {
      formOnChange(e, e.constructor === Array ? e.map(function (v) {
        return v.value;
      }) : e.value);
    }

    if (props.onChange) {
      props.onChange(e);
    }
  };

  var isInitialMulti = props.isMulti && Array.isArray(initialSelectValue);

  var _useSelectStyle = (0, _styles["default"])({
    size: size,
    border: border
  }),
      selectStyleProps = _useSelectStyle.root,
      selectTheme = _useSelectStyle.theme; // Handle default value


  var defaultSelectValue = null;

  if (props.isMulti) {
    defaultSelectValue = [];
  }

  options.forEach(function (o) {
    if (o.value && o.value === initialSelectValue || isInitialMulti && initialSelectValue.indexOf(o.value) > -1) {
      if (isInitialMulti) {
        defaultSelectValue.push(o);
      } else {
        defaultSelectValue = o;
      }
    } else if (o.options) {
      var v = o.options.filter(function (opt) {
        // when isMulti, handle initial form values of array types
        if (isInitialMulti && initialSelectValue.indexOf(opt.value) > -1) {
          return true;
        }

        if (opt.value === initialSelectValue) {
          return true;
        }

        return false;
      });

      if (v.length > 0) {
        if (isInitialMulti) {
          defaultSelectValue = defaultSelectValue.concat(v);
        } else {
          defaultSelectValue = v;
        }
      }
    }
  });

  var selectProps = _objectSpread(_objectSpread({}, props), {}, {
    onChange: hasOnChange && onChange,
    styles: selectStyleProps,
    defaultValue: defaultSelectValue,
    theme: function theme(_theme) {
      return _objectSpread(_objectSpread(_objectSpread({}, _theme), selectTheme.root), {}, {
        colors: _objectSpread(_objectSpread({}, _theme.colors), selectTheme.colors),
        spacing: _objectSpread(_objectSpread({}, _theme.spacing), selectTheme.spacing)
      });
    }
  });

  return __jsx(_react["default"].Fragment, null, renderSelect(selectProps, ref));
});
exports.Select = Select;
//# sourceMappingURL=Select.js.map