"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useCreateIntegration = void 0; var _react = require("react"); var _reactFastCompare = _interopRequireDefault(require("react-fast-compare")); var _public = require("@kbn/kibana-react-plugin/public"); var _useTrackedPromise = require("@kbn/use-tracked-promise"); var _i18n = require("@kbn/i18n"); /* * 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. */ // Errors const GENERIC_ERROR_MESSAGE = _i18n.i18n.translate('xpack.observability_onboarding.useCreateIntegration.integrationError.genericError', { defaultMessage: 'Unable to create an integration' }); const useCreateIntegration = ({ onIntegrationCreationSuccess, onIntegrationCreationFailure, initialLastCreatedIntegration, deletePreviousIntegration = true }) => { const { services: { http } } = (0, _public.useKibana)(); const [lastCreatedIntegration, setLastCreatedIntegration] = (0, _react.useState)(initialLastCreatedIntegration); const [createIntegrationRequest, callCreateIntegration] = (0, _useTrackedPromise.useTrackedPromise)({ cancelPreviousOn: 'creation', createPromise: async integrationOptions => { if (lastCreatedIntegration && deletePreviousIntegration) { await (http === null || http === void 0 ? void 0 : http.delete(`/api/fleet/epm/packages/${lastCreatedIntegration.integrationName}/1.0.0`, {})); } await (http === null || http === void 0 ? void 0 : http.post('/api/fleet/epm/custom_integrations', { body: JSON.stringify(integrationOptions) })); return integrationOptions; }, onResolve: integrationOptions => { setLastCreatedIntegration(integrationOptions); onIntegrationCreationSuccess(integrationOptions); }, onReject: requestError => { var _requestError$body, _requestError$body2; if ((requestError === null || requestError === void 0 ? void 0 : (_requestError$body = requestError.body) === null || _requestError$body === void 0 ? void 0 : _requestError$body.statusCode) === 409) { onIntegrationCreationFailure({ type: 'NamingCollision', message: requestError.body.message }); } else if ((requestError === null || requestError === void 0 ? void 0 : (_requestError$body2 = requestError.body) === null || _requestError$body2 === void 0 ? void 0 : _requestError$body2.statusCode) === 403) { var _requestError$body3; onIntegrationCreationFailure({ type: 'AuthorizationError', message: requestError === null || requestError === void 0 ? void 0 : (_requestError$body3 = requestError.body) === null || _requestError$body3 === void 0 ? void 0 : _requestError$body3.message }); } else { var _requestError$body$me, _requestError$body4; onIntegrationCreationFailure({ type: 'UnknownError', message: (_requestError$body$me = requestError === null || requestError === void 0 ? void 0 : (_requestError$body4 = requestError.body) === null || _requestError$body4 === void 0 ? void 0 : _requestError$body4.message) !== null && _requestError$body$me !== void 0 ? _requestError$body$me : GENERIC_ERROR_MESSAGE }); } } }, [lastCreatedIntegration, deletePreviousIntegration, onIntegrationCreationSuccess, onIntegrationCreationFailure, setLastCreatedIntegration]); const createIntegration = (0, _react.useCallback)(integrationOptions => { // Bypass creating the integration again if ((0, _reactFastCompare.default)(integrationOptions, lastCreatedIntegration)) { onIntegrationCreationSuccess(integrationOptions); } else { callCreateIntegration(integrationOptions); } }, [callCreateIntegration, lastCreatedIntegration, onIntegrationCreationSuccess]); return { createIntegration, createIntegrationRequest }; }; exports.useCreateIntegration = useCreateIntegration;