"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
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 CentralizedContainer = _styledComponents.default.div`
  display: flex;
  justify-content: center;
  align-items: center;
`;
function TutorialFleetInstructions({
  http,
  basePath,
  isDarkTheme,
  kibanaVersion
}) {
  const [data, setData] = (0, _react.useState)();
  const [isLoading, setIsLoading] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    async function fetchData() {
      setIsLoading(true);
      try {
        const response = await http.get('/internal/apm/fleet/migration_check');
        setData(response);
      } catch (e) {
        setIsLoading(false);
        console.error('Error while fetching fleet details.', e);
      }
      setIsLoading(false);
    }
    fetchData();
  }, [http]);
  const hasApmIntegrations = !!(data !== null && data !== void 0 && data.has_apm_integrations);
  const cloudApmMigrationEnabled = !!(data !== null && data !== void 0 && data.cloud_apm_migration_enabled);
  const hasCloudAgentPolicy = !!(data !== null && data !== void 0 && data.has_cloud_agent_policy);
  const cloudApmPackagePolicy = data === null || data === void 0 ? void 0 : data.cloud_apm_package_policy;
  const hasCloudApmPackagePolicy = !!cloudApmPackagePolicy;
  const hasRequiredRole = !!(data !== null && data !== void 0 && data.has_required_role);
  const shouldLinkToMigration = cloudApmMigrationEnabled && hasCloudAgentPolicy && !hasCloudApmPackagePolicy && hasRequiredRole;
  const apmIntegrationHref = shouldLinkToMigration ? `${basePath}/app/apm/settings/schema` : `${basePath}/app/integrations#/detail/apm/overview`;
  if (isLoading) {
    return /*#__PURE__*/_react.default.createElement(CentralizedContainer, null, /*#__PURE__*/_react.default.createElement(_eui.EuiLoadingSpinner, null));
  }

  // When APM integration is enable in Fleet
  if (hasApmIntegrations) {
    return /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
      "data-test-subj": "apmTutorialFleetInstructionsManageApmIntegrationInFleetButton",
      iconType: "gear",
      fill: true,
      href: `${basePath}/app/integrations/detail/apm/policies`
    }, _i18n.i18n.translate('xpack.apm.tutorial.apmServer.fleet.manageApmIntegration.button', {
      defaultMessage: 'Manage APM integration in Fleet'
    }));
  }
  // When APM integration is not installed in Fleet or for some reason the API didn't work out
  return /*#__PURE__*/_react.default.createElement(_eui.EuiPanel, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: 7
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiCard, {
    display: "plain",
    textAlign: "left",
    title: _i18n.i18n.translate('xpack.apm.tutorial.apmServer.fleet.title', {
      defaultMessage: 'Elastic APM now available in Fleet!'
    }),
    description: _i18n.i18n.translate('xpack.apm.tutorial.apmServer.fleet.message', {
      defaultMessage: 'The APM integration installs Elasticsearch templates and ingest pipelines for APM data.'
    }),
    footer: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
      "data-test-subj": "apmTutorialFleetInstructionsApmIntegrationButton",
      iconType: "analyzeEvent",
      color: "success",
      href: apmIntegrationHref
    }, _i18n.i18n.translate('xpack.apm.tutorial.apmServer.fleet.apmIntegration.button', {
      defaultMessage: 'APM integration'
    })), /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, {
      size: "m"
    }), /*#__PURE__*/_react.default.createElement(_eui.EuiText, {
      size: "s"
    }, /*#__PURE__*/_react.default.createElement("p", null, _i18n.i18n.translate('xpack.apm.tutorial.apmServer.fleet.apmIntegration.description', {
      defaultMessage: 'Fleet allows you to centrally manage Elastic Agents running the APM integration. The default option is to install a Fleet Server on a dedicated host. For setups without a dedicated host, we recommend following the instructions to install the standalone APM Server for your operating system by selecting the respective tab above.'
    }))))
  })), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
    grow: 3
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiImage, {
    src: `${basePath}/plugins/kibanaReact/assets/${isDarkTheme ? 'illustration_integrations_darkmode.svg' : 'illustration_integrations_lightmode.svg'}`,
    alt: "Illustration"
  }))));
}
// eslint-disable-next-line import/no-default-export
var _default = TutorialFleetInstructions;
exports.default = _default;
module.exports = exports.default;