"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CreatePolicyWizard = void 0;
var _react = _interopRequireWildcard(require("react"));
var _i18n = require("@kbn/i18n");
var _eui = require("@elastic/eui");
var _app_context = require("../../app_context");
var _steps = require("./steps");
var _create_policy_context = require("./create_policy_context");
var _api = require("../../services/api");
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.
 */

const CONFIGURATION = 1;
const FIELD_SELECTION = 2;
const CREATE = 3;
const CreatePolicyWizard = () => {
  const {
    history,
    services: {
      notificationService
    }
  } = (0, _app_context.useAppContext)();
  const {
    draft,
    completionState
  } = (0, _create_policy_context.useCreatePolicyContext)();
  const [currentStep, setCurrentStep] = (0, _react.useState)(CONFIGURATION);
  const [isLoading, setIsLoading] = (0, _react.useState)(false);
  const [createError, setCreateError] = (0, _react.useState)(null);
  const getStepStatus = (0, _react.useCallback)(forStep => {
    if (currentStep === forStep) {
      return 'current';
    }
    return 'incomplete';
  }, [currentStep]);
  const onSubmit = (0, _react.useCallback)(async executePolicyAfterCreation => {
    setIsLoading(true);
    const {
      error,
      data
    } = await (0, _api.createEnrichPolicy)(draft, executePolicyAfterCreation);
    setIsLoading(false);
    if (data) {
      const toastMessage = executePolicyAfterCreation ? _i18n.i18n.translate('xpack.idxMgmt.enrichPoliciesCreate.createAndExecuteNotificationMessage', {
        defaultMessage: 'Created and executed policy: {policyName}',
        values: {
          policyName: draft.name
        }
      }) : _i18n.i18n.translate('xpack.idxMgmt.enrichPoliciesCreate.createNotificationMessage', {
        defaultMessage: 'Created policy: {policyName}',
        values: {
          policyName: draft.name
        }
      });
      notificationService.showSuccessToast(toastMessage);
    }
    if (error) {
      setCreateError(error);
      return;
    }
    history.push('/enrich_policies');
  }, [draft, history, setIsLoading, setCreateError, notificationService]);
  const changeCurrentStepTo = (0, _react.useCallback)(step => {
    setCurrentStep(step);
    setCreateError(null);
  }, [setCurrentStep, setCreateError]);
  const stepDefinitions = (0, _react.useMemo)(() => [{
    step: CONFIGURATION,
    title: _i18n.i18n.translate('xpack.idxMgmt.enrichPolicyCreate.configurationStepLabel', {
      defaultMessage: 'Configuration'
    }),
    status: completionState.configurationStep ? 'complete' : getStepStatus(CONFIGURATION),
    children: currentStep === CONFIGURATION && /*#__PURE__*/_react.default.createElement(_steps.ConfigurationStep, {
      onNext: () => changeCurrentStepTo(FIELD_SELECTION)
    })
  }, {
    step: FIELD_SELECTION,
    title: _i18n.i18n.translate('xpack.idxMgmt.enrichPolicyCreate.fieldSelectionStepLabel', {
      defaultMessage: 'Field selection'
    }),
    status: completionState.fieldsSelectionStep ? 'complete' : getStepStatus(FIELD_SELECTION),
    children: currentStep === FIELD_SELECTION && /*#__PURE__*/_react.default.createElement(_steps.FieldSelectionStep, {
      onNext: () => changeCurrentStepTo(CREATE),
      onBack: () => changeCurrentStepTo(CONFIGURATION)
    })
  }, {
    step: CREATE,
    title: _i18n.i18n.translate('xpack.idxMgmt.enrichPolicyCreate.createStepLabel', {
      defaultMessage: 'Create'
    }),
    status: currentStep === CREATE ? 'current' : 'incomplete',
    children: currentStep === CREATE && /*#__PURE__*/_react.default.createElement(_steps.CreateStep, {
      onSubmit: onSubmit,
      isLoading: isLoading,
      onBack: () => changeCurrentStepTo(FIELD_SELECTION)
    })
  }], [currentStep, changeCurrentStepTo, completionState, getStepStatus, isLoading, onSubmit]);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, createError && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiCallOut, {
    title: _i18n.i18n.translate('xpack.idxMgmt.enrichPolicyCreate.errorTitle', {
      defaultMessage: 'Unable to create your policy'
    }),
    color: "danger",
    iconType: "error",
    "data-test-subj": "errorWhenCreatingCallout"
  }, /*#__PURE__*/_react.default.createElement("p", {
    className: "eui-textBreakWord"
  }, (createError === null || createError === void 0 ? void 0 : createError.message) || (createError === null || createError === void 0 ? void 0 : createError.error))), /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, {
    size: "xl"
  })), /*#__PURE__*/_react.default.createElement(_eui.EuiSteps, {
    steps: stepDefinitions
  }));
};
exports.CreatePolicyWizard = CreatePolicyWizard;