function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */

import React, { useCallback } from 'react';
import { useCelestialNodeActionsContext } from '../../shared/contexts/node_actions_context';
import { DEFAULT_METRICS } from '../../shared/constants/common.constants';
import { formatCount } from '../../shared/utils/format_count';
import { HealthDonut } from '../health_donut';
import { NodeShell } from './node_shell';
import { TypeBadge } from './type_badge';
import { MetricBar } from './metric_bar';
import { StatusIndicator } from './status_indicator';
import { t } from '../../shared/i18n/t';
import './service_card_node.scss';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * Modernized APM service card node with type badge, title/subtitle,
 * optional donut chart, metric bar, and configurable action button.
 */
export const ServiceCardNode = _ref => {
  var _data$health, _data$metrics, _data$metrics2, _data$metrics$errors, _data$health2, _data$health3, _data$health4, _actionButton$onClick, _actionButton$label, _actionButton$label2;
  let {
    data
  } = _ref;
  const {
    onDashboardClick,
    selectedNodeId
  } = useCelestialNodeActionsContext();
  const isSelected = data.id === selectedNodeId;
  const isBreach = !!((_data$health = data.health) !== null && _data$health !== void 0 && _data$health.breached);
  const totalRequests = formatCount((_data$metrics = data.metrics) === null || _data$metrics === void 0 ? void 0 : _data$metrics.requests);
  const errorRate = (_data$metrics2 = data.metrics) !== null && _data$metrics2 !== void 0 && _data$metrics2.requests ? ((_data$metrics$errors = data.metrics.errors4xx) !== null && _data$metrics$errors !== void 0 ? _data$metrics$errors : 0) / data.metrics.requests * 100 : 0;
  const sliStatus = ((_data$health2 = data.health) === null || _data$health2 === void 0 ? void 0 : _data$health2.status) === 'breached' ? 'error' : ((_data$health3 = data.health) === null || _data$health3 === void 0 ? void 0 : _data$health3.status) === 'recovered' ? 'warning' : undefined;
  const onViewDashboardClick = useCallback(event => {
    onDashboardClick === null || onDashboardClick === void 0 || onDashboardClick(event, data);
  }, [onDashboardClick, data]);
  const actionButton = data.actionButton;
  const showDonut = data.showDonut !== false;
  return /*#__PURE__*/_jsx(NodeShell, {
    borderColor: isBreach ? 'var(--osd-color-status-breached)' : data.color,
    backgroundColor: isBreach ? 'var(--osd-color-container-breached)' : undefined,
    glowColor: isBreach ? 'var(--osd-color-status-breached)' : data.color,
    isSelected: isSelected,
    isFaded: !!data.isFaded,
    className: "osd:bg-container-default osd:w-68 osd:min-h-24 osd:p-3",
    "data-test-subj": "serviceCardNode-".concat(data.id),
    children: /*#__PURE__*/_jsxs("div", {
      className: "osd:flex osd:flex-col osd:gap-2",
      children: [/*#__PURE__*/_jsxs("div", {
        className: "osd:flex osd:items-center osd:justify-between",
        children: [data.typeBadge && data.typeBadge !== false && /*#__PURE__*/_jsx(TypeBadge, {
          label: data.typeBadge.label,
          color: data.typeBadge.color,
          icon: data.typeBadge.icon,
          textColor: data.typeBadge.textColor
        }), sliStatus && /*#__PURE__*/_jsx(StatusIndicator, {
          status: sliStatus,
          label: ((_data$health4 = data.health) === null || _data$health4 === void 0 ? void 0 : _data$health4.status) === 'breached' ? t('node.sliStatus.breached') : t('node.sliStatus.recovered')
        })]
      }), /*#__PURE__*/_jsxs("div", {
        className: "osd:flex osd:items-start osd:justify-between osd:gap-2",
        children: [/*#__PURE__*/_jsxs("div", {
          className: "osd:min-w-0",
          children: [/*#__PURE__*/_jsx("div", {
            className: "osd:font-bold osd:text-sm osd:text-body-default osd:truncate",
            children: data.title
          }), data.subtitle && /*#__PURE__*/_jsx("div", {
            className: "osd:text-xs osd:text-body-secondary osd:truncate",
            children: data.subtitle
          })]
        }), actionButton !== false && /*#__PURE__*/_jsx("button", {
          className: "osd-resetFocusState osd:text-link-default osd:hover:text-link-hover osd:transition-colors osd:cursor-pointer osd:bg-transparent osd:border-0 osd:p-0 osd:text-xs osd:whitespace-nowrap osd:flex-shrink-0",
          onClick: (_actionButton$onClick = actionButton === null || actionButton === void 0 ? void 0 : actionButton.onClick) !== null && _actionButton$onClick !== void 0 ? _actionButton$onClick : onViewDashboardClick,
          "aria-label": "".concat((_actionButton$label = actionButton === null || actionButton === void 0 ? void 0 : actionButton.label) !== null && _actionButton$label !== void 0 ? _actionButton$label : t('buttons.viewInsights'), " for ").concat(data.title),
          "data-test-subj": "serviceCardNode-viewInsights-".concat(data.id),
          children: (_actionButton$label2 = actionButton === null || actionButton === void 0 ? void 0 : actionButton.label) !== null && _actionButton$label2 !== void 0 ? _actionButton$label2 : t('buttons.viewInsights')
        })]
      }), /*#__PURE__*/_jsxs("div", {
        className: "osd:flex osd:items-center osd:gap-3",
        children: [showDonut && data.icon && /*#__PURE__*/_jsx("div", {
          className: "osd:flex-shrink-0",
          children: /*#__PURE__*/_jsx(HealthDonut, {
            metrics: data.metrics || _objectSpread({}, DEFAULT_METRICS),
            size: 48,
            icon: data.icon,
            isLegendEnabled: true
          })
        }), /*#__PURE__*/_jsxs("div", {
          className: "osd:flex-1 osd:flex osd:flex-col osd:gap-1",
          children: [/*#__PURE__*/_jsx("div", {
            className: "osd:text-xs osd:text-body-secondary",
            children: t('node.requestsAbbreviated', {
              value: totalRequests
            })
          }), /*#__PURE__*/_jsx(MetricBar, {
            value: errorRate,
            max: 100,
            color: "var(--osd-color-ok)",
            label: "".concat(errorRate.toFixed(1), "%")
          })]
        })]
      })]
    })
  });
};