"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Property = exports.FormatProperty = void 0;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _clsx2 = _interopRequireDefault(require("clsx"));

var _Icon = require("../Icon");

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

var _utils = require("../Canary/utils");

var _jsxRuntime = require("react/jsx-runtime");

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var FormatProperty = function FormatProperty(_ref) {
  var property = _ref.property,
      _ref$short = _ref.short,
      short = _ref$short === void 0 ? false : _ref$short;

  if (property == null) {
    return "undefined";
  }

  var text = property.text;

  if (property.type === "url") {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
      href: property.text,
      target: "_blank",
      rel: "noreferrer",
      className: "underline",
      children: property.text.replace("https://", "")
    });
  }

  if (property.value != null) {
    if (property.max != null) {
      var percent = (property.value / property.max * 100).toFixed(0);
      text = "".concat(percent, "%");

      if (percent > 70) {
        text = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          className: "text-red-500",
          children: text
        });
      }
    } else if (property.unit && property.unit.startsWith("milli")) {
      text = (property.value / 1000).toFixed;
    } else if (property.unit === "bytes") {
      text = (0, _common.formatBytes)(property.value, 0);
    }

    if (!short && property.max != null) {
      if (property.unit.startsWith("milli")) {
        text += " of ".concat((property.max / 1000).toFixed(2));
      } else if (property.unit === "bytes") {
        text += " of ".concat((0, _common.formatBytes)(property.max, 0));
      }
    }
  }

  if ((0, _utils.isEmpty)(text)) {
    return "";
  }

  return text;
};

exports.FormatProperty = FormatProperty;

var Property = function Property(_ref2) {
  var property = _ref2.property,
      className = _ref2.className;
  var name = property.name,
      icon = property.icon,
      color = property.color;

  if (property.type === "hidden" || (0, _utils.isEmpty)(property.text) && (0, _utils.isEmpty)(property.value)) {
    return null;
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _clsx2.default)("flex", _defineProperty({}, className, className)),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
      name: icon,
      className: "mr-1",
      size: "2xsi"
    }), !(0, _utils.isEmpty)(name) && /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
      className: "text-xs overflow-hidden truncate text-gray-400 pr-1",
      children: [name, ":"]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: (0, _clsx2.default)("text-xs overflow-hidden truncate", "text-".concat(color, "-500")),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(FormatProperty, {
        property: property
      })
    })]
  });
};

exports.Property = Property;
Property.propTypes = {
  // property: PropTypes.shape({
  //   name: PropTypes.string.isRequired,
  //   text: PropTypes.string,
  // }).isRequired,
  className: _propTypes.default.string
};
Property.defaultProps = {
  className: ""
};