function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React from 'react';
import { PanResponder } from 'react-native';
import { FormControlContext } from '../../composites/FormControl';
import { Box } from '../../primitives';
import { usePropsConfig } from '../../../theme';
export const SliderContext = /*#__PURE__*/React.createContext({});

class NBSlider extends React.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      barSize: null,
      deltaValue: 0,
      value: this.props.defaultValue || 0
    });

    _defineProperty(this, "panResponder", PanResponder.create({
      onMoveShouldSetPanResponderCapture: () => true,
      onPanResponderMove: (_event, gestureState) => !(this.props.isDisabled || this.props.isReadOnly) && this.onMove(gestureState),
      onPanResponderRelease: () => this.onEndMove(),
      onPanResponderTerminate: () => {}
    }));

    _defineProperty(this, "onBarLayout", event => {
      const {
        width: barSize
      } = event.nativeEvent.layout;
      this.setState({
        barSize
      });
    });

    _defineProperty(this, "capValueWithinRange", (value, range) => {
      if (value < range[0]) return range[0];
      if (value > range[1]) return range[1];
      return value;
    });

    _defineProperty(this, "getValueFromBottomOffset", (offset, barSize, rangeMin, rangeMax) => {
      if (barSize === null) return 0;
      return (rangeMax - rangeMin) * offset / barSize;
    });

    _defineProperty(this, "getOffsetFromValue", (value, rangeMin, rangeMax, barSize) => {
      if (barSize === null) return 0;
      const valueOffset = value - rangeMin;
      const totalRange = rangeMax - rangeMin;
      const percentage = valueOffset / totalRange;
      return barSize * percentage;
    });
  }

  onMove(gestureState) {
    const {
      barSize
    } = this.state;
    const newDeltaValue = this.getValueFromBottomOffset(gestureState.dx, barSize, this.props.min || 0, this.props.max || 100);
    this.props.onChange && this.props.onChange(this.state.value + newDeltaValue);
    this.setState({
      deltaValue: newDeltaValue
    });
  }

  onEndMove() {
    const {
      value,
      deltaValue
    } = this.state;
    this.props.onChangeEnd && this.props.onChangeEnd(value + deltaValue);
    this.setState({
      value: value + deltaValue,
      deltaValue: 0
    });
  }

  render() {
    const {
      value,
      deltaValue,
      barSize
    } = this.state;
    const cappedValue = this.capValueWithinRange(value + deltaValue, [this.props.min || 0, this.props.max || 100]);
    const sliderOffset = this.getOffsetFromValue(cappedValue, this.props.min || 0, this.props.max || 100, barSize);
    return /*#__PURE__*/React.createElement(SliderContext.Provider, {
      value: {
        sliderOffset,
        colorScheme: this.props.activeColor,
        barSize: this.state.barSize,
        panResponder: this.panResponder,
        isReversed: this.props.isReversed,
        thumbSize: this.props.thumbSize,
        sliderSize: this.props.sliderSize
      }
    }, /*#__PURE__*/React.createElement(Box, _extends({
      position: "relative",
      display: "flex",
      my: 3,
      justifyContent: "center",
      alignItems: "center",
      minHeight: 3,
      minWidth: "100%"
    }, this.props, {
      onLayout: this.onBarLayout
    }), this.state.barSize && this.state.value && this.props.children));
  }

}

_defineProperty(NBSlider, "contextType", SliderContext);

const Slider = ({ ...props
}) => {
  const formControlContext = React.useContext(FormControlContext);
  const newProps = usePropsConfig('Slider', { ...formControlContext,
    ...props
  });
  return /*#__PURE__*/React.createElement(NBSlider, newProps);
};

export default Slider;
//# sourceMappingURL=Slider.js.map