"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Range = exports.NoMarginText = void 0;

var _lodash = require("lodash");

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

var _styledComponents = _interopRequireWildcard(require("styled-components"));

var _uuid = require("uuid");

var _primitives = require("../../../primitives");

var _typography = require("../../typography");

var _branding = require("../../../mixins/branding");

var _focus = require("../../../mixins/focus");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

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; }

var focusStateMixin = (0, _styledComponents.css)(["box-shadow:", ",", " ", ";"], _focus.focusedBoxShadow, _focus.focusedBorderShadow, _focus.focusedBorderColor);

var getRangeInputBackgroundColor = function getRangeInputBackgroundColor(_ref) {
  var theme = _ref.theme,
      greyOut = _ref.greyOut;
  var colorCode = greyOut ? 'greyscale-medium' : 'brand-secondary-base';
  return (0, _branding.getPaletteColor)(colorCode)({
    theme: theme
  });
};

var getWidth = function getWidth(_ref2) {
  var value = _ref2.value,
      options = _ref2.options;
  var index = (0, _lodash.indexOf)(options, value); // want 0-indexed length

  return "".concat(index / (options.length - 1) * 100);
};

var StyledRangeInput = _styledComponents["default"].input.withConfig({
  displayName: "StyledRangeInput",
  componentId: "sc-19bo2by-0"
})(["appearance:none;margin:12px 0 0;width:100%;height:6px;background:linear-gradient( to right,", " 0%,", " ", "%,", " ", "%,", " 100% );::-webkit-slider-runnable-track{border-radius:3px;cursor:pointer;height:6px;width:100%;}::-webkit-slider-thumb{appearance:none;background:#fff;transition:", ";border-radius:10px;box-shadow:0 2px 8px rgba(0,0,0,0.2);cursor:pointer;height:20px;margin-top:-7px;width:20px;}::-moz-range-track{background:", ";border-radius:3px;cursor:pointer;height:6px;width:100%;}::-moz-range-thumb{background:#fff;border-radius:10px;box-shadow:0 2px 8px rgba(0,0,0,0.2);cursor:pointer;height:20px;width:20px;transition:", ";}::-moz-range-progress{background:", ";border-radius:3px;cursor:pointer;height:6px;width:100%;}::-ms-track{background:transparent;border-color:transparent;color:transparent;cursor:pointer;height:6px;width:100%;}::-ms-fill-lower{background:", ";border-radius:3px;}::-ms-fill-upper{background:", ";border-radius:3px;}::-ms-thumb{background:#fff;border-radius:10px;box-shadow:0 2px 8px rgba(0,0,0,0.2);cursor:pointer;height:20px;width:20px;transition:", ";}:focus{outline:none;::-webkit-slider-thumb{", "}::-moz-range-thumb{", "}::-ms-thumb{", "}}"], getRangeInputBackgroundColor, getRangeInputBackgroundColor, getWidth, (0, _branding.getPaletteColor)('greyscale-light'), getWidth, (0, _branding.getPaletteColor)('greyscale-light'), _focus.transitionFade, (0, _branding.getPaletteColor)('greyscale-light'), _focus.transitionFade, getRangeInputBackgroundColor, getRangeInputBackgroundColor, (0, _branding.getPaletteColor)('greyscale-light'), _focus.transitionFade, focusStateMixin, focusStateMixin, focusStateMixin);

var NoMarginText = (0, _styledComponents["default"])(_typography.Text).withConfig({
  displayName: "NoMarginText",
  componentId: "sc-19bo2by-1"
})(["margin-bottom:0;"]);
exports.NoMarginText = NoMarginText;
var SliderContainer = (0, _styledComponents["default"])(_primitives.Box).withConfig({
  displayName: "SliderContainer",
  componentId: "sc-19bo2by-2"
})(["position:relative;"]);
var SliderLabel = (0, _styledComponents["default"])(_typography.Head5).withConfig({
  displayName: "SliderLabel",
  componentId: "sc-19bo2by-3"
})(["color:", ";"], (0, _branding.getPaletteColor)('greyscale-dark'));

var Labels = function Labels(_ref3) {
  var minLabel = _ref3.minLabel,
      maxLabel = _ref3.maxLabel;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(SliderLabel, {
    align: "left"
  }, minLabel), /*#__PURE__*/_react["default"].createElement(SliderLabel, {
    align: "right"
  }, maxLabel));
};

var LegendContainer = function LegendContainer(_ref4) {
  var children = _ref4.children;
  return /*#__PURE__*/_react["default"].createElement(_primitives.Box, {
    stacked: "row",
    justify: "space-between",
    align: "center",
    spacing: "pt1"
  }, children);
};

var range = function range(_ref5) {
  var start = _ref5.start,
      stop = _ref5.stop,
      step = _ref5.step;
  return Array.from({
    length: (stop - start) / step + 1
  }, function (_, i) {
    return start + i * step;
  });
};

var Range = function Range(props) {
  var onRangeChange = props.onRangeChange,
      _props$minLabel = props.minLabel,
      minLabel = _props$minLabel === void 0 ? '' : _props$minLabel,
      _props$maxLabel = props.maxLabel,
      maxLabel = _props$maxLabel === void 0 ? '' : _props$maxLabel,
      min = props.min,
      max = props.max,
      value = props.value,
      ariaValuetext = props.ariaValuetext,
      _props$spacing = props.spacing,
      spacing = _props$spacing === void 0 ? 'mt1' : _props$spacing,
      grow = props.grow,
      shrink = props.shrink,
      basis = props.basis,
      onBlur = props.onBlur,
      onFocus = props.onFocus,
      className = props.className,
      id = props.id,
      rest = _objectWithoutProperties(props, ["onRangeChange", "minLabel", "maxLabel", "min", "max", "value", "ariaValuetext", "spacing", "grow", "shrink", "basis", "onBlur", "onFocus", "className", "id"]);

  var rangeInputId = (0, _react.useMemo)(function () {
    return "range-".concat((0, _uuid.v4)());
  }, []);
  var options = range({
    start: min,
    stop: max,
    step: 1
  });
  var rangeInputLabel;
  var showLabel = true;
  var label = '';

  if (props.ariaLabelledBy != null) {
    rangeInputLabel = {
      'aria-labelledby': props.ariaLabelledBy
    };
  } else if (props.label != null) {
    rangeInputLabel = {
      'aria-label': props.label
    }; // $FlowFixMe the type guard above should prevent this from being an issue.

    showLabel = !props.hideLabel;
    label = props.label;
  }

  return /*#__PURE__*/_react["default"].createElement(SliderContainer, _extends({
    spacing: spacing,
    grow: grow,
    shrink: shrink,
    basis: basis
  }, {
    "data-cy": props['data-cy'],
    "data-qa": props['data-qa'],
    id: id,
    className: className
  }, rest), showLabel && /*#__PURE__*/_react["default"].createElement(_typography.FieldLabel, {
    htmlFor: rangeInputId
  }, label), /*#__PURE__*/_react["default"].createElement(StyledRangeInput, _extends({
    type: "range",
    role: "slider",
    min: min,
    max: max,
    options: options,
    value: value,
    id: rangeInputId,
    onChange: function onChange(event) {
      return onRangeChange(parseInt(event.target.value, 10));
    },
    "aria-orientation": "horizontal",
    "aria-valuetext": ariaValuetext,
    onFocus: onFocus,
    onBlur: onBlur
  }, rangeInputLabel)), /*#__PURE__*/_react["default"].createElement(LegendContainer, null, /*#__PURE__*/_react["default"].createElement(Labels, {
    minLabel: (0, _lodash.isEmpty)(minLabel) ? min.toString() : minLabel,
    maxLabel: (0, _lodash.isEmpty)(maxLabel) ? max.toString() : maxLabel
  })));
};

exports.Range = Range;
Range.displayName = 'Range';