"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AgentPolicySelection = void 0;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _i18n = require("@kbn/i18n");
var _i18nReact = require("@kbn/i18n-react");
var _eui = require("@elastic/eui");
var _agent_policy_package_badges = require("../agent_policy_package_badges");
var _hooks = require("../../hooks");
var _advanced_agent_authentication_settings = require("./advanced_agent_authentication_settings");
/*
 * 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 AgentPolicyFormRow = (0, _styledComponents.default)(_eui.EuiFormRow)`
  .euiFormRow__label {
    width: 100%;
  }
`;
const AgentPolicySelection = props => {
  const {
    docLinks
  } = (0, _hooks.useStartServices)();
  const {
    agentPolicies,
    selectedPolicyId,
    setSelectedPolicyId,
    excludeFleetServer,
    onClickCreatePolicy,
    isFleetServerPolicy
  } = props;
  const hasFleetAllPrivileges = (0, _hooks.useAuthz)().fleet.all;
  const onChangeCallback = event => {
    const {
      value
    } = event.target;
    setSelectedPolicyId(value);
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiText, null, isFleetServerPolicy ? /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
    id: "xpack.fleet.enrollmentStepAgentPolicy.selectAgentPolicyFleetServerText",
    defaultMessage: "Fleet Server runs on Elastic Agent, and agents are enrolled in agent policies which represent hosts. You can select an existing agent policy configured for Fleet Server, or you may choose to create a new one."
  }) : /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
    id: "xpack.fleet.enrollmentStepAgentPolicy.createAgentPolicyText",
    defaultMessage: "Type of hosts are controlled by an {agentPolicy}. Choose an agent policy or create a new one.",
    values: {
      agentPolicy: /*#__PURE__*/_react.default.createElement(_eui.EuiLink, {
        href: docLinks.links.fleet.agentPolicy,
        target: "_blank"
      }, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
        id: "xpack.fleet.agentPolicyForm.createAgentPolicyDocLink",
        defaultMessage: "agent policy"
      }))
    }
  })), /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, {
    size: "m"
  }), /*#__PURE__*/_react.default.createElement(AgentPolicyFormRow, {
    fullWidth: true,
    label: /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
      justifyContent: "flexEnd"
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
      grow: false
    }, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_eui.EuiLink, {
      disabled: !hasFleetAllPrivileges,
      onClick: onClickCreatePolicy
    }, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
      id: "xpack.fleet.enrollmentStepAgentPolicy.addPolicyButton",
      defaultMessage: "Create new agent policy"
    })))))
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiSelect, {
    fullWidth: true,
    isLoading: !agentPolicies,
    options: agentPolicies.map(agentPolicy => ({
      value: agentPolicy.id,
      text: agentPolicy.name
    })),
    value: selectedPolicyId,
    onChange: onChangeCallback,
    "aria-label": _i18n.i18n.translate('xpack.fleet.enrollmentStepAgentPolicy.policySelectAriaLabel', {
      defaultMessage: 'Agent policy'
    }),
    hasNoInitialSelection: !selectedPolicyId,
    "data-test-subj": "agentPolicyDropdown",
    isInvalid: !selectedPolicyId
  })), selectedPolicyId && !isFleetServerPolicy && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, {
    size: "m"
  }), /*#__PURE__*/_react.default.createElement(_agent_policy_package_badges.AgentPolicyPackageBadges, {
    agentPolicyId: selectedPolicyId,
    excludeFleetServer: excludeFleetServer
  })), props.withKeySelection && props.onKeyChange && selectedPolicyId && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, {
    size: "m"
  }), /*#__PURE__*/_react.default.createElement(_advanced_agent_authentication_settings.AdvancedAgentAuthenticationSettings, {
    selectedApiKeyId: props.selectedApiKeyId,
    onKeyChange: props.onKeyChange,
    initialAuthenticationSettingsOpen: !props.selectedApiKeyId,
    agentPolicyId: selectedPolicyId
  })));
};
exports.AgentPolicySelection = AgentPolicySelection;