function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useRef, useState } from 'react';
import styled, { css } from 'styled-components';
import { isHTMLElement } from '../../common/utils';
import { useSafeLayoutEffect } from '../../common/Hooks';
import { Box, Flexbox, Number, Typography } from '../..';
var TRIANGLE_TOP_BORDER_SIZE = 2;
var TRIANGLE_SIDE_BORDER_SIZE = 6;
var TRIANGLE_OFFSET = 1;
export var intersectionStyles = css(["height:", "px;width:", "px;position:absolute;bottom:100%;margin-bottom:", "px;pointer-events:none;"], function (p) {
  return p.theme.spacing.unit(1);
}, function (p) {
  return p.theme.spacing.unit(1);
}, function (_ref) {
  var theme = _ref.theme;
  return theme.spacing.unit(TRIANGLE_OFFSET + TRIANGLE_TOP_BORDER_SIZE);
});
var IntersectionLeft = styled.div.withConfig({
  displayName: "LineScale__IntersectionLeft",
  componentId: "sc-1jdwn7d-0"
})(["", " left:-6px;"], intersectionStyles);
var IntersectionRight = styled.div.withConfig({
  displayName: "LineScale__IntersectionRight",
  componentId: "sc-1jdwn7d-1"
})(["", " right:-6px;"], intersectionStyles);
var leftCollisionStyle = css(["left:-", "px;margin:0;transform:translateY(-100%) translateY(-10px);"], TRIANGLE_SIDE_BORDER_SIZE);
var rightCollisionStyle = css(["right:-", "px;margin-right:0;margin-left:100%;transform:translateX(-100%) translateY(-100%) translateY(-10px);"], TRIANGLE_SIDE_BORDER_SIZE);
var transformStyle = css(["transform:translateX(-50%) translateY(-100%) translateY(-10px);"]);
var Indicator = styled('span').withConfig({
  displayName: "LineScale__Indicator",
  componentId: "sc-1jdwn7d-2"
})(["position:relative;bottom:100%;margin:0 ", "% ", "px;padding:2px ", "px;", " ", " ", " height:", "px;white-space:nowrap;text-align:center;background:", ";color:", ";border-radius:", ";"], function (p) {
  return p.$value;
}, function (_ref2) {
  var theme = _ref2.theme;
  return theme.spacing.unit(TRIANGLE_OFFSET + TRIANGLE_TOP_BORDER_SIZE);
}, function (_ref3) {
  var theme = _ref3.theme;
  return theme.spacing.unit(1.5);
}, function (p) {
  return p.$leftCollision && leftCollisionStyle;
}, function (p) {
  return p.$rightCollision && rightCollisionStyle;
}, function (p) {
  return !p.$leftCollision && !p.$rightCollision && transformStyle;
}, function (_ref4) {
  var theme = _ref4.theme;
  return theme.spacing.unit(4);
}, function (p) {
  return p.$valueColor || p.theme.color.lineScaleValueColor;
}, function (_ref5) {
  var theme = _ref5.theme;
  return theme.color.textLight;
}, function (_ref6) {
  var theme = _ref6.theme;
  return theme.borderRadius2;
});
var AverageLabel = styled('span').withConfig({
  displayName: "LineScale__AverageLabel",
  componentId: "sc-1jdwn7d-3"
})(["position:relative;bottom:-", "px;margin-left:", "%;margin-right:", "%;", " ", " ", " padding:2px ", "px;white-space:nowrap;text-align:center;color:", ";"], function (_ref7) {
  var theme = _ref7.theme;
  return theme.spacing.unit(8);
}, function (p) {
  return p.$averageValue;
}, function (p) {
  return p.$averageValue;
}, function (p) {
  return p.$leftCollision && leftCollisionStyle;
}, function (p) {
  return p.$rightCollision && rightCollisionStyle;
}, function (p) {
  return !p.$leftCollision && !p.$rightCollision && transformStyle;
}, function (_ref8) {
  var theme = _ref8.theme;
  return theme.spacing.unit(1.5);
}, function (_ref9) {
  var theme = _ref9.theme;
  return theme.color.label;
});
var StyledBox = styled(Box).withConfig({
  displayName: "LineScale__StyledBox",
  componentId: "sc-1jdwn7d-4"
})(["position:relative;"]);
var StyledFlexbox = styled(Flexbox).withConfig({
  displayName: "LineScale__StyledFlexbox",
  componentId: "sc-1jdwn7d-5"
})(["position:relative;height:2px;background-color:", ";margin-bottom:", "px;&::before{display:block;content:'';position:absolute;bottom:-", "px;margin-left:", "%;width:0;height:", "px;border-left:1px dashed ", ";}&::after{display:block;content:'';position:absolute;bottom:100%;margin-bottom:", "px;margin-left:calc(", "% - ", "px);margin-right:calc(", "% - ", "px);width:0;height:0;border-left:", "px solid transparent;border-right:", "px solid transparent;border-top:", "px solid ", ";}"], function (_ref10) {
  var theme = _ref10.theme;
  return theme.color.backgroundBlack;
}, function (_ref11) {
  var theme = _ref11.theme;
  return theme.spacing.unit(2);
}, function (_ref12) {
  var theme = _ref12.theme;
  return theme.spacing.unit(6);
}, function (p) {
  return p.$averageValue;
}, function (_ref13) {
  var theme = _ref13.theme;
  return theme.spacing.unit(14);
}, function (_ref14) {
  var theme = _ref14.theme;
  return theme.color.label;
}, function (_ref15) {
  var theme = _ref15.theme;
  return theme.spacing.unit(TRIANGLE_OFFSET);
}, function (p) {
  return p.$value;
}, TRIANGLE_SIDE_BORDER_SIZE, function (p) {
  return p.$value;
}, TRIANGLE_SIDE_BORDER_SIZE, TRIANGLE_SIDE_BORDER_SIZE, TRIANGLE_SIDE_BORDER_SIZE, function (_ref16) {
  var theme = _ref16.theme;
  return theme.spacing.unit(TRIANGLE_TOP_BORDER_SIZE);
}, function (p) {
  return p.$valueColor || p.theme.color.lineScaleValueColor;
});
var xAxisCollision = function xAxisCollision(a, b) {
  if (!isHTMLElement(a) || !isHTMLElement(b)) {
    return false;
  }
  var _b$getBoundingClientR = b.getBoundingClientRect(),
    bX = _b$getBoundingClientR.x,
    bWidth = _b$getBoundingClientR.width;
  return a.getBoundingClientRect().x <= bX + bWidth;
};
export var LineScale = function LineScale(_ref17) {
  var value = _ref17.value,
    valueLabel = _ref17.valueLabel,
    valueColor = _ref17.valueColor,
    averageValue = _ref17.averageValue,
    averageValueLabel = _ref17.averageValueLabel,
    minLabel = _ref17.minLabel,
    maxLabel = _ref17.maxLabel;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    leftCollision = _useState2[0],
    setLeftCollision = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    leftAverageTextCollision = _useState4[0],
    setLeftAverageTextCollision = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    rightCollision = _useState6[0],
    setRightCollision = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    rightAverageTextCollision = _useState8[0],
    setRightAverageTextCollision = _useState8[1];
  var indicatorRef = useRef(null);
  var averageTextRef = useRef(null);
  var intersectionLeft = useRef(null);
  var intersectionRight = useRef(null);
  useSafeLayoutEffect(function () {
    if (indicatorRef.current && intersectionLeft.current) {
      var hasLeftCollision = xAxisCollision(indicatorRef.current, intersectionLeft.current);
      setLeftCollision(hasLeftCollision);
    }
    if (indicatorRef.current && intersectionRight.current) {
      var hasRightCollision = xAxisCollision(intersectionRight.current, indicatorRef.current);
      setRightCollision(hasRightCollision);
    }
    if (averageTextRef.current && intersectionLeft.current) {
      var _hasLeftCollision = xAxisCollision(averageTextRef.current, intersectionLeft.current);
      setLeftAverageTextCollision(_hasLeftCollision);
    }
    if (averageTextRef.current && intersectionRight.current) {
      var _hasRightCollision = xAxisCollision(intersectionRight.current, averageTextRef.current);
      setRightAverageTextCollision(_hasRightCollision);
    }
  }, [indicatorRef, averageTextRef, intersectionRight, intersectionLeft]);
  return React.createElement(Typography, {
    type: "tertiary",
    color: function color(t) {
      return t.color.label;
    }
  }, React.createElement(StyledBox, {
    mt: 8
  }, React.createElement(Flexbox, {
    container: true,
    direction: "column"
  }, React.createElement(IntersectionLeft, {
    ref: intersectionLeft
  }), React.createElement(IntersectionRight, {
    ref: intersectionRight
  }), React.createElement(Flexbox, {
    item: true,
    flex: "1 1 auto"
  }, React.createElement(StyledFlexbox, {
    container: true,
    $value: value,
    $averageValue: averageValue,
    $valueColor: valueColor
  }, React.createElement(Indicator, {
    $value: value,
    $valueColor: valueColor,
    $leftCollision: leftCollision,
    $rightCollision: rightCollision,
    ref: indicatorRef
  }, React.createElement(Typography, {
    type: "tertiary",
    color: function color(t) {
      return t.color.textLight;
    }
  }, Boolean(valueLabel) && valueLabel || React.createElement(Number, {
    value: value,
    decimals: 2
  }))))), (Boolean(minLabel) || Boolean(maxLabel)) && React.createElement(Flexbox, {
    container: true,
    justifyContent: "space-between"
  }, React.createElement(Flexbox, {
    item: true
  }, minLabel), React.createElement(Flexbox, {
    item: true
  }, maxLabel)), (Boolean(averageValue) || Boolean(averageValueLabel)) && React.createElement(Flexbox, {
    item: true,
    flex: "1 1 auto"
  }, React.createElement(Flexbox, {
    container: true
  }, React.createElement(AverageLabel, {
    $leftCollision: leftAverageTextCollision,
    $rightCollision: rightAverageTextCollision,
    ref: averageTextRef,
    $averageValue: averageValue
  }, React.createElement(Typography, {
    type: "tertiary",
    color: function color(t) {
      return t.color.label;
    }
  }, averageValueLabel)))))));
};
export default LineScale;