"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DEFAULT_SELECT_VALUE = void 0;
exports.useDownloadSourcesOptions = useDownloadSourcesOptions;
exports.useFleetServerHostsOptions = useFleetServerHostsOptions;
exports.useOutputOptions = useOutputOptions;
var _react = _interopRequireWildcard(require("react"));
var _i18n = require("@kbn/i18n");
var _i18nReact = require("@kbn/i18n-react");
var _eui = require("@elastic/eui");
var _hooks = require("../../../../hooks");
var _constants = require("../../../../../../../common/constants");
var _services = require("../../../../../../../common/services");
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.
 */

// The super select component do not support null or '' as a value
const DEFAULT_SELECT_VALUE = '@@##DEFAULT_SELECT##@@';
exports.DEFAULT_SELECT_VALUE = DEFAULT_SELECT_VALUE;
function getOutputLabel(name, disabledMessage) {
  if (!disabledMessage) {
    return name;
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiText, {
    size: "s"
  }, name), /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, {
    size: "xs"
  }), /*#__PURE__*/_react.default.createElement(_eui.EuiText, {
    size: "s"
  }, disabledMessage));
}
function getDefaultOutput(defaultOutputName, defaultOutputDisabled, defaultOutputDisabledMessage) {
  return {
    inputDisplay: getOutputLabel(_i18n.i18n.translate('xpack.fleet.agentPolicy.outputOptions.defaultOutputText', {
      defaultMessage: 'Default (currently {defaultOutputName})',
      values: {
        defaultOutputName
      }
    }), defaultOutputDisabledMessage),
    value: DEFAULT_SELECT_VALUE,
    disabled: defaultOutputDisabled
  };
}
function useOutputOptions(agentPolicy) {
  const outputsRequest = (0, _hooks.useGetOutputs)();
  const licenseService = (0, _hooks.useLicense)();

  // Allow changing output when agent policy has fleet server or synthetics integrations
  const isPolicyPerOutputAllowed = licenseService.hasAtLeast(_constants.LICENCE_FOR_PER_POLICY_OUTPUT) || (0, _services.policyHasFleetServer)(agentPolicy) || (0, _services.policyHasSyntheticsIntegration)(agentPolicy);
  const allowedOutputTypes = (0, _react.useMemo)(() => (0, _services.getAllowedOutputTypeForPolicy)(agentPolicy), [agentPolicy]);
  const dataOutputOptions = (0, _react.useMemo)(() => {
    if (outputsRequest.isLoading || !outputsRequest.data) {
      return [];
    }
    if (outputsRequest.isLoading || !outputsRequest.data) {
      return [];
    }
    const defaultOutput = outputsRequest.data.items.find(item => item.is_default);
    const defaultOutputName = defaultOutput === null || defaultOutput === void 0 ? void 0 : defaultOutput.name;
    const defaultOutputDisabled = (defaultOutput === null || defaultOutput === void 0 ? void 0 : defaultOutput.type) && !allowedOutputTypes.includes(defaultOutput.type);
    const defaultOutputDisabledMessage = defaultOutputDisabled ? /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
      id: "xpack.fleet.agentPolicyForm.outputOptionDisableOutputTypeText",
      defaultMessage: "{outputType} output for agent integration is not supported for Fleet Server, Synthetics or APM.",
      values: {
        outputType: defaultOutput.type
      }
    }) : undefined;
    return [getDefaultOutput(defaultOutputName, defaultOutputDisabled, defaultOutputDisabledMessage), ...outputsRequest.data.items.map(item => {
      const isOutputTypeUnsupported = !allowedOutputTypes.includes(item.type);
      return {
        value: item.id,
        inputDisplay: getOutputLabel(item.name, isOutputTypeUnsupported ? /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
          id: "xpack.fleet.agentPolicyForm.outputOptionDisabledTypeNotSupportedText",
          defaultMessage: "{outputType} output for agent integration is not supported for Fleet Server, Synthetics or APM.",
          values: {
            outputType: item.type
          }
        }) : undefined),
        disabled: !isPolicyPerOutputAllowed || isOutputTypeUnsupported
      };
    })];
  }, [outputsRequest, isPolicyPerOutputAllowed, allowedOutputTypes]);
  const monitoringOutputOptions = (0, _react.useMemo)(() => {
    var _outputsRequest$data$;
    if (outputsRequest.isLoading || !outputsRequest.data) {
      return [];
    }
    const defaultOutputName = (_outputsRequest$data$ = outputsRequest.data.items.find(item => item.is_default_monitoring)) === null || _outputsRequest$data$ === void 0 ? void 0 : _outputsRequest$data$.name;
    return [getDefaultOutput(defaultOutputName), ...outputsRequest.data.items.map(item => {
      return {
        value: item.id,
        inputDisplay: item.name,
        disabled: !isPolicyPerOutputAllowed
      };
    })];
  }, [outputsRequest, isPolicyPerOutputAllowed]);
  return (0, _react.useMemo)(() => ({
    dataOutputOptions,
    monitoringOutputOptions,
    isLoading: outputsRequest.isLoading
  }), [dataOutputOptions, monitoringOutputOptions, outputsRequest.isLoading]);
}
function useDownloadSourcesOptions(agentPolicy) {
  const downloadSourcesRequest = (0, _hooks.useGetDownloadSources)();
  const dataDownloadSourceOptions = (0, _react.useMemo)(() => {
    if (downloadSourcesRequest.isLoading || !downloadSourcesRequest.data) {
      return [];
    }
    const defaultDownloadSource = downloadSourcesRequest.data.items.find(item => item.is_default);
    const defaultDownloadSourceName = defaultDownloadSource === null || defaultDownloadSource === void 0 ? void 0 : defaultDownloadSource.name;
    return [getDefaultDownloadSource(defaultDownloadSourceName), ...downloadSourcesRequest.data.items.filter(item => !item.is_default).map(item => {
      return {
        value: item.id,
        inputDisplay: item.name
      };
    })];
  }, [downloadSourcesRequest]);
  return (0, _react.useMemo)(() => ({
    dataDownloadSourceOptions,
    isLoading: downloadSourcesRequest.isLoading
  }), [dataDownloadSourceOptions, downloadSourcesRequest.isLoading]);
}
function getDefaultDownloadSource(defaultDownloadSourceName, defaultDownloadSourceDisabled, defaultDownloadSourceDisabledMessage) {
  return {
    inputDisplay: getOutputLabel(_i18n.i18n.translate('xpack.fleet.agentPolicy.downloadSourcesOptions.defaultOutputText', {
      defaultMessage: 'Default (currently {defaultDownloadSourceName})',
      values: {
        defaultDownloadSourceName
      }
    }), defaultDownloadSourceDisabledMessage),
    value: DEFAULT_SELECT_VALUE,
    disabled: defaultDownloadSourceDisabled
  };
}
function useFleetServerHostsOptions(agentPolicy) {
  const fleetServerHostsRequest = (0, _hooks.useGetFleetServerHosts)();
  const fleetServerHostsOptions = (0, _react.useMemo)(() => {
    if (fleetServerHostsRequest.isLoading || !fleetServerHostsRequest.data) {
      return [];
    }
    const defaultFleetServerHosts = fleetServerHostsRequest.data.items.find(item => item.is_default);
    const defaultFleetServerHostsName = defaultFleetServerHosts === null || defaultFleetServerHosts === void 0 ? void 0 : defaultFleetServerHosts.name;
    return [getDefaultFleetServerHosts(defaultFleetServerHostsName), ...fleetServerHostsRequest.data.items.filter(item => !item.is_default).map(item => {
      return {
        value: item.id,
        inputDisplay: item.name
      };
    })];
  }, [fleetServerHostsRequest]);
  return (0, _react.useMemo)(() => ({
    fleetServerHostsOptions,
    isLoading: fleetServerHostsRequest.isLoading
  }), [fleetServerHostsOptions, fleetServerHostsRequest.isLoading]);
}
function getDefaultFleetServerHosts(defaultFleetServerHostsName, defaultFleetServerHostsDisabled, defaultFleetServerHostsDisabledMessage) {
  return {
    inputDisplay: getOutputLabel(_i18n.i18n.translate('xpack.fleet.agentPolicy.fleetServerHostsOptions.defaultOutputText', {
      defaultMessage: 'Default (currently {defaultFleetServerHostsName})',
      values: {
        defaultFleetServerHostsName
      }
    }), defaultFleetServerHostsDisabledMessage),
    value: DEFAULT_SELECT_VALUE,
    disabled: defaultFleetServerHostsDisabled
  };
}