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

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

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

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

var _throttle = _interopRequireDefault(require("lodash/throttle"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

// Style

/* istanbul ignore next */
if (typeof window !== 'undefined') require("./style.scss");
/* istanbul ignore next */

var nearest10s = function nearest10s(num) {
  var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
  return Math.round(num / base) * base;
};

var defaultOpts = {
  showMarks: false,
  showLabels: undefined,
  roundLabels: undefined,
  limit: {
    lower: undefined,
    upper: undefined
  }
};

var Range = function Range(_ref) {
  var list = _ref.list,
      max = _ref.max,
      min = _ref.min,
      options = _ref.options,
      step = _ref.step,
      defaultValue = _ref.defaultValue,
      props = (0, _objectWithoutProperties2["default"])(_ref, ["list", "max", "min", "options", "step", "defaultValue"]);

  var opts = _objectSpread(_objectSpread({}, defaultOpts), options);

  var ROOT = /*#__PURE__*/_react["default"].createRef();

  var INPUT = /*#__PURE__*/_react["default"].createRef();
  /* istanbul ignore next */


  var updateLabel = function updateLabel(num) {
    if (opts.showLabels === false) return '';
    if (opts.roundLabels === false) return num;
    var label;
    var datalist = INPUT.current.list;

    if (datalist) {
      var selector = "option[value=\"".concat(num, "\"]");
      var option = datalist.querySelector(selector);

      if (!option) {
        var rounded = nearest10s(num, opts.roundLabels || step);
        selector = "option[value=\"".concat(rounded, "\"]");
        option = datalist.querySelector(selector);
      }

      label = option && (option.label || num) || (!opts.showLabels ? '' : num);
      if ('updateInfo' in props) props.updateInfo(option); // TODO: change callback to [updateLabels]
    } else {
      label = num;
      if ('updateInfo' in props) props.updateInfo(label); // TODO: change callback to [updateLabels]
    }

    return label;
  };
  /* istanbul ignore next */


  var handleChange = (0, _throttle["default"])(function (e) {
    var target = e.target;
    var val = target.value;
    var out = ROOT.current.querySelector('output');
    var wrap = ROOT.current.closest('.Range');
    out.value = updateLabel(val);
    wrap.style.setProperty('--range-val', val);
    return val;
  }, 88);
  (0, _react.useEffect)(function () {
    var event = new Event('input', {
      bubbles: true
    });
    var target = INPUT.current;
    target.dispatchEvent(event);
  });
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: ROOT,
    className: "Range ".concat(opts.showMarks ? 'Range_marks' :
    /* istanbul ignore next */
    ''),
    style: _objectSpread(_objectSpread({}, props.style), {}, {
      '--range-min': min,
      '--range-max': max,

      /* istanbul ignore next */
      '--range-marks': typeof opts.showMarks === 'number' ? opts.showMarks : step,
      '--range-val': defaultValue
    })
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "Range-progress"
  }), /*#__PURE__*/_react["default"].createElement("input", {
    ref: INPUT,
    className: "Range-input",
    defaultValue: defaultValue,
    list: list,
    max: max,
    min: min,
    onInput: function onInput(e) {
      if (opts.limit.lower && e.target.value < opts.limit.lower) {
        INPUT.current.value = opts.limit.lower;
      }

      e.persist();
      return handleChange(e);
    },
    step: step,
    style: {
      width: '100%'
    },
    type: "range"
  }), /*#__PURE__*/_react["default"].createElement("output", {
    className: "Range-output",
    htmlFor: "TEST"
  }, opts.showLabels && defaultValue), props.children);
};

Range.propTypes = {
  defaultValue: _propTypes["default"].number,
  id: _propTypes["default"].string,
  list: _propTypes["default"].string,
  max: _propTypes["default"].number,
  min: _propTypes["default"].number,
  options: _propTypes["default"].shape({
    limit: _propTypes["default"].shape({
      lower: _propTypes["default"].number,
      upper: _propTypes["default"].number
    }),
    roundLabels: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].bool]),
    showLabels: _propTypes["default"].bool,
    showMarks: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].number])
  }),
  step: _propTypes["default"].number,
  style: _propTypes["default"].object,
  updateInfo: _propTypes["default"].func
};
Range.defaultProps = {
  defaultValue: 12,
  list: '',
  max: 10,
  min: 0,
  options: {
    showLabels: undefined,
    showMarks: false
  },
  step: 1
};
var _default = Range;
exports["default"] = _default;