"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 _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

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

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

var _invariant = _interopRequireDefault(require("invariant"));

var _reactCompoundSlider = require("react-compound-slider");

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

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

var Handle = function Handle(_ref) {
  var _ref$domain = (0, _slicedToArray2["default"])(_ref.domain, 2),
      min = _ref$domain[0],
      max = _ref$domain[1],
      _ref$handle = _ref.handle,
      id = _ref$handle.id,
      value = _ref$handle.value,
      percent = _ref$handle.percent,
      variant = _ref.variant,
      getHandleProps = _ref.getHandleProps;

  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, /*#__PURE__*/_react["default"].createElement(_Box["default"], (0, _extends2["default"])({
    sx: {
      variant: "".concat(variant, ".handleTouch"),
      left: "".concat(percent, "%"),
      position: 'absolute',
      transform: 'translate(-50%, -50%)'
    }
  }, getHandleProps(id))), /*#__PURE__*/_react["default"].createElement(_Box["default"], {
    role: "slider",
    "aria-valuemin": min,
    "aria-valuemax": max,
    "aria-valuenow": value,
    sx: {
      variant: "".concat(variant, ".handle"),
      left: "".concat(percent, "%"),
      position: 'absolute',
      transform: 'translate(-50%, -50%)',
      zIndex: 2
    }
  }));
};

process.env.NODE_ENV !== "production" ? Handle.propTypes = {
  domain: _propTypes["default"].arrayOf(_propTypes["default"].number).isRequired,
  getHandleProps: _propTypes["default"].func.isRequired,
  handle: _propTypes["default"].shape({
    id: _propTypes["default"].any,
    value: _propTypes["default"].any,
    percent: _propTypes["default"].number
  }).isRequired,
  variant: _propTypes["default"].oneOf(['forms.slider', 'forms.slider.danger', 'forms.slider.disabled'])
} : void 0;

var Track = function Track(_ref2) {
  var source = _ref2.source,
      target = _ref2.target,
      getTrackProps = _ref2.getTrackProps,
      variant = _ref2.variant;
  return /*#__PURE__*/_react["default"].createElement(_Box["default"], (0, _extends2["default"])({
    sx: {
      variant: "".concat(variant, ".track"),
      position: 'absolute',
      transform: 'translate(0%, -50%)',
      left: "".concat(source.percent, "%"),
      width: "".concat(target.percent - source.percent, "%"),
      zIndex: 1
    }
  }, getTrackProps()));
};

process.env.NODE_ENV !== "production" ? Track.propTypes = {
  getTrackProps: _propTypes["default"].func.isRequired,
  source: _propTypes["default"].shape({
    percent: _propTypes["default"].number.isRequired
  }),
  target: _propTypes["default"].shape({
    percent: _propTypes["default"].number.isRequired
  }),
  variant: _propTypes["default"].oneOf(['forms.slider', 'forms.slider.danger', 'forms.slider.disabled'])
} : void 0;

var SliderRail = function SliderRail(_ref3) {
  var getRailProps = _ref3.getRailProps,
      variant = _ref3.variant;
  return /*#__PURE__*/_react["default"].createElement(_Box["default"], (0, _extends2["default"])({
    sx: {
      variant: "".concat(variant, ".rail"),
      position: 'absolute',
      width: '100%',
      transform: 'translate(0%, -50%)'
    }
  }, getRailProps()));
};

process.env.NODE_ENV !== "production" ? SliderRail.propTypes = {
  getRailProps: _propTypes["default"].func.isRequired,
  variant: _propTypes["default"].oneOf(['forms.slider', 'forms.slider.danger', 'forms.slider.disabled'])
} : void 0;
var Slider = /*#__PURE__*/(0, _react.forwardRef)(function (_ref4, ref) {
  var disabled = _ref4.disabled,
      _ref4$step = _ref4.step,
      step = _ref4$step === void 0 ? 1 : _ref4$step,
      min = _ref4.min,
      variantProp = _ref4.variant,
      max = _ref4.max,
      onChange = _ref4.onChange,
      onUpdate = _ref4.onUpdate,
      value = _ref4.value,
      rest = (0, _objectWithoutProperties2["default"])(_ref4, ["disabled", "step", "min", "variant", "max", "onChange", "onUpdate", "value"]);
  (0, _invariant["default"])((0, _ramdaExtension.isNotNil)(min), '`min` is required for Slider component');
  (0, _invariant["default"])((0, _ramdaExtension.isNotNil)(max), '`max` is required for Slider component');
  var variant = !variantProp ? 'forms.slider' : "forms.slider.".concat(variantProp);
  var domain = [min, max];
  return /*#__PURE__*/_react["default"].createElement(_reactCompoundSlider.Slider, (0, _extends2["default"])({
    mode: 1,
    step: step,
    domain: domain,
    rootStyle: {
      position: 'relative',
      width: '100%',
      touchAction: 'none'
    },
    onUpdate: onUpdate,
    onChange: onChange,
    values: value,
    disabled: disabled,
    ref: ref
  }, rest), /*#__PURE__*/_react["default"].createElement(_reactCompoundSlider.Rail, null, function (_ref5) {
    var getRailProps = _ref5.getRailProps;
    return /*#__PURE__*/_react["default"].createElement(SliderRail, {
      variant: variant,
      getRailProps: getRailProps
    });
  }), /*#__PURE__*/_react["default"].createElement(_reactCompoundSlider.Handles, null, function (_ref6) {
    var handles = _ref6.handles,
        getHandleProps = _ref6.getHandleProps;
    return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, handles.map(function (handle) {
      return /*#__PURE__*/_react["default"].createElement(Handle, {
        variant: variant,
        key: handle.id,
        handle: handle,
        domain: domain,
        disabled: disabled,
        getHandleProps: getHandleProps
      });
    }));
  }), /*#__PURE__*/_react["default"].createElement(_reactCompoundSlider.Tracks, {
    right: false,
    left: false
  }, function (_ref7) {
    var tracks = _ref7.tracks,
        getTrackProps = _ref7.getTrackProps;
    return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, tracks.map(function (_ref8) {
      var id = _ref8.id,
          source = _ref8.source,
          target = _ref8.target;
      return /*#__PURE__*/_react["default"].createElement(Track, {
        variant: variant,
        key: id,
        source: source,
        target: target,
        getTrackProps: getTrackProps
      });
    }));
  }));
});
Slider.displayName = 'Slider';
process.env.NODE_ENV !== "production" ? Slider.propTypes = {
  disabled: _propTypes["default"].bool,
  max: _propTypes["default"].number.isRequired,
  min: _propTypes["default"].number.isRequired,
  onChange: _propTypes["default"].func,
  onUpdate: _propTypes["default"].func,
  step: _propTypes["default"].any,
  value: _propTypes["default"].any,
  variant: _propTypes["default"].oneOf(['danger', 'disabled'])
} : void 0;
var _default = Slider;
exports["default"] = _default;