"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _ramda = require("ramda");

var _ramdaExtension = require("ramda-extension");

var _useGeneratedId = _interopRequireDefault(require("../hooks/useGeneratedId"));

var _useDebounce3 = _interopRequireDefault(require("../hooks/useDebounce"));

var _Slider = _interopRequireDefault(require("../Slider"));

var _Text = _interopRequireDefault(require("../Text"));

var _Box = _interopRequireDefault(require("../Box"));

var _FormGroup = _interopRequireWildcard(require("../FormGroup"));

var DefaultRenderValues = function DefaultRenderValues(_ref) {
  var children = _ref.children;
  return _react.Children.map(children, function (child, i) {
    return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, i === 0 ? '' : ' - ', child);
  });
};

var SliderField = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
  var idProp = _ref2.id,
      Value = _ref2.renderValue,
      _ref2$renderValues = _ref2.renderValues,
      RenderValues = _ref2$renderValues === void 0 ? DefaultRenderValues : _ref2$renderValues,
      label = _ref2.label,
      hasError = _ref2.hasError,
      disabled = _ref2.disabled,
      readOnly = _ref2.readOnly,
      hint = _ref2.hint,
      value = _ref2.value,
      rest = (0, _objectWithoutProperties2["default"])(_ref2, ["id", "renderValue", "renderValues", "label", "hasError", "disabled", "readOnly", "hint", "value"]);
  var generatedStub = (0, _useGeneratedId["default"])();
  var generatedId = "field-".concat(generatedStub);
  var id = idProp || generatedId;
  var variant = (0, _FormGroup.getVariant)({
    disabled: disabled,
    readOnly: readOnly,
    hasError: hasError
  });

  var _useDebounce = (0, _useDebounce3["default"])(value, 200),
      _useDebounce2 = (0, _slicedToArray2["default"])(_useDebounce, 1),
      valueDebounced = _useDebounce2[0];

  var getValue = function getValue(x) {
    return Value ? /*#__PURE__*/_react["default"].createElement(Value, {
      key: x
    }, x) : x;
  };

  return /*#__PURE__*/_react["default"].createElement(_Box["default"], {
    ref: ref,
    sx: {
      position: 'relative'
    }
  }, /*#__PURE__*/_react["default"].createElement(_Text["default"], {
    sx: {
      textAlign: 'right',
      position: ['absolute', 'absolute', 'static'],
      right: 0,
      top: 0
    },
    mb: 0
  }, /*#__PURE__*/_react["default"].createElement(RenderValues, null, (0, _ramda.compose)((0, _ramda.map)(getValue), _ramdaExtension.ensureArray)(valueDebounced))), /*#__PURE__*/_react["default"].createElement(_FormGroup["default"], {
    id: id,
    label: label,
    hint: hint,
    disabled: disabled,
    readOnly: readOnly,
    hasError: hasError
  }, /*#__PURE__*/_react["default"].createElement(_Slider["default"], (0, _extends2["default"])({
    variant: variant,
    disabled: disabled,
    readOnly: readOnly,
    id: id,
    value: value
  }, rest))));
});
process.env.NODE_ENV !== "production" ? SliderField.propTypes = {
  /** Disabled state */
  disabled: _propTypes["default"].bool,

  /** Error state */
  hasError: _propTypes["default"].bool,

  /** Hint itext */
  hint: _propTypes["default"].node,

  /** Used to connect label and slider. If not defined, component will generate
   * its own. */
  id: _propTypes["default"].string,

  /** Label for Slider  */
  label: _propTypes["default"].node,

  /** Called when `onChange` event is triggered. */
  onChange: _propTypes["default"].func,

  /** Read-only state */
  readOnly: _propTypes["default"].bool,

  /** Formattting component for the value */
  renderValue: _propTypes["default"].elementType,

  /** Formattting for array of values */
  renderValues: _propTypes["default"].func,

  /** Current value of checkbox. */
  value: _propTypes["default"].any
} : void 0;
SliderField.displayName = 'SliderField';
var _default = SliderField;
exports["default"] = _default;