import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isNil from '../../node_modules/lodash-es/isNil.js';
import 'core-js/modules/es.array.concat.js';
import 'core-js/modules/es.object.values.js';
import React__default from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { gray60 } from '@carbon/colors';
import { Link } from '@carbon/react';
import { CARD_LAYOUTS } from '../../constants/LayoutConstants.js';
import CardIcon from '../ImageCard/CardIcon.js';
import useMatchingThreshold from '../../hooks/useMatchingThreshold.js';
import { Tooltip } from '../Tooltip/index.js';
import { getTranslatedLabel } from '../../utils/cardUtilityFunctions.js';
import ValueRenderer from './ValueRenderer.js';
import UnitRenderer from './UnitRenderer.js';
import { BASE_CLASS_NAME } from './valueCardUtils.js';
import { CaretUp, CaretDown } from '@carbon/icons-react';

var propTypes = {
  attribute: PropTypes.shape({
    label: PropTypes.string,
    unit: PropTypes.string,
    dataSourceId: PropTypes.string,
    measurementUnitLabel: PropTypes.string,
    // decimal precision
    precision: PropTypes.number,
    thresholds: PropTypes.arrayOf(PropTypes.shape({
      comparison: PropTypes.oneOf(['<', '>', '=', '<=', '>=']).isRequired,
      value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
      color: PropTypes.string,
      icon: PropTypes.string
    })),
    tooltip: PropTypes.string,
    // Position of threshold icon: 'label' (inline with label) or 'unit' (above unit)
    thresholdsIconPosition: PropTypes.oneOf(['label', 'unit'])
  }).isRequired,
  customFormatter: PropTypes.func,
  formatter: PropTypes.func,
  isEditable: PropTypes.bool,
  layout: PropTypes.oneOf(Object.values(CARD_LAYOUTS)),
  locale: PropTypes.string,
  /** whether to use translated labels in cards */
  shouldUseTranslatedLabels: PropTypes.bool,
  i18n: PropTypes.objectOf(PropTypes.any),
  renderIconByName: PropTypes.func,
  /** Optional trend information */
  secondaryValue: PropTypes.shape({
    color: PropTypes.string,
    trend: PropTypes.oneOf(['up', 'down']),
    value: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.number]),
    href: PropTypes.string,
    onClick: PropTypes.func
  }),
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.number]),
  fontSize: PropTypes.number.isRequired,
  /** optional option to determine whether the number should be abbreviated (i.e. 10,000 = 10K) */
  isNumberValueCompact: PropTypes.bool.isRequired,
  /** number of attributes */
  attributeCount: PropTypes.number.isRequired,
  testId: PropTypes.string,
  /** callback when the attribute is clicked to trigger further action like opening a modal */
  onValueClick: PropTypes.func
};
var defaultProps = {
  layout: null,
  value: null,
  renderIconByName: null,
  secondaryValue: null,
  locale: 'en',
  customFormatter: null,
  formatter: null,
  isEditable: false,
  shouldUseTranslatedLabels: false,
  i18n: {},
  testId: 'attribute',
  onValueClick: null
};
var BEM_BASE = "".concat(BASE_CLASS_NAME, "__attribute");

/**
 * An attribute has a Value, Units and maybe a Threshold or Trend.
 * He also determines which threshold applies to a given attribute (perhaps that should be moved)
 */
var Attribute = function Attribute(_ref) {
  var _ref$attribute = _ref.attribute,
    label = _ref$attribute.label,
    unit = _ref$attribute.unit,
    thresholds = _ref$attribute.thresholds,
    precision = _ref$attribute.precision,
    dataSourceId = _ref$attribute.dataSourceId,
    measurementUnitLabel = _ref$attribute.measurementUnitLabel,
    tooltip = _ref$attribute.tooltip,
    _ref$attribute$thresh = _ref$attribute.thresholdsIconPosition,
    thresholdsIconPosition = _ref$attribute$thresh === void 0 ? 'unit' : _ref$attribute$thresh,
    attributeCount = _ref.attributeCount,
    customFormatter = _ref.customFormatter,
    formatter = _ref.formatter,
    isEditable = _ref.isEditable,
    layout = _ref.layout,
    locale = _ref.locale,
    shouldUseTranslatedLabels = _ref.shouldUseTranslatedLabels,
    i18n = _ref.i18n,
    renderIconByName = _ref.renderIconByName,
    secondaryValue = _ref.secondaryValue,
    value = _ref.value,
    fontSize = _ref.fontSize,
    isNumberValueCompact = _ref.isNumberValueCompact,
    testId = _ref.testId,
    onValueClick = _ref.onValueClick;
  var matchedThreshold = useMatchingThreshold({
    thresholds: thresholds,
    value: value
  });

  // matching threshold will be the first match in the list, or a value of null if not isEditable
  var matchingThreshold = thresholds ? isEditable ? thresholds[0] : matchedThreshold : null;
  var valueColor = matchingThreshold && matchingThreshold.icon === undefined ? matchingThreshold.color : null;

  // need to reduce the width size to fit multiple attributes when card layout is horizontal
  var attributeWidthPercentage = layout === CARD_LAYOUTS.HORIZONTAL ? 100 / attributeCount : 100;

  // Get translated label if shouldUseTranslatedLabels is true
  var displayLabel = getTranslatedLabel(label, shouldUseTranslatedLabels, i18n);
  var displayUnitLabel = getTranslatedLabel(unit, shouldUseTranslatedLabels, i18n);

  // Render threshold icon component
  var renderThresholdIcon = function renderThresholdIcon() {
    if (!(matchingThreshold !== null && matchingThreshold !== void 0 && matchingThreshold.icon)) return null;
    return /*#__PURE__*/React__default.createElement(CardIcon, {
      fill: matchingThreshold.color,
      color: matchingThreshold.color,
      width: 16,
      height: 16,
      title: "".concat(matchingThreshold.comparison, " ").concat(matchingThreshold.value),
      renderIconByName: renderIconByName,
      icon: matchingThreshold.icon,
      testId: "".concat(testId, "-threshold-icon"),
      className: classnames(_defineProperty(_defineProperty({}, "".concat(BEM_BASE, "-threshold-icon"), thresholdsIconPosition === 'unit'), "".concat(BEM_BASE, "-label-icon"), thresholdsIconPosition === 'label'))
    });
  };
  return /*#__PURE__*/React__default.createElement("div", {
    className: classnames("".concat(BEM_BASE, "-wrapper"), _defineProperty(_defineProperty({}, "".concat(BEM_BASE, "-wrapper--vertical"), layout === CARD_LAYOUTS.VERTICAL), "".concat(BEM_BASE, "-wrapper--horizontal"), layout === CARD_LAYOUTS.HORIZONTAL)),
    style: {
      '--value-card-attribute-width': "".concat(attributeWidthPercentage, "%")
    }
  }, /*#__PURE__*/React__default.createElement("div", {
    className: classnames("".concat(BEM_BASE, "-label"), _defineProperty({}, "".concat(BEM_BASE, "-label--with-icon"), thresholdsIconPosition === 'label' && (matchingThreshold === null || matchingThreshold === void 0 ? void 0 : matchingThreshold.icon)))
  }, thresholdsIconPosition === 'label' && renderThresholdIcon(), tooltip ? /*#__PURE__*/React__default.createElement(Tooltip, {
    direction: "right",
    showIcon: false,
    triggerText: displayLabel
  }, /*#__PURE__*/React__default.createElement("p", null, tooltip)) : /*#__PURE__*/React__default.createElement("span", {
    "data-testid": "".concat(testId, "-threshold-label")
  }, displayLabel)), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(BEM_BASE)
  }, /*#__PURE__*/React__default.createElement(ValueRenderer, {
    value: value,
    layout: layout,
    precision: precision,
    color: valueColor,
    locale: locale
    // Prefer new formatter. Fall back to legacy customFormatter.
    ,
    formatter: formatter,
    customFormatter: customFormatter,
    fontSize: fontSize,
    isNumberValueCompact: isNumberValueCompact,
    testId: "".concat(testId, "-value"),
    dataSourceId: dataSourceId,
    onClick: onValueClick
  }), /*#__PURE__*/React__default.createElement("div", {
    className: "".concat(BEM_BASE, "-unit-with-icon")
  }, thresholdsIconPosition === 'unit' && renderThresholdIcon(), /*#__PURE__*/React__default.createElement(UnitRenderer, {
    unit: measurementUnitLabel || displayUnitLabel,
    testId: "".concat(testId, "-unit")
  }))), !isNil(secondaryValue) ? /*#__PURE__*/React__default.createElement("div", {
    "data-testid": "".concat(testId, "-secondary-value"),
    className: "".concat(BEM_BASE, "-secondary-value"),
    style: {
      '--secondary-value-color': gray60
    }
  }, secondaryValue.trend === 'up' ? /*#__PURE__*/React__default.createElement(CaretUp, {
    className: "".concat(BEM_BASE, "_trend-icon"),
    "aria-label": "trending up",
    "data-testid": "".concat(testId, "-trending-up"),
    fill: secondaryValue.color || gray60
  }) : secondaryValue.trend === 'down' ? /*#__PURE__*/React__default.createElement(CaretDown, {
    className: "".concat(BEM_BASE, "_trend-icon"),
    "aria-label": "trending down",
    "data-testid": "".concat(testId, "-trending-down"),
    fill: secondaryValue.color || gray60
  }) : null, secondaryValue.href || secondaryValue.onClick ? /*#__PURE__*/React__default.createElement(Link, {
    "data-testid": "".concat(testId, "-secondary-value--link"),
    className: "".concat(BEM_BASE, "-secondary-value--link"),
    href: secondaryValue === null || secondaryValue === void 0 ? void 0 : secondaryValue.href,
    rel: "noopener noreferrer",
    onClick: function onClick() {
      return secondaryValue === null || secondaryValue === void 0 ? void 0 : secondaryValue.onClick({
        dataSourceId: dataSourceId,
        secondaryValue: secondaryValue
      });
    }
  }, secondaryValue.value) : secondaryValue.value) : null);
};
Attribute.propTypes = propTypes;
Attribute.defaultProps = defaultProps;
Attribute.__docgenInfo = {
  "description": "An attribute has a Value, Units and maybe a Threshold or Trend.\nHe also determines which threshold applies to a given attribute (perhaps that should be moved)",
  "methods": [],
  "displayName": "Attribute",
  "props": {
    "layout": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "enum",
        "computed": true,
        "value": "Object.values(CARD_LAYOUTS)"
      },
      "required": false
    },
    "value": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "union",
        "value": [{
          "name": "string"
        }, {
          "name": "bool"
        }, {
          "name": "number"
        }]
      },
      "required": false
    },
    "renderIconByName": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "secondaryValue": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Optional trend information",
      "type": {
        "name": "shape",
        "value": {
          "color": {
            "name": "string",
            "required": false
          },
          "trend": {
            "name": "enum",
            "value": [{
              "value": "'up'",
              "computed": false
            }, {
              "value": "'down'",
              "computed": false
            }],
            "required": false
          },
          "value": {
            "name": "union",
            "value": [{
              "name": "string"
            }, {
              "name": "bool"
            }, {
              "name": "number"
            }],
            "required": false
          },
          "href": {
            "name": "string",
            "required": false
          },
          "onClick": {
            "name": "func",
            "required": false
          }
        }
      },
      "required": false
    },
    "locale": {
      "defaultValue": {
        "value": "'en'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "customFormatter": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "formatter": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "isEditable": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "shouldUseTranslatedLabels": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "whether to use translated labels in cards",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "i18n": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "objectOf",
        "value": {
          "name": "any"
        }
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'attribute'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "onValueClick": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "callback when the attribute is clicked to trigger further action like opening a modal",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "attribute": {
      "description": "",
      "type": {
        "name": "shape",
        "value": {
          "label": {
            "name": "string",
            "required": false
          },
          "unit": {
            "name": "string",
            "required": false
          },
          "dataSourceId": {
            "name": "string",
            "required": false
          },
          "measurementUnitLabel": {
            "name": "string",
            "required": false
          },
          "precision": {
            "name": "number",
            "required": false
          },
          "thresholds": {
            "name": "arrayOf",
            "value": {
              "name": "shape",
              "value": {
                "comparison": {
                  "name": "enum",
                  "value": [{
                    "value": "'<'",
                    "computed": false
                  }, {
                    "value": "'>'",
                    "computed": false
                  }, {
                    "value": "'='",
                    "computed": false
                  }, {
                    "value": "'<='",
                    "computed": false
                  }, {
                    "value": "'>='",
                    "computed": false
                  }],
                  "required": true
                },
                "value": {
                  "name": "union",
                  "value": [{
                    "name": "string"
                  }, {
                    "name": "number"
                  }],
                  "required": true
                },
                "color": {
                  "name": "string",
                  "required": false
                },
                "icon": {
                  "name": "string",
                  "required": false
                }
              }
            },
            "required": false
          },
          "tooltip": {
            "name": "string",
            "required": false
          },
          "thresholdsIconPosition": {
            "name": "enum",
            "value": [{
              "value": "'label'",
              "computed": false
            }, {
              "value": "'unit'",
              "computed": false
            }],
            "required": false
          }
        }
      },
      "required": true
    },
    "fontSize": {
      "description": "",
      "type": {
        "name": "number"
      },
      "required": true
    },
    "isNumberValueCompact": {
      "description": "optional option to determine whether the number should be abbreviated (i.e. 10,000 = 10K)",
      "type": {
        "name": "bool"
      },
      "required": true
    },
    "attributeCount": {
      "description": "number of attributes",
      "type": {
        "name": "number"
      },
      "required": true
    }
  }
};
var Attribute$1 = Attribute;

export { Attribute$1 as default };
