import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { Fragment, forwardRef } from 'react';
import PropTypes from 'prop-types';
import invariant from 'invariant';
import { Slider as CompoundSlider, Handles, Rail, Tracks } from 'react-compound-slider';
import { isNotNil } from 'ramda-extension';
import Box from '../Box';

var Handle = function Handle(_ref) {
  var _ref$domain = _slicedToArray(_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.createElement(Fragment, null, /*#__PURE__*/React.createElement(Box, _extends({
    sx: {
      variant: "".concat(variant, ".handleTouch"),
      left: "".concat(percent, "%"),
      position: 'absolute',
      transform: 'translate(-50%, -50%)'
    }
  }, getHandleProps(id))), /*#__PURE__*/React.createElement(Box, {
    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.arrayOf(PropTypes.number).isRequired,
  getHandleProps: PropTypes.func.isRequired,
  handle: PropTypes.shape({
    id: PropTypes.any,
    value: PropTypes.any,
    percent: PropTypes.number
  }).isRequired,
  variant: PropTypes.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.createElement(Box, _extends({
    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.func.isRequired,
  source: PropTypes.shape({
    percent: PropTypes.number.isRequired
  }),
  target: PropTypes.shape({
    percent: PropTypes.number.isRequired
  }),
  variant: PropTypes.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.createElement(Box, _extends({
    sx: {
      variant: "".concat(variant, ".rail"),
      position: 'absolute',
      width: '100%',
      transform: 'translate(0%, -50%)'
    }
  }, getRailProps()));
};

process.env.NODE_ENV !== "production" ? SliderRail.propTypes = {
  getRailProps: PropTypes.func.isRequired,
  variant: PropTypes.oneOf(['forms.slider', 'forms.slider.danger', 'forms.slider.disabled'])
} : void 0;
var Slider = /*#__PURE__*/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 = _objectWithoutProperties(_ref4, ["disabled", "step", "min", "variant", "max", "onChange", "onUpdate", "value"]);

  invariant(isNotNil(min), '`min` is required for Slider component');
  invariant(isNotNil(max), '`max` is required for Slider component');
  var variant = !variantProp ? 'forms.slider' : "forms.slider.".concat(variantProp);
  var domain = [min, max];
  return /*#__PURE__*/React.createElement(CompoundSlider, _extends({
    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.createElement(Rail, null, function (_ref5) {
    var getRailProps = _ref5.getRailProps;
    return /*#__PURE__*/React.createElement(SliderRail, {
      variant: variant,
      getRailProps: getRailProps
    });
  }), /*#__PURE__*/React.createElement(Handles, null, function (_ref6) {
    var handles = _ref6.handles,
        getHandleProps = _ref6.getHandleProps;
    return /*#__PURE__*/React.createElement(Fragment, null, handles.map(function (handle) {
      return /*#__PURE__*/React.createElement(Handle, {
        variant: variant,
        key: handle.id,
        handle: handle,
        domain: domain,
        disabled: disabled,
        getHandleProps: getHandleProps
      });
    }));
  }), /*#__PURE__*/React.createElement(Tracks, {
    right: false,
    left: false
  }, function (_ref7) {
    var tracks = _ref7.tracks,
        getTrackProps = _ref7.getTrackProps;
    return /*#__PURE__*/React.createElement(Fragment, null, tracks.map(function (_ref8) {
      var id = _ref8.id,
          source = _ref8.source,
          target = _ref8.target;
      return /*#__PURE__*/React.createElement(Track, {
        variant: variant,
        key: id,
        source: source,
        target: target,
        getTrackProps: getTrackProps
      });
    }));
  }));
});
Slider.displayName = 'Slider';
process.env.NODE_ENV !== "production" ? Slider.propTypes = {
  disabled: PropTypes.bool,
  max: PropTypes.number.isRequired,
  min: PropTypes.number.isRequired,
  onChange: PropTypes.func,
  onUpdate: PropTypes.func,
  step: PropTypes.any,
  value: PropTypes.any,
  variant: PropTypes.oneOf(['danger', 'disabled'])
} : void 0;
export default Slider;