"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.InferencePipelineCard = void 0;
var _react = _interopRequireWildcard(require("react"));
var _kea = require("kea");
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _pipelines = require("../../../../../../common/types/pipelines");
var _constants = require("../../../../shared/constants");
var _http = require("../../../../shared/http");
var _routes = require("../../../routes");
var _utils = require("../../shared/ml_inference/utils");
var _index_name_logic = require("../index_name_logic");
var _index_view_logic = require("../index_view_logic");
var _delete_inference_pipeline_button = require("./delete_inference_pipeline_button");
var _ml_model_health = require("./ml_model_health");
var _ml_model_type_badge = require("./ml_model_type_badge");
var _pipelines_logic = require("./pipelines_logic");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

const InferencePipelineCard = pipeline => {
  const {
    http
  } = (0, _kea.useValues)(_http.HttpLogic);
  const {
    indexName
  } = (0, _kea.useValues)(_index_name_logic.IndexNameLogic);
  const {
    ingestionMethod
  } = (0, _kea.useValues)(_index_view_logic.IndexViewLogic);
  const [isPopOverOpen, setIsPopOverOpen] = (0, _react.useState)(false);
  const [showConfirmDelete, setShowConfirmDelete] = (0, _react.useState)(false);
  const {
    deleteMlPipeline,
    detachMlPipeline
  } = (0, _kea.useActions)(_pipelines_logic.PipelinesLogic);
  const showConfirmDeleteModal = () => {
    setShowConfirmDelete(true);
    setIsPopOverOpen(false);
  };
  const {
    pipelineName,
    types: modelTypes
  } = pipeline;
  const modelType = (0, _utils.getMLType)(modelTypes);
  const modelTitle = (0, _utils.getModelDisplayTitle)(modelType);
  const actionButton = /*#__PURE__*/_react.default.createElement(_eui.EuiButtonEmpty, {
    iconSide: "right",
    flush: "both",
    iconType: "boxesVertical",
    onClick: () => setIsPopOverOpen(!isPopOverOpen)
  }, _i18n.i18n.translate('xpack.enterpriseSearch.inferencePipelineCard.actionButton', {
    defaultMessage: 'Actions'
  }));
  return /*#__PURE__*/_react.default.createElement(_eui.EuiPanel, {
    color: "subdued"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    direction: "column",
    gutterSize: "xs"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    alignItems: "center"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiTitle, {
    size: "xs"
  }, /*#__PURE__*/_react.default.createElement("h4", null, modelTitle !== null && modelTitle !== void 0 ? modelTitle : pipelineName))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiPopover, {
    button: actionButton,
    isOpen: isPopOverOpen,
    closePopover: () => setIsPopOverOpen(false)
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiPopoverTitle, {
    paddingSize: "m"
  }, _i18n.i18n.translate('xpack.enterpriseSearch.inferencePipelineCard.action.title', {
    defaultMessage: 'Actions'
  })), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    direction: "column",
    gutterSize: "none"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_eui.EuiButtonEmpty, {
    "data-telemetry-id": `entSearchContent-${ingestionMethod}-pipelines-inferencePipeline-stackManagement`,
    size: "s",
    flush: "both",
    iconType: "eye",
    color: "text",
    href: http.basePath.prepend(`/app/management/ingest/ingest_pipelines/?pipeline=${pipelineName}`)
  }, _i18n.i18n.translate('xpack.enterpriseSearch.inferencePipelineCard.action.view', {
    defaultMessage: 'View in Stack Management'
  })))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_eui.EuiButtonEmpty, {
    "data-telemetry-id": `entSearchContent-${ingestionMethod}-pipelines-inferencePipeline-detachPipeline`,
    size: "s",
    flush: "both",
    iconType: "unlink",
    color: "text",
    onClick: () => {
      detachMlPipeline({
        indexName,
        pipelineName
      });
      setIsPopOverOpen(false);
    }
  }, _i18n.i18n.translate('xpack.enterpriseSearch.inferencePipelineCard.action.detach', {
    defaultMessage: 'Detach pipeline'
  })))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_delete_inference_pipeline_button.DeleteInferencePipelineButton, {
    "data-telemetry-id": `entSearchContent-${ingestionMethod}-pipelines-inferencePipeline-deletePipeline`,
    onClick: showConfirmDeleteModal,
    pipeline: pipeline
  })))))))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    gutterSize: "s",
    alignItems: "center",
    justifyContent: "flexEnd"
  }, modelTitle && /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiTextColor, {
    color: "subdued"
  }, pipelineName)), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_ml_model_health.TrainedModelHealth, {
    modelState: pipeline.modelState,
    modelStateReason: pipeline.modelStateReason
  })), pipeline.modelState === _pipelines.TrainedModelState.NotDeployed && /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false,
    style: {
      paddingRight: '1rem'
    }
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiToolTip, {
    position: "top",
    content: _i18n.i18n.translate('xpack.enterpriseSearch.inferencePipelineCard.modelState.notDeployed.fixLink', {
      defaultMessage: 'Fix issue in Trained Models'
    })
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiButtonIcon, {
    "aria-label": _i18n.i18n.translate('xpack.enterpriseSearch.inferencePipelineCard.modelState.notDeployed.fixLink', {
      defaultMessage: 'Fix issue in Trained Models'
    }),
    "data-telemetry-id": `entSearchContent-${ingestionMethod}-pipelines-inferencePipeline-fixIssueInTrainedModels`,
    href: http.basePath.prepend(_routes.ML_MANAGE_TRAINED_MODELS_PATH),
    display: "base",
    size: "xs",
    iconType: "wrench"
  }))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: false
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    gutterSize: "xs"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement("span", null, /*#__PURE__*/_react.default.createElement(_ml_model_type_badge.MLModelTypeBadge, {
    type: modelType
  })))))))))), showConfirmDelete && /*#__PURE__*/_react.default.createElement(_eui.EuiConfirmModal, {
    onCancel: () => setShowConfirmDelete(false),
    onConfirm: () => {
      setShowConfirmDelete(false);
      deleteMlPipeline({
        indexName,
        pipelineName
      });
    },
    title: _i18n.i18n.translate('xpack.enterpriseSearch.inferencePipelineCard.deleteConfirm.title', {
      defaultMessage: 'Delete Pipeline'
    }),
    buttonColor: "danger",
    cancelButtonText: _constants.CANCEL_BUTTON_LABEL,
    confirmButtonText: _constants.DELETE_BUTTON_LABEL,
    defaultFocusedButton: "confirm",
    maxWidth: true
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiText, null, /*#__PURE__*/_react.default.createElement("p", null, _i18n.i18n.translate('xpack.enterpriseSearch.inferencePipelineCard.deleteConfirm.description', {
    defaultMessage: 'You are removing the pipeline "{pipelineName}" from the Machine Learning Inference Pipeline and deleting it.',
    values: {
      pipelineName
    }
  })))));
};
exports.InferencePipelineCard = InferencePipelineCard;