'use strict';

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

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var hasOuterValue = require('../utils/hasOuterValue.js');

var getInitialValue = function getInitialValue(_ref) {
  var defaultValue = _ref.defaultValue,
    multiselect = _ref.multiselect;
  if (multiselect) {
    return defaultValue || [];
  }
  return defaultValue || '';
};
var useValue = function useValue(_ref2) {
  var outerValue = _ref2.outerValue,
    outerOnChange = _ref2.outerOnChange,
    defaultValue = _ref2.defaultValue,
    multiselect = _ref2.multiselect,
    nativeMode = _ref2.nativeMode;
  // Внутреннее состояние для корректной работы в uncontrolled-режиме.
  var _useState = React.useState(getInitialValue({
      defaultValue: defaultValue,
      multiselect: multiselect
    })),
    _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
    internalValue = _useState2[0],
    setInternalValue = _useState2[1];

  // Актуальное состояние компонента.
  var value = hasOuterValue.hasOuterValue(outerValue) ? outerValue : internalValue;
  var handleChange = function handleChange(newValue, item) {
    setInternalValue(newValue);
    if (outerOnChange && !nativeMode) {
      outerOnChange(newValue, item);
    }
  };
  return {
    value: value,
    handleChange: handleChange
  };
};

exports.useValue = useValue;
