import _head from "ramda/src/head";
var _excluded = ["delay", "fallback"];
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; }
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; }
import React, { useEffect, useState } from 'react';
import styled, { useTheme } from 'styled-components';
import { isArray, isString } from '../../common/utils';
var Animation = styled.span.withConfig({
  displayName: "Spinner__Animation",
  componentId: "sc-x7jzy3-0"
})(["@keyframes spinner{from{transform:rotate(0deg);}to{transform:rotate(360deg);}}display:inline-block;animation:spinner 0.6s linear infinite;"]);
var StyledSvg = styled.svg.withConfig({
  displayName: "Spinner__StyledSvg",
  componentId: "sc-x7jzy3-1"
})(["display:block;"]);
var evalColor = function evalColor(theme, color) {
  if (typeof color === 'function') {
    var usedColor = color(theme);
    if (isArray(usedColor)) {
      return _head(usedColor) || theme.color.cta;
    }
    return isString(usedColor) ? usedColor : theme.color.cta;
  }
  return color || theme.color.cta;
};
var getDelay = function getDelay(delay) {
  if (typeof delay === 'number') {
    return delay;
  }
  return delay === false ? 0 : 1000;
};
var RawSpinner = function RawSpinner(_ref) {
  var _ref$size = _ref.size,
    size = _ref$size === void 0 ? 4 : _ref$size,
    color = _ref.color,
    id = _ref.id;
  var theme = useTheme();
  var calculatedSize = theme.spacing.unit(size);
  var id1 = "spinner-".concat(id, "-1");
  var id2 = "spinner-".concat(id, "-2");
  var usedColor = evalColor(theme, color);
  return React.createElement(Animation, null, React.createElement(StyledSvg, {
    width: calculatedSize,
    height: calculatedSize,
    viewBox: "0 0 24 24"
  }, React.createElement("defs", null, React.createElement("linearGradient", {
    id: id1,
    x1: "50%",
    x2: "50%",
    y1: "0%",
    y2: "100%"
  }, React.createElement("stop", {
    offset: "0%",
    stopColor: usedColor,
    stopOpacity: "0"
  }), React.createElement("stop", {
    offset: "100%",
    stopColor: usedColor,
    stopOpacity: ".5"
  })), React.createElement("linearGradient", {
    id: id2,
    x1: "50%",
    x2: "50%",
    y1: "0%",
    y2: "100%"
  }, React.createElement("stop", {
    offset: "0%",
    stopColor: usedColor,
    stopOpacity: ".5"
  }), React.createElement("stop", {
    offset: "100%",
    stopColor: usedColor
  }))), React.createElement("g", {
    fill: "none",
    fillRule: "evenodd"
  }, React.createElement("path", {
    fill: "url(#".concat(id1, ")"),
    d: "M12,0 C18.627417,0 24,5.372583 24,12 C24,18.627417 18.627417,24 12,24 M12,21 C16.9705627,21 21,16.9705627 21,12 C21,7.02943725 16.9705627,3 12,3"
  }), React.createElement("path", {
    fill: "url(#".concat(id2, ")"),
    d: "M0,0 C6.627417,0 12,5.372583 12,12 C12,18.627417 6.627417,24 0,24 M0,21 C4.97056275,21 9,16.9705627 9,12 C9,7.02943725 4.97056275,3 0,3",
    transform: "rotate(-180 6 12)"
  }))));
};
export var Spinner = function Spinner(_ref2) {
  var delay = _ref2.delay,
    fallback = _ref2.fallback,
    restProps = _objectWithoutProperties(_ref2, _excluded);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    spinning = _useState2[0],
    setSpinning = _useState2[1];
  var noDelay = delay === 0 || delay === false;
  useEffect(function () {
    var timer = setTimeout(function () {
      setSpinning(true);
    }, getDelay(delay));
    return function () {
      return clearTimeout(timer);
    };
  }, []);
  if (noDelay || spinning) {
    return React.createElement(RawSpinner, restProps);
  }
  if (fallback) {
    return fallback;
  }
  return null;
};
Spinner.displayName = 'Spinner';