import _has from "ramda/src/has";
import _or from "ramda/src/or";
import React from 'react';
import { useIntl } from 'react-intl';
import styled from 'styled-components';
import { assert } from '../../common/utils';
import { Typography, VisuallyHidden } from '../..';
var getPrefix = function getPrefix(sign, value) {
  return sign && value > 0 ? '+' : '';
};
var getTickDecimals = function getTickDecimals(value, ticks) {
  if (process.env.NODE_ENV !== 'production') {
    var wrongTick = ticks.find(_or(_has('from_price'), _has('to_price')));
    assert(!wrongTick, "Found ticks with snake cased keys, they should be in camelcase.");
  }
  var tick = ticks.find(function (t) {
    return value >= t.fromPrice && value < t.toPrice + (t.tick || 0);
  });
  return tick ? tick.decimals : undefined;
};
var getNumberOptions = function getNumberOptions(value, _ref) {
  var ticks = _ref.ticks,
    decimals = _ref.decimals,
    maximumDecimals = _ref.maximumDecimals,
    minimumDecimals = _ref.minimumDecimals;
  if (typeof ticks !== 'undefined') {
    var tick = getTickDecimals(value, ticks);
    return {
      maximumFractionDigits: tick,
      minimumFractionDigits: tick
    };
  }
  if (typeof decimals !== 'undefined') {
    return {
      maximumFractionDigits: decimals,
      minimumFractionDigits: decimals
    };
  }
  if (typeof maximumDecimals !== 'undefined' || typeof minimumDecimals !== 'undefined') {
    return {
      maximumFractionDigits: maximumDecimals,
      minimumFractionDigits: minimumDecimals
    };
  }
  return {};
};
var getDecimalsFromMinMax = function getDecimalsFromMinMax(value) {
  var maximumDecimals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Number.MAX_VALUE;
  var minimumDecimals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var split = value.toString().split('.');
  var decimals = split && split[1] ? split[1].length : 0;
  if (decimals <= minimumDecimals) return minimumDecimals;
  if (decimals >= maximumDecimals) return maximumDecimals;
  return decimals;
};
export var getRoundedValue = function getRoundedValue(value, _ref2) {
  var ticks = _ref2.ticks,
    decimals = _ref2.decimals,
    maximumDecimals = _ref2.maximumDecimals,
    minimumDecimals = _ref2.minimumDecimals;
  var dec = null;
  if (typeof decimals !== 'undefined') dec = decimals;
  if (typeof ticks !== 'undefined') dec = getTickDecimals(value, ticks);
  if (typeof maximumDecimals !== 'undefined' || typeof minimumDecimals !== 'undefined') dec = getDecimalsFromMinMax(value, maximumDecimals, minimumDecimals);
  if (dec === null) return value;
  return +Number(value).toFixed(dec);
};
var TypographyWithInheritedWeight = styled(Typography).withConfig({
  displayName: "Number__TypographyWithInheritedWeight",
  componentId: "sc-1pkc1h1-0"
})(["font-weight:inherit;"]);
export var NumberComponent = function NumberComponent(_ref3) {
  var value = _ref3.value,
    decimals = _ref3.decimals,
    minimumDecimals = _ref3.minimumDecimals,
    maximumDecimals = _ref3.maximumDecimals,
    ticks = _ref3.ticks,
    _ref3$percentage = _ref3.percentage,
    percentage = _ref3$percentage === void 0 ? false : _ref3$percentage,
    currency = _ref3.currency,
    currencySize = _ref3.currencySize,
    _ref3$sign = _ref3.sign,
    sign = _ref3$sign === void 0 ? false : _ref3$sign,
    _ref3$maskValue = _ref3.maskValue,
    maskValue = _ref3$maskValue === void 0 ? false : _ref3$maskValue;
  var _useIntl = useIntl(),
    formatNumber = _useIntl.formatNumber;
  if (typeof value === 'undefined' || value === null || !Number.isFinite(value)) return React.createElement(React.Fragment, null, "-");
  if (typeof currency !== 'undefined' && currency === null) return React.createElement(React.Fragment, null, "-");
  var roundedValue = getRoundedValue(value, {
    ticks: ticks,
    decimals: decimals,
    minimumDecimals: minimumDecimals,
    maximumDecimals: maximumDecimals
  });
  var formattedNumber = formatNumber(value, getNumberOptions(value, {
    ticks: ticks,
    decimals: decimals,
    minimumDecimals: minimumDecimals,
    maximumDecimals: maximumDecimals
  }));
  var isMinusZero = function isMinusZero(val) {
    return 1 / val === -Infinity;
  };
  var getNumber = function getNumber() {
    if (isMinusZero(roundedValue)) {
      return '0';
    }
    return React.createElement(React.Fragment, null, getPrefix(sign, roundedValue), formattedNumber);
  };
  var number = React.createElement(React.Fragment, null, maskValue ? '• • • •' : getNumber(), percentage && '%', currency && typeof currencySize === 'undefined' && " ".concat(currency));
  if (typeof currencySize === 'undefined') return number;
  return React.createElement(React.Fragment, null, React.createElement(VisuallyHidden, null, number, " ".concat(currency)), React.createElement("span", {
    "aria-hidden": true
  }, number, ' ', React.createElement(TypographyWithInheritedWeight, {
    type: currencySize
  }, currency)));
};