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

var _tslib = require("tslib");

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

var _one = require("remax/one");

var _common = require("./../common");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }

var prefixCls = (0, _common.getPrefixCls)('progress');

var validProgress = function validProgress(progress) {
  if (!progress || progress < 0) {
    return 0;
  }

  if (progress > 100) {
    return 100;
  }

  return progress;
};

var sortGradient = function sortGradient(gradients) {
  var tempArr = [];
  Object.keys(gradients).forEach(function (key) {
    var formattedKey = parseFloat(key.replace(/%/g, ''));

    if (!isNaN(formattedKey)) {
      tempArr.push({
        key: formattedKey,
        value: gradients[key]
      });
    }
  });
  tempArr = tempArr.sort(function (a, b) {
    return a.key - b.key;
  });
  return tempArr.map(function (_ref) {
    var key = _ref.key,
        value = _ref.value;
    return "".concat(value, " ").concat(key, "%");
  }).join(', ');
};

var handleGradient = function handleGradient(color) {
  var _color$from = color.from,
      from = _color$from === void 0 ? '#1890ff' : _color$from,
      _color$to = color.to,
      to = _color$to === void 0 ? '#1890ff' : _color$to,
      _color$direction = color.direction,
      direction = _color$direction === void 0 ? 'to right' : _color$direction,
      rest = (0, _tslib.__rest)(color, ["from", "to", "direction"]);

  if (Object.keys(rest).length !== 0) {
    var sortedGradients = sortGradient(rest);
    return {
      backgroundImage: "linear-gradient(".concat(direction, ", ").concat(sortedGradients, ")")
    };
  }

  return {
    backgroundImage: "linear-gradient(".concat(direction, ", ").concat(from, ", ").concat(to, ")")
  };
};

var Line = function Line(props) {
  var percent = props.percent,
      width = props.width,
      size = props.size,
      color = props.color,
      bgColor = props.bgColor,
      _props$shape = props.shape,
      shape = _props$shape === void 0 ? 'round' : _props$shape,
      successPercent = props.successPercent,
      children = props.children;
  var backgroundProps;

  if (color && typeof color !== 'string') {
    backgroundProps = handleGradient(color);
  } else {
    backgroundProps = {
      background: color
    };
  }

  var bgStyle;
  var borderRadius = null;

  if (shape === 'sharp') {
    borderRadius = 0;
  }

  if (shape === 'square') {
    borderRadius = '4px';
  }

  if (borderRadius || borderRadius === 0) {
    bgStyle = {
      borderRadius: borderRadius
    };
  }

  if (bgColor && typeof bgColor === 'string') {
    bgStyle = Object.assign(Object.assign({}, bgStyle), {
      backgroundColor: bgColor
    });
  }

  var sizeWidth = '16px';

  if (size === 'small') {
    sizeWidth = '12px';
  }

  if (size === 'large') {
    sizeWidth = '20px';
  }

  var percentStyle = Object.assign({
    width: "".concat(validProgress(percent), "%"),
    height: width || sizeWidth,
    borderRadius: borderRadius
  }, backgroundProps);
  var successPercentStyle = {
    width: "".concat(validProgress(successPercent), "%"),
    height: width || sizeWidth,
    borderRadius: borderRadius
  };
  var successSegment = successPercent !== undefined ? /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-success-bg"),
    style: successPercentStyle
  }) : null;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-outer")
  }, /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-inner"),
    style: bgStyle
  }, /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-bg"),
    style: percentStyle
  }), successSegment)), children);
};

var _default = Line;
exports.default = _default;