"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getBackwardCompatibleDocumentTypeFilter = getBackwardCompatibleDocumentTypeFilter;
exports.getDurationFieldForTransactions = getDurationFieldForTransactions;
exports.getHasTransactionsEvents = getHasTransactionsEvents;
exports.getProcessorEventForTransactions = getProcessorEventForTransactions;
exports.getSearchTransactionsEvents = getSearchTransactionsEvents;
exports.isRootTransaction = isRootTransaction;
var _server = require("@kbn/observability-plugin/server");
var _common = require("@kbn/observability-plugin/common");
var _aggregated_transactions = require("../../../../common/aggregated_transactions");
var _apm = require("../../../../common/es_fields/apm");
var _document_type = require("../../../../common/document_type");
/*
 * 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.
 */

async function getHasTransactionsEvents({
  start,
  end,
  apmEventClient,
  kuery
}) {
  const response = await apmEventClient.search('get_has_aggregated_transactions', {
    apm: {
      events: [_common.ProcessorEvent.metric]
    },
    body: {
      track_total_hits: 1,
      terminate_after: 1,
      size: 0,
      query: {
        bool: {
          filter: [{
            exists: {
              field: _apm.TRANSACTION_DURATION_HISTOGRAM
            }
          }, ...(start && end ? (0, _server.rangeQuery)(start, end) : []), ...(0, _server.kqlQuery)(kuery)]
        }
      }
    }
  });
  return response.hits.total.value > 0;
}
async function getSearchTransactionsEvents({
  config,
  start,
  end,
  apmEventClient,
  kuery
}) {
  switch (config.searchAggregatedTransactions) {
    case _aggregated_transactions.SearchAggregatedTransactionSetting.always:
      return kuery ? getHasTransactionsEvents({
        start,
        end,
        apmEventClient,
        kuery
      }) : true;
    case _aggregated_transactions.SearchAggregatedTransactionSetting.auto:
      return getHasTransactionsEvents({
        start,
        end,
        apmEventClient,
        kuery
      });
    case _aggregated_transactions.SearchAggregatedTransactionSetting.never:
      return false;
  }
}
function getDurationFieldForTransactions(typeOrSearchAgggregatedTransactions, useDurationSummaryField) {
  let type;
  if (typeOrSearchAgggregatedTransactions === true) {
    type = _document_type.ApmDocumentType.TransactionMetric;
  } else if (typeOrSearchAgggregatedTransactions === false) {
    type = _document_type.ApmDocumentType.TransactionEvent;
  } else {
    type = typeOrSearchAgggregatedTransactions;
  }
  if (type === _document_type.ApmDocumentType.ServiceTransactionMetric || type === _document_type.ApmDocumentType.TransactionMetric) {
    if (useDurationSummaryField) {
      return _apm.TRANSACTION_DURATION_SUMMARY;
    }
    return _apm.TRANSACTION_DURATION_HISTOGRAM;
  }
  return _apm.TRANSACTION_DURATION;
}

// The function returns Document type filter for 1m Transaction Metrics
function getBackwardCompatibleDocumentTypeFilter(searchAggregatedTransactions) {
  return searchAggregatedTransactions ? [{
    bool: {
      filter: [{
        exists: {
          field: _apm.TRANSACTION_DURATION_HISTOGRAM
        }
      }],
      must_not: [{
        terms: {
          [_apm.METRICSET_INTERVAL]: ['10m', '60m']
        }
      }, {
        term: {
          [_apm.METRICSET_NAME]: 'service_transaction'
        }
      }]
    }
  }] : [];
}
function getProcessorEventForTransactions(searchAggregatedTransactions) {
  return searchAggregatedTransactions ? _common.ProcessorEvent.metric : _common.ProcessorEvent.transaction;
}
function isRootTransaction(searchAggregatedTransactions) {
  return searchAggregatedTransactions ? {
    term: {
      [_apm.TRANSACTION_ROOT]: true
    }
  } : {
    bool: {
      must_not: {
        exists: {
          field: _apm.PARENT_ID
        }
      }
    }
  };
}