import { ConfigService } from "../../configService/index.mjs";
import { SeverityTypeEnums } from "../../enums/unitySystemEnums.mjs";
import { useAxiosGet } from "../../utilities/useAxiosGet.mjs";
import { StatusChip } from "../dataDisplay/status/StatusChip.mjs";
import { executeQueryInstance } from "../../udp/utilities/methodUtils.mjs";
import { getThresholdStatus } from "./utilities/aggregateUtils.mjs";
import { AlertPill } from "../dataDisplay/entityHeader/ui/AlertPill.mjs";
import React, { useEffect, useMemo, useState } from "react";
import { CircularProgress, Typography } from "@material-ui/core";
import PropTypes from "prop-types";
//#region src/UI/utilityDisplay/AggregateChip.jsx
const AggregateChip = (props) => {
	const { aggregateThresholdId, aggregateThresholdParamMappings, label, chipColor, setStatus, isActionable = false, isAlert = false, hideAlert = false, onAlertClick } = props;
	const [color, setColor] = useState(chipColor);
	const [aggregateValue, setAggregateValue] = useState(null);
	const [loading, setLoading] = useState(false);
	const [{ data: aggregateThreshold, loading: loadingAggregateThreshold }] = useAxiosGet(ConfigService.integrationV2ApiUrl, `aggregateThreshold/${aggregateThresholdId}`, {}, !!!aggregateThresholdId);
	const alertDisplayLabel = useMemo(() => {
		if (!label) return aggregateValue;
		return `${label.replace("{value}", aggregateValue)}`;
	}, [aggregateValue, label]);
	useEffect(() => {
		const setAggregateData = async (apiMethodInstanceId, valueKey) => {
			setLoading(true);
			const response = await executeQueryInstance(aggregateThresholdParamMappings ? { data: aggregateThresholdParamMappings } : { data: {} }, apiMethodInstanceId).catch((res) => setLoading(false));
			if (response?.status === 200 && valueKey) {
				const aggregateResult = response.data?.aggregateResults && response.data?.aggregateResults[0];
				if (aggregateResult) setAggregateValue(aggregateResult[valueKey] ? Math.round(aggregateResult[valueKey]) : "");
			}
			setLoading(false);
		};
		if (aggregateThreshold) setAggregateData(aggregateThreshold.apiMethodInstanceId, aggregateThreshold.aggregateKey);
	}, [aggregateThreshold]);
	useEffect(() => {
		if (aggregateThreshold && aggregateValue) {
			const status = getThresholdStatus(aggregateThreshold, aggregateValue);
			setColor(status);
			if (setStatus) setStatus(status || "default");
		}
	}, [
		aggregateThreshold,
		aggregateValue,
		setStatus
	]);
	if (loading || loadingAggregateThreshold) return /* @__PURE__ */ React.createElement(CircularProgress, { size: 18 });
	if (!color && !isAlert) return /* @__PURE__ */ React.createElement(Typography, {
		variant: "caption2",
		style: { fontSize: "0.7rem" }
	}, label || aggregateValue);
	if (isAlert) return /* @__PURE__ */ React.createElement(AlertPill, {
		handleClick: onAlertClick,
		successCount: color === SeverityTypeEnums.Success ? aggregateValue : "",
		errorCount: color === SeverityTypeEnums.Error ? aggregateValue : "",
		warningCount: color === SeverityTypeEnums.Warning ? aggregateValue : "",
		infoCount: color === SeverityTypeEnums.Info ? aggregateValue : "",
		defaultCount: !color && (aggregateValue || ""),
		successLabel: color === SeverityTypeEnums.Success ? alertDisplayLabel : "",
		errorLabel: color === SeverityTypeEnums.Error ? alertDisplayLabel : "",
		warningLabel: color === SeverityTypeEnums.Warning ? alertDisplayLabel : "",
		infoLabel: color === SeverityTypeEnums.Info ? alertDisplayLabel : "",
		defaultLabel: !color && (alertDisplayLabel || ""),
		hideSuccess: color === SeverityTypeEnums.Success ? hideAlert : false,
		hideError: color === SeverityTypeEnums.Error ? hideAlert : false,
		hideWarning: color === SeverityTypeEnums.Warning ? hideAlert : false,
		hideInfo: color === SeverityTypeEnums.Info ? hideAlert : false,
		hideDefault: !color ? hideAlert : false
	});
	return label || aggregateValue ? /* @__PURE__ */ React.createElement(StatusChip, {
		label: label || aggregateValue,
		status: color,
		useSmallChip: true,
		isActionable
	}) : null;
};
AggregateChip.propTypes = {
	chipColor: PropTypes.oneOf([
		"success",
		"error",
		"warning",
		"info",
		void 0,
		null
	]),
	aggregateThresholdId: PropTypes.oneOf([
		PropTypes.string,
		void 0,
		null
	])
};
//#endregion
export { AggregateChip };

//# sourceMappingURL=AggregateChip.mjs.map