"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AlertingFlyout = AlertingFlyout;
var _react = _interopRequireWildcard(require("react"));
var _public = require("@kbn/kibana-react-plugin/public");
var _apm_rule_types = require("../../../../../common/rules/apm_rule_types");
var _get_initial_alert_values = require("../../utils/get_initial_alert_values");
var _use_service_name = require("../../../../hooks/use_service_name");
var _use_apm_params = require("../../../../hooks/use_apm_params");
var _environment_filter_values = require("../../../../../common/environment_filter_values");
var _use_time_range = require("../../../../hooks/use_time_range");
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.
 */

function AlertingFlyout(props) {
  const {
    addFlyoutVisible,
    setAddFlyoutVisibility,
    ruleType
  } = props;
  const serviceName = (0, _use_service_name.useServiceName)();
  const {
    query,
    path
  } = (0, _use_apm_params.useApmParams)('/*');
  const rangeFrom = 'rangeFrom' in query ? query.rangeFrom : undefined;
  const rangeTo = 'rangeTo' in query ? query.rangeTo : undefined;
  const {
    start,
    end
  } = (0, _use_time_range.useTimeRange)({
    rangeFrom,
    rangeTo,
    optional: true
  });
  const environment = 'environment' in query ? query.environment : _environment_filter_values.ENVIRONMENT_ALL.value;
  const transactionType = 'transactionType' in query ? query.transactionType : undefined;
  const transactionName = 'transactionName' in query ? query.transactionName : undefined;
  const errorGroupingKey = 'groupId' in path ? path.groupId : undefined;
  const {
    services
  } = (0, _public.useKibana)();
  const initialValues = (0, _get_initial_alert_values.getInitialAlertValues)(ruleType, serviceName);
  const onCloseAddFlyout = (0, _react.useCallback)(() => setAddFlyoutVisibility(false), [setAddFlyoutVisibility]);
  const addAlertFlyout = (0, _react.useMemo)(() => ruleType && services.triggersActionsUi.getAddRuleFlyout({
    consumer: _apm_rule_types.APM_SERVER_FEATURE_ID,
    onClose: onCloseAddFlyout,
    ruleTypeId: ruleType,
    canChangeTrigger: false,
    initialValues,
    metadata: {
      environment,
      serviceName,
      ...(ruleType === _apm_rule_types.ApmRuleType.ErrorCount ? {} : {
        transactionType
      }),
      transactionName,
      errorGroupingKey,
      start,
      end
    }
  }), /* eslint-disable-next-line react-hooks/exhaustive-deps */
  [ruleType, environment, onCloseAddFlyout, services.triggersActionsUi, serviceName, transactionName, errorGroupingKey, transactionType, environment, start, end]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, addFlyoutVisible && addAlertFlyout);
}