"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.CircularProgressBar = void 0;

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

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

var _circularProgressBar = require("./circular-progress-bar.constants");

var _box = require("../../../primitives/box");

var _circularProgressBar2 = require("./circular-progress-bar.css-rules");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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 _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(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var SVG = _styledComponents["default"].svg.withConfig({
  displayName: "SVG",
  componentId: "cd7llc-0"
})(["width:100%;"]);

var Path = _styledComponents["default"].path.withConfig({
  displayName: "Path",
  componentId: "cd7llc-1"
})(["stroke:", ";stroke-linecap:square;stroke-width:", ";transition:stroke-dashoffset 0.5s ease 0s;"], _circularProgressBar2.getPathColor, function (props) {
  return props.strokeWidth;
});

var Trail = _styledComponents["default"].path.withConfig({
  displayName: "Trail",
  componentId: "cd7llc-2"
})(["stroke:", ";stroke-width:", ";"], _circularProgressBar2.getTrailColor, function (props) {
  return props.strokeWidth;
});

var FixedSizeBox = (0, _styledComponents["default"])(_box.Box).withConfig({
  displayName: "FixedSizeBox",
  componentId: "cd7llc-3"
})(["height:", ";width:", ";"], function (_ref) {
  var size = _ref.size;
  return size || _circularProgressBar.DEFAULT_SIZE;
}, function (_ref2) {
  var size = _ref2.size;
  return size || _circularProgressBar.DEFAULT_SIZE;
});

var getPathProps = function getPathProps(strokeWidth, counterClockwise, percentage) {
  // the radius of the path is defined to be in the middle, so in order for the path to
  // fit perfectly inside the 100x100 viewBox, need to subtract half the strokeWidth
  var pathRadius = _circularProgressBar.FULL_RADIUS - strokeWidth / 2;
  var rotation = counterClockwise ? 1 : 0; // Move to center of canvas
  // Relative move to top canvas
  // Relative arc to bottom of canvas
  // Relative arc to top of canvas

  var pathDescription = "\n      M ".concat(_circularProgressBar.CENTER_X, ",").concat(_circularProgressBar.CENTER_Y, "\n      m 0,-").concat(pathRadius, "\n      a ").concat(pathRadius, ",").concat(pathRadius, " ").concat(rotation, " 1 1 0,").concat(2 * pathRadius, "\n      a ").concat(pathRadius, ",").concat(pathRadius, " ").concat(rotation, " 1 1 0,-").concat(2 * pathRadius, "\n    "); // Calculate pathStyles

  var diameter = Math.PI * 2 * pathRadius;
  var truncatedPercentage = Math.min(Math.max(percentage, _circularProgressBar.MIN_PERCENTAGE), _circularProgressBar.MAX_PERCENTAGE);
  var dashoffset = (100 - truncatedPercentage) / 100 * diameter;
  var pathStyles = {
    strokeDasharray: "".concat(diameter, "px ").concat(diameter, "px"),
    strokeDashoffset: "".concat(counterClockwise ? -dashoffset : dashoffset, "px"),
    strokeLinecap: 'butt'
  };
  return {
    pathDescription: pathDescription,
    pathStyles: pathStyles
  };
};

var CircularProgressBar = function CircularProgressBar(_ref3) {
  var _ref3$color = _ref3.color,
      color = _ref3$color === void 0 ? _circularProgressBar.DEFAULT_FG_COLOR : _ref3$color,
      _ref3$bgColor = _ref3.bgColor,
      bgColor = _ref3$bgColor === void 0 ? _circularProgressBar.DEFAULT_BG_COLOR : _ref3$bgColor,
      percentage = _ref3.percentage,
      className = _ref3.className,
      _ref3$strokeWidth = _ref3.strokeWidth,
      strokeWidth = _ref3$strokeWidth === void 0 ? _circularProgressBar.DEFAULT_STROKE_WIDTH : _ref3$strokeWidth,
      _ref3$initialAnimatio = _ref3.initialAnimation,
      initialAnimation = _ref3$initialAnimatio === void 0 ? false : _ref3$initialAnimatio,
      _ref3$counterClockwis = _ref3.counterClockwise,
      counterClockwise = _ref3$counterClockwis === void 0 ? false : _ref3$counterClockwis,
      size = _ref3.size,
      spacing = _ref3.spacing,
      dataQa = _ref3['data-qa'],
      dataCy = _ref3['data-cy'],
      id = _ref3.id;

  var _useState = (0, _react.useState)(initialAnimation ? 0 : percentage),
      _useState2 = _slicedToArray(_useState, 2),
      _percentage = _useState2[0],
      setPercentage = _useState2[1];

  (0, _react.useEffect)(function () {
    var timeout = setTimeout(function () {
      return setPercentage(percentage);
    }, _circularProgressBar.ANIMATION_DELAY);
    return function () {
      return clearTimeout(timeout);
    };
  }, [percentage]);

  var _useMemo = (0, _react.useMemo)(function () {
    return getPathProps(strokeWidth, counterClockwise, _percentage);
  }, [_percentage, counterClockwise, strokeWidth]),
      pathDescription = _useMemo.pathDescription,
      pathStyles = _useMemo.pathStyles;

  return /*#__PURE__*/_react["default"].createElement(FixedSizeBox, {
    size: size,
    role: "progressbar",
    "aria-valuenow": percentage,
    "aria-valuemin": "0",
    "aria-valuemax": "100",
    spacing: spacing,
    "data-cy": dataCy,
    "data-qa": dataQa,
    id: id
  }, /*#__PURE__*/_react["default"].createElement(SVG, {
    className: className,
    viewBox: "0 0 ".concat(_circularProgressBar.MAX_X, " ").concat(_circularProgressBar.MAX_Y)
  }, /*#__PURE__*/_react["default"].createElement(Trail, {
    d: pathDescription,
    strokeWidth: strokeWidth,
    fillOpacity: 0,
    bgColor: bgColor
  }), /*#__PURE__*/_react["default"].createElement(Path, {
    d: pathDescription,
    strokeWidth: strokeWidth,
    fillOpacity: 0,
    style: pathStyles,
    color: color
  })));
};

exports.CircularProgressBar = CircularProgressBar;
CircularProgressBar.displayName = 'CircularProgressBar';