{"version":3,"file":"AggregateChip.mjs","names":["React","useEffect","useMemo","useState","PropTypes","StatusChip","useAxiosGet","Typography","CircularProgress","executeQueryInstance","getThresholdStatus","AlertPill","SeverityTypeEnums","ConfigService","AggregateChip","props","aggregateThresholdId","aggregateThresholdParamMappings","label","chipColor","setStatus","isActionable","isAlert","hideAlert","onAlertClick","color","setColor","aggregateValue","setAggregateValue","loading","setLoading","data","aggregateThreshold","loadingAggregateThreshold","integrationV2ApiUrl","alertDisplayLabel","replace","setAggregateData","apiMethodInstanceId","valueKey","response","catch","res","status","aggregateResult","aggregateResults","Math","round","aggregateKey","fontSize","Success","Error","Warning","Info","propTypes","oneOf","undefined","string"],"sources":["../../../src/UI/utilityDisplay/AggregateChip.jsx"],"sourcesContent":["import React, { useEffect, useMemo, useState } from 'react';\nimport PropTypes from 'prop-types';\nimport { StatusChip } from '../dataDisplay/status/StatusChip';\nimport { useAxiosGet } from '../../utilities/useAxiosGet';\nimport { Typography, CircularProgress } from '@material-ui/core';\nimport { executeQueryInstance } from '../../udp/utilities/methodUtils';\nimport { getThresholdStatus } from './utilities/aggregateUtils';\nimport { AlertPill } from '../dataDisplay/entityHeader/ui/AlertPill';\nimport { SeverityTypeEnums } from '../../enums/unitySystemEnums';\nimport { ConfigService } from '../../configService';\n\nexport const AggregateChip = (props) => {\n  const {\n    aggregateThresholdId,\n    aggregateThresholdParamMappings,\n    label,\n    chipColor,\n    setStatus,\n    isActionable = false,\n    isAlert = false,\n    hideAlert = false,\n    onAlertClick\n  } = props;\n  const [color, setColor] = useState(chipColor);\n  const [aggregateValue, setAggregateValue] = useState(null);\n  const [loading, setLoading] = useState(false);\n  const [{ data: aggregateThreshold, loading: loadingAggregateThreshold }] =\n    useAxiosGet(\n      ConfigService.integrationV2ApiUrl,\n      `aggregateThreshold/${aggregateThresholdId}`,\n      {},\n      !!!aggregateThresholdId\n    );\n\n  const alertDisplayLabel = useMemo(() => {\n    if (!label) {\n      return aggregateValue;\n    }\n    return `${label.replace('{value}', aggregateValue)}`;\n  }, [aggregateValue, label]);\n\n  useEffect(() => {\n    const setAggregateData = async (apiMethodInstanceId, valueKey) => {\n      setLoading(true);\n      const data = aggregateThresholdParamMappings\n        ? { data: aggregateThresholdParamMappings }\n        : { data: {} };\n      const response = await executeQueryInstance(\n        data,\n        apiMethodInstanceId\n      ).catch((res) => setLoading(false));\n      if (response?.status === 200 && valueKey) {\n        const aggregateResult =\n          response.data?.aggregateResults && response.data?.aggregateResults[0];\n        if (aggregateResult) {\n          setAggregateValue(\n            aggregateResult[valueKey]\n              ? Math.round(aggregateResult[valueKey])\n              : ''\n          );\n        }\n      }\n      setLoading(false);\n    };\n    if (aggregateThreshold) {\n      setAggregateData(\n        aggregateThreshold.apiMethodInstanceId,\n        aggregateThreshold.aggregateKey\n      );\n    }\n    // when aggregateThresholdParamMappings changes we don't want to re-render chip\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [aggregateThreshold]);\n\n  useEffect(() => {\n    if (aggregateThreshold && aggregateValue) {\n      const status = getThresholdStatus(aggregateThreshold, aggregateValue);\n      setColor(status);\n      if (setStatus) {\n        setStatus(status || 'default');\n      }\n    }\n  }, [aggregateThreshold, aggregateValue, setStatus]);\n\n  if (loading || loadingAggregateThreshold) {\n    return <CircularProgress size={18} />;\n  }\n\n  if (!color && !isAlert) {\n    return (\n      <Typography variant='caption2' style={{ fontSize: '0.7rem' }}>\n        {label || aggregateValue}\n      </Typography>\n    );\n  }\n\n  if (isAlert) {\n    return (\n      <AlertPill\n        handleClick={onAlertClick}\n        successCount={color === SeverityTypeEnums.Success ? aggregateValue : ''}\n        errorCount={color === SeverityTypeEnums.Error ? aggregateValue : ''}\n        warningCount={color === SeverityTypeEnums.Warning ? aggregateValue : ''}\n        infoCount={color === SeverityTypeEnums.Info ? aggregateValue : ''}\n        defaultCount={!color && (aggregateValue || '')}\n        successLabel={\n          color === SeverityTypeEnums.Success ? alertDisplayLabel : ''\n        }\n        errorLabel={color === SeverityTypeEnums.Error ? alertDisplayLabel : ''}\n        warningLabel={\n          color === SeverityTypeEnums.Warning ? alertDisplayLabel : ''\n        }\n        infoLabel={color === SeverityTypeEnums.Info ? alertDisplayLabel : ''}\n        defaultLabel={!color && (alertDisplayLabel || '')}\n        hideSuccess={color === SeverityTypeEnums.Success ? hideAlert : false}\n        hideError={color === SeverityTypeEnums.Error ? hideAlert : false}\n        hideWarning={color === SeverityTypeEnums.Warning ? hideAlert : false}\n        hideInfo={color === SeverityTypeEnums.Info ? hideAlert : false}\n        hideDefault={!color ? hideAlert : false}\n      />\n    );\n  }\n\n  return label || aggregateValue ? (\n    <StatusChip\n      label={label || aggregateValue}\n      status={color}\n      useSmallChip={true}\n      isActionable={isActionable}\n    />\n  ) : null;\n};\n\nAggregateChip.propTypes = {\n  /**\n   * Color of chip\n   */\n  chipColor: PropTypes.oneOf([\n    'success',\n    'error',\n    'warning',\n    'info',\n    undefined,\n    null\n  ]),\n  /**\n   * Id of AggregateThreshold\n   */\n  aggregateThresholdId: PropTypes.oneOf([PropTypes.string, undefined, null])\n};\n"],"mappings":";;;;;;;;;;;AAWA,MAAac,iBAAiBC,UAAU;CACtC,MAAM,EACJC,sBACAC,iCACAC,OACAC,WACAC,WACAC,eAAe,OACfC,UAAU,OACVC,YAAY,OACZC,iBACET;CACJ,MAAM,CAACU,OAAOC,YAAYvB,SAASgB,UAAU;CAC7C,MAAM,CAACQ,gBAAgBC,qBAAqBzB,SAAS,KAAK;CAC1D,MAAM,CAAC0B,SAASC,cAAc3B,SAAS,MAAM;CAC7C,MAAM,CAAC,EAAE4B,MAAMC,oBAAoBH,SAASI,+BAC1C3B,YACEO,cAAcqB,qBACd,sBAAsBlB,wBACtB,EAAE,EACF,CAAC,CAAC,CAACA,qBACJ;CAEH,MAAMmB,oBAAoBjC,cAAc;AACtC,MAAI,CAACgB,MACH,QAAOS;AAET,SAAO,GAAGT,MAAMkB,QAAQ,WAAWT,eAAe;IACjD,CAACA,gBAAgBT,MAAM,CAAC;AAE3Bj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