"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _core = require("@blueprintjs/core");
var _editorUtils = require("../utils/editorUtils");
var _lodash = require("lodash");
require("./UncontrolledSliderWithPlusMinusBtns.css");
var _excluded = ["noWraparound", "title", "initialValue", "label", "passedRef", "clickStepSize", "style", "onClick", "smallSlider", "bindOutsideChangeHelper", "className"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var UncontrolledSliderWithPlusMinusBtns = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(UncontrolledSliderWithPlusMinusBtns, _React$Component);
  function UncontrolledSliderWithPlusMinusBtns() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _defineProperty(_assertThisInitialized(_this), "state", {
      value: 0
    });
    return _this;
  }
  UncontrolledSliderWithPlusMinusBtns.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.justUpdateInitialValOnce && prevState.oldInitialValue !== undefined) {
      return null;
    }
    //potentially coerce the initial value coming in
    if (prevState.oldInitialValue !== nextProps.initialValue) {
      var val = nextProps.coerceInitialValue ? nextProps.coerceInitialValue(nextProps) : nextProps.initialValue;
      return {
        value: val,
        //set the state value if a new initial value comes in!
        oldInitialValue: nextProps.initialValue
      };
    } else {
      return null;
    }
  };
  var _proto = UncontrolledSliderWithPlusMinusBtns.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var value = this.state.value;
    var _this$props = this.props,
      noWraparound = _this$props.noWraparound,
      title = _this$props.title,
      initialValue = _this$props.initialValue,
      label = _this$props.label,
      passedRef = _this$props.passedRef,
      clickStepSize = _this$props.clickStepSize,
      style = _this$props.style,
      _onClick = _this$props.onClick,
      smallSlider = _this$props.smallSlider,
      bindOutsideChangeHelper = _this$props.bindOutsideChangeHelper,
      className = _this$props.className,
      rest = _objectWithoutPropertiesLoose(_this$props, _excluded);
    var _this$props2 = this.props,
      min = _this$props2.min,
      max = _this$props2.max;
    var stepSize = this.props.stepSize || (max - min) / 10;
    if (bindOutsideChangeHelper) {
      bindOutsideChangeHelper.triggerChange = function (fn) {
        var valToPass = (0, _lodash.isNumber)(value) && !isNaN(value) ? value : initialValue;
        return fn({
          value: valToPass,
          changeValue: function changeValue(newVal) {
            var newnew = (0, _lodash.clamp)(newVal, min, max);
            _this2.setState({
              value: newnew
            });
            _this2.props.onChange && _this2.props.onChange(newnew);
            _this2.props.onRelease && _this2.props.onRelease(newnew);
          }
        });
      };
    }
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: className + " " + (smallSlider ? "small-slider" : ""),
      onClick: function onClick(e) {
        _onClick && _onClick(e);
        (0, _editorUtils.preventDefaultStopPropagation)(e);
      },
      ref: passedRef,
      onDrag: _editorUtils.preventDefaultStopPropagation,
      onDragStart: _editorUtils.preventDefaultStopPropagation,
      onDragEnd: _editorUtils.preventDefaultStopPropagation,
      onMouseDown: _editorUtils.preventDefaultStopPropagation
      // onMouseUp={preventDefaultStopPropagation} //tnr: commenting this out because it was breaking sliders onRelease
      ,
      title: title,
      style: _extends({}, style, {
        display: "flex",
        marginLeft: 15,
        marginRight: 20
      })
    }, /*#__PURE__*/_react["default"].createElement(_core.Icon, {
      onClick: function onClick() {
        var newVal = value - (clickStepSize || stepSize);
        if (newVal < min) {
          if (noWraparound) {
            newVal = min;
          } else {
            newVal = max - (clickStepSize || stepSize);
          }
        }
        _this2.setState({
          value: newVal
        });
        _this2.props.onChange(newVal);
        _this2.props.onRelease && _this2.props.onRelease(newVal);
      },
      style: {
        cursor: "pointer",
        marginRight: 8
      },
      intent: _core.Intent.PRIMARY,
      icon: this.props.leftIcon || "minus"
    }), /*#__PURE__*/_react["default"].createElement(_core.Slider, _extends({}, _extends({}, rest, {
      value: value
    }), {
      onRelease: function onRelease(newVal) {
        return _this2.props.onRelease && _this2.props.onRelease(newVal);
      },
      onChange: function onChange(value) {
        _this2.setState({
          value: value
        });
        _this2.props.onChange && _this2.props.onChange(value);
      }
    })), /*#__PURE__*/_react["default"].createElement(_core.Icon, {
      onClick: function onClick() {
        var newVal = value + (clickStepSize || stepSize);
        if (newVal > max) {
          if (noWraparound) {
            newVal = max;
          } else {
            newVal = min + (clickStepSize || stepSize);
          }
        }
        _this2.setState({
          value: newVal
        });
        _this2.props.onChange(newVal);
        _this2.props.onRelease && _this2.props.onRelease(newVal);
      },
      style: {
        cursor: "pointer",
        marginLeft: 8
      },
      intent: _core.Intent.PRIMARY,
      icon: this.props.rightIcon || "plus"
    }));
  };
  return UncontrolledSliderWithPlusMinusBtns;
}(_react["default"].Component);
exports["default"] = UncontrolledSliderWithPlusMinusBtns;
module.exports = exports.default;