"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _eui = require("@elastic/eui");
var _hook_form_lib = require("@kbn/es-ui-shared-plugin/static/forms/hook_form_lib");
var _components = require("@kbn/es-ui-shared-plugin/static/forms/components");
var _kibana = require("../../../common/lib/kibana");
var _use_get_choices = require("./use_get_choices");
var _helpers = require("./helpers");
var i18n = _interopRequireWildcard(require("./translations"));
var _deprecated_callout = require("../deprecated_callout");
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 choicesToGet = ['category', 'subcategory', 'priority'];
const defaultFields = {
  category: [],
  subcategory: [],
  priority: []
};
const ServiceNowSIRFieldsComponent = ({
  connector
}) => {
  var _choicesData$data;
  const form = (0, _hook_form_lib.useFormContext)();
  const [{
    fields
  }] = (0, _hook_form_lib.useFormData)();
  const {
    category = null
  } = fields !== null && fields !== void 0 ? fields : {};
  const {
    http
  } = (0, _kibana.useKibana)().services;
  const showConnectorWarning = connector.isDeprecated;
  const {
    isLoading,
    isFetching,
    data: choicesData
  } = (0, _use_get_choices.useGetChoices)({
    http,
    connector,
    fields: choicesToGet
  });
  const choices = (_choicesData$data = choicesData === null || choicesData === void 0 ? void 0 : choicesData.data) !== null && _choicesData$data !== void 0 ? _choicesData$data : [];
  const isLoadingChoices = isLoading || isFetching;
  const choicesFormatted = choices.reduce((acc, value) => ({
    ...acc,
    [value.element]: [...(acc[value.element] != null ? acc[value.element] : []), value]
  }), defaultFields);
  const categoryOptions = (0, _react.useMemo)(() => (0, _helpers.choicesToEuiOptions)(choicesFormatted.category), [choicesFormatted.category]);
  const priorityOptions = (0, _react.useMemo)(() => (0, _helpers.choicesToEuiOptions)(choicesFormatted.priority), [choicesFormatted.priority]);
  const subcategoryOptions = (0, _react.useMemo)(() => (0, _helpers.choicesToEuiOptions)(choicesFormatted.subcategory.filter(choice => choice.dependent_value === category)), [choicesFormatted.subcategory, category]);
  const onCategoryChange = () => {
    form.setFieldValue('fields.subcategory', null);
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, showConnectorWarning && /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_deprecated_callout.DeprecatedCallout, null))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
    "data-test-subj": "connector-fields-sn-sir",
    direction: "column",
    gutterSize: "s"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFormRow, {
    fullWidth: true,
    label: i18n.ALERT_FIELDS_LABEL
  }, /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_hook_form_lib.UseField, {
    path: "fields.destIp",
    config: {
      defaultValue: true
    },
    component: _components.CheckBoxField,
    componentProps: {
      euiFieldProps: {
        'data-test-subj': 'destIpCheckbox',
        label: i18n.DEST_IP,
        compressed: true
      }
    }
  })), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_hook_form_lib.UseField, {
    path: "fields.sourceIp",
    config: {
      defaultValue: true
    },
    component: _components.CheckBoxField,
    componentProps: {
      euiFieldProps: {
        'data-test-subj': 'sourceIpCheckbox',
        label: i18n.SOURCE_IP,
        compressed: true
      }
    }
  }))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_hook_form_lib.UseField, {
    path: "fields.malwareUrl",
    config: {
      defaultValue: true
    },
    component: _components.CheckBoxField,
    componentProps: {
      euiFieldProps: {
        'data-test-subj': 'malwareUrlCheckbox',
        label: i18n.MALWARE_URL,
        compressed: true
      }
    }
  })), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_hook_form_lib.UseField, {
    path: "fields.malwareHash",
    config: {
      defaultValue: true
    },
    component: _components.CheckBoxField,
    componentProps: {
      euiFieldProps: {
        'data-test-subj': 'malwareHashCheckbox',
        label: i18n.MALWARE_HASH,
        compressed: true
      }
    }
  }))))))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_hook_form_lib.UseField, {
    path: "fields.priority",
    component: _components.SelectField,
    config: {
      label: i18n.PRIORITY
    },
    componentProps: {
      euiFieldProps: {
        'data-test-subj': 'prioritySelect',
        options: priorityOptions,
        hasNoInitialSelection: true,
        fullWidth: true,
        disabled: isLoadingChoices,
        isLoading: isLoadingChoices
      }
    }
  }))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, null, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_hook_form_lib.UseField, {
    path: "fields.category",
    component: _components.SelectField,
    config: {
      label: i18n.CATEGORY
    },
    onChange: onCategoryChange,
    componentProps: {
      euiFieldProps: {
        'data-test-subj': 'categorySelect',
        options: categoryOptions,
        hasNoInitialSelection: true,
        fullWidth: true,
        disabled: isLoadingChoices,
        isLoading: isLoadingChoices
      }
    }
  })), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, null, /*#__PURE__*/_react.default.createElement(_hook_form_lib.UseField, {
    path: "fields.subcategory",
    component: _components.SelectField,
    config: {
      label: i18n.SUBCATEGORY
    },
    componentProps: {
      euiFieldProps: {
        'data-test-subj': 'subcategorySelect',
        options: subcategoryOptions,
        hasNoInitialSelection: true,
        fullWidth: true,
        disabled: isLoadingChoices,
        isLoading: isLoadingChoices
      }
    }
  })))));
};
exports.default = ServiceNowSIRFieldsComponent;
ServiceNowSIRFieldsComponent.displayName = 'ServiceNowSIRFieldsComponent';

// eslint-disable-next-line import/no-default-export
module.exports = exports.default;