'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _typeof = require('@babel/runtime/helpers/typeof');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var React = require('react');
var formik = require('formik');
var neetoCist = require('@bigbinary/neeto-cist');
var ramda = require('ramda');
var Select = require('../Select.js');
var jsxRuntime = require('react/jsx-runtime');
require('classnames');
require('@bigbinary/neeto-icons/Down');
require('@bigbinary/neeto-icons/Close');
require('../react-select-creatable.esm-CYabSQvs.js');
require('@babel/runtime/helpers/esm/extends');
require('@babel/runtime/helpers/esm/objectSpread2');
require('@babel/runtime/helpers/esm/classCallCheck');
require('@babel/runtime/helpers/esm/createClass');
require('@babel/runtime/helpers/esm/inherits');
require('@babel/runtime/helpers/esm/createSuper');
require('@babel/runtime/helpers/esm/toConsumableArray');
require('@babel/runtime/helpers/extends');
require('@babel/runtime/helpers/esm/taggedTemplateLiteral');
require('@babel/runtime/helpers/esm/objectWithoutProperties');
require('@babel/runtime/helpers/esm/slicedToArray');
require('@babel/runtime/helpers/esm/typeof');
require('@babel/runtime/helpers/esm/defineProperty');
require('react-dom');
require('@babel/runtime/helpers/objectSpread2');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('@babel/runtime/helpers/inherits');
require('@babel/runtime/helpers/createSuper');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/taggedTemplateLiteral');
require('../useId-DXHUzdxd.js');
require('react-router-dom');
require('@bigbinary/neeto-hotkeys');
require('../overlayManager.js');
require('../index-CNZKW5X0.js');
require('qs');
require('../en-B1i-pQJm.js');
require('../index-BgzkP01c.js');
require('dayjs');
require('dayjs/plugin/localeData');
require('dayjs/plugin/utc');
require('dayjs/plugin/weekday');
require('dayjs/plugin/weekOfYear');
require('i18next');
require('../Label.js');
require('@bigbinary/neeto-icons/Help');
require('../Button.js');
require('../Spinner.js');
require('../Tooltip.js');
require('@tippyjs/react');
require('tippy.js');
require('../usePrefersReducedMotion-D2Ck84HM.js');
require('../Popover.js');
require('../Typography.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _typeof__default = /*#__PURE__*/_interopDefault(_typeof);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);

var _excluded = ["name", "options", "getOptionValue", "isMulti"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var SelectField = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _props$name = props.name,
    name = _props$name === void 0 ? "" : _props$name,
    _props$options = props.options,
    options = _props$options === void 0 ? [] : _props$options,
    _props$getOptionValue = props.getOptionValue,
    getOptionValue = _props$getOptionValue === void 0 ? null : _props$getOptionValue,
    _props$isMulti = props.isMulti,
    isMulti = _props$isMulti === void 0 ? false : _props$isMulti,
    otherProps = _objectWithoutProperties__default.default(props, _excluded);
  var _useField = formik.useField(name),
    _useField2 = _slicedToArray__default.default(_useField, 3),
    field = _useField2[0],
    meta = _useField2[1],
    _useField2$ = _useField2[2],
    setValue = _useField2$.setValue,
    setTouched = _useField2$.setTouched;
  var _useFormikContext = formik.useFormikContext(),
    _useFormikContext$sta = _useFormikContext.status,
    status = _useFormikContext$sta === void 0 ? {} : _useFormikContext$sta,
    setStatus = _useFormikContext.setStatus;
  var fieldStatus = formik.getIn(status, name);
  var isMenuOpen = React.useRef(otherProps.defaultMenuIsOpen);
  var getRealOptionValue = function getRealOptionValue(option) {
    if (typeof getOptionValue !== "function") {
      return option.value;
    }
    return getOptionValue(option);
  };
  var buildValueObj = function buildValueObj(value, options) {
    if (_typeof__default.default(value) === "object") return value;
    var isGrouped = neetoCist.existsBy({
      options: Array.isArray
    }, options);
    var searchOptions = options;
    if (isGrouped) {
      searchOptions = ramda.flatten(ramda.pluck("options", options));
    }
    return searchOptions.filter(function (option) {
      return getRealOptionValue(option) === value;
    })[0];
  };
  return /*#__PURE__*/jsxRuntime.jsx(Select, _objectSpread(_objectSpread({
    options: options,
    error: meta.touched ? meta.error || fieldStatus : "",
    getOptionValue: getOptionValue || ramda.prop("value"),
    innerRef: ref,
    isMulti: !!isMulti,
    name: field.name,
    value: ramda.either(ramda.isNil, ramda.isEmpty)(field.value) ? null : buildValueObj(field.value, options),
    onBlur: function onBlur() {
      return React.startTransition(function () {
        setTouched(true);
      });
    },
    onChange: function onChange(value) {
      setStatus(ramda.dissoc(name, status));
      setValue(value);
    }
  }, otherProps), {}, {
    onKeyDown: function onKeyDown(event) {
      var _otherProps$onKeyDown;
      if (event.key === "Enter" && isMenuOpen.current) {
        event.stopPropagation();
      }
      (_otherProps$onKeyDown = otherProps.onKeyDown) === null || _otherProps$onKeyDown === void 0 ? void 0 : _otherProps$onKeyDown.call(otherProps, event);
    },
    onMenuClose: function onMenuClose() {
      var _otherProps$onMenuClo;
      isMenuOpen.current = false;
      (_otherProps$onMenuClo = otherProps.onMenuClose) === null || _otherProps$onMenuClo === void 0 ? void 0 : _otherProps$onMenuClo.call(otherProps);
    },
    onMenuOpen: function onMenuOpen() {
      var _otherProps$onMenuOpe;
      isMenuOpen.current = true;
      (_otherProps$onMenuOpe = otherProps.onMenuOpen) === null || _otherProps$onMenuOpe === void 0 ? void 0 : _otherProps$onMenuOpe.call(otherProps);
    }
  }));
});
SelectField.displayName = "SelectField";

module.exports = SelectField;
//# sourceMappingURL=Select.js.map
