'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var SelectNative_styles = require('./SelectNative.styles.js');
var useForkRef = require('../../../../../hooks/useForkRef.js');
var createEvent = require('../../../../../utils/createEvent.js');

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

var React__default = /*#__PURE__*/_interopDefault(React);

var _option;
var SelectNative = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var name = _ref.name,
    multiple = _ref.multiple,
    items = _ref.items,
    value = _ref.value,
    onChange = _ref.onChange,
    onSetValue = _ref.onSetValue;
  var values = multiple ? value : [value];
  var selectRef = React.useRef(null);
  var forkRef = useForkRef.useForkRef(selectRef, ref);
  var options = Array.from(items.keys());
  React.useLayoutEffect(function () {
    if (selectRef.current && !multiple) {
      var valueInit = selectRef.current.value;
      if (valueInit) {
        onSetValue(valueInit);
      }
    }
    if (selectRef.current && multiple) {
      var valuesInit = Array.from(selectRef.current.selectedOptions).map(function (v) {
        return v.value;
      });
      if (valuesInit.length !== 0) {
        onSetValue(valuesInit);
      }
    }
  }, []);
  React.useLayoutEffect(function () {
    var event = createEvent.createEvent(selectRef);
    if (onChange) {
      onChange(event);
    }
  }, [value]);
  return /*#__PURE__*/React__default.default.createElement(React__default.default.Fragment, null, /*#__PURE__*/React__default.default.createElement(SelectNative_styles.SelectHidden, {
    ref: forkRef,
    multiple: multiple,
    name: name,
    hidden: true,
    value: multiple ? values : values[0]
  }, _option || (_option = /*#__PURE__*/React__default.default.createElement("option", null, " ")), options.map(function (v) {
    return /*#__PURE__*/React__default.default.createElement("option", {
      key: v,
      value: v
    }, v);
  })));
});

exports.SelectNative = SelectNative;
//# sourceMappingURL=SelectNative.js.map
