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

import React from 'react';
import { StyleSheet, Animated, Easing } from 'react-native';
import { Box, Text } from '../../..';
import styled from 'styled-components';
import { color, border } from 'styled-system';
import { usePropsConfig } from '../../../theme';
const StyleAnimatedView = styled(Animated.View)(color, border);

const CircularProgress = ({
  value,
  size,
  isIndeterminate,
  thickness,
  color: colorProp,
  trackColor,
  max,
  min,
  ...props
}) => {
  if (min) {
    value = value - min;
  }

  if (!size) {
    size = 'md';
  }

  let sizeProps;
  let newProps = usePropsConfig('CircularProgress', {
    size: size
  });

  if (typeof size === 'string') {
    sizeProps = {
      height: newProps.height,
      width: newProps.width
    };
  } else {
    sizeProps = {
      height: size,
      width: size
    };
  }

  let defaultThickness = 4;

  if (thickness) {
    defaultThickness = thickness;
  }

  const degree = new Animated.Value(0);

  if (isIndeterminate) {
    Animated.loop(Animated.timing(degree, {
      toValue: 1,
      duration: 600,
      easing: Easing.linear,
      useNativeDriver: true
    })).start();
  }

  const defaultStyling = {
    borderRadius: 100,
    position: 'absolute',
    borderLeftColor: 'transparent',
    borderBottomColor: 'transparent',
    ...sizeProps
  };
  const styles = StyleSheet.create({
    firstProgressLayer: {
      borderWidth: defaultThickness,
      ...defaultStyling,
      transform: [{
        rotateZ: '-135deg'
      }]
    },
    secondProgressLayer: {
      borderWidth: defaultThickness,
      ...defaultStyling,
      transform: [{
        rotateZ: '45deg'
      }]
    },
    offsetLayer: {
      borderWidth: defaultThickness,
      ...defaultStyling,
      transform: [{
        rotateZ: '-135deg'
      }]
    },
    animateStyle: {
      borderWidth: defaultThickness,
      ...defaultStyling,
      borderTopColor: 'transparent',
      transform: [{
        rotateZ: degree.interpolate({
          inputRange: [0, 1],
          outputRange: ['0deg', '360deg']
        })
      }]
    }
  });
  let halfSide = (max ? min ? max - min : max : 100) / 2; // calculating the halfvalue of the progress according to min and max

  const propStyle = (percent, base_degrees) => {
    const rotateBy = base_degrees + percent * 180 / halfSide;
    return {
      transform: [{
        rotateZ: rotateBy + 'deg'
      }]
    };
  };

  const renderThirdLayer = percent => {
    if (percent > halfSide) {
      return /*#__PURE__*/React.createElement(Box, {
        borderColor: colorProp ? colorProp : 'green.4',
        style: [styles.secondProgressLayer, propStyle(percent - halfSide, 45)]
      });
    } else {
      return /*#__PURE__*/React.createElement(Box, {
        borderColor: trackColor ? trackColor : 'gray.2',
        style: styles.offsetLayer
      });
    }
  };

  let firstProgressLayerStyle;

  if (value > halfSide) {
    firstProgressLayerStyle = propStyle(halfSide, -135);
  } else {
    firstProgressLayerStyle = propStyle(value, -135);
  }

  return /*#__PURE__*/React.createElement(Box, _extends({}, sizeProps, {
    rounded: 100,
    borderWidth: defaultThickness,
    borderColor: trackColor ? trackColor : 'gray.2',
    justifyContent: "center",
    alignItems: "center"
  }, props), !isIndeterminate ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Box, {
    borderColor: colorProp ? colorProp : 'green.4',
    style: [styles.firstProgressLayer, firstProgressLayerStyle]
  }), renderThirdLayer(value), /*#__PURE__*/React.createElement(Box, {
    fontSize: sizeProps.height / 4
  }, props.children)) : /*#__PURE__*/React.createElement(StyleAnimatedView, {
    borderColor: colorProp,
    style: styles.animateStyle
  }));
};

export const CircularProgressLabel = props => {
  return /*#__PURE__*/React.createElement(Text, props);
};
export default CircularProgress;
//# sourceMappingURL=index.js.map