"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.showDelete = exports.renderFieldName = exports.getConflictModalContent = exports.Table = void 0;
var _react = _interopRequireWildcard(require("react"));
var _eui = require("@elastic/eui");
var _i18n = require("@kbn/i18n");
var _i18nReact = require("@kbn/i18n-react");
var _public = require("@kbn/kibana-react-plugin/public");
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 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

const showDelete = field => {
  var _field$runtimeField, _field$runtimeField2;
  return (
    // runtime fields that aren't composite subfields
    !field.isMapped && field.isUserEditable && ((_field$runtimeField = field.runtimeField) === null || _field$runtimeField === void 0 ? void 0 : _field$runtimeField.type) !== 'composite' ||
    // composite runtime field definitions
    ((_field$runtimeField2 = field.runtimeField) === null || _field$runtimeField2 === void 0 ? void 0 : _field$runtimeField2.type) === 'composite' && field.type === 'composite'
  );
};

// localized labels
exports.showDelete = showDelete;
const additionalInfoAriaLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.additionalInfoAriaLabel', {
  defaultMessage: 'Additional field information'
});
const conflictDetailIconAria = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.conflictDetailIconAria', {
  defaultMessage: 'Conflict Detail'
});
const primaryTimeAriaLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.primaryTimeAriaLabel', {
  defaultMessage: 'Primary time field'
});
const primaryTimeTooltip = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.primaryTimeTooltip', {
  defaultMessage: 'This field represents the time that events occurred.'
});
const nameHeader = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.nameHeader', {
  defaultMessage: 'Name'
});
const typeHeader = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.typeHeader', {
  defaultMessage: 'Type'
});
const formatHeader = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.formatHeader', {
  defaultMessage: 'Format'
});
const searchableHeader = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.searchableHeader', {
  defaultMessage: 'Searchable'
});
const searchableDescription = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.searchableDescription', {
  defaultMessage: 'These fields can be used in the filter bar'
});
const isSearchableAriaLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.isSearchableAria', {
  defaultMessage: 'Is searchable'
});
const aggregatableLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.aggregatableLabel', {
  defaultMessage: 'Aggregatable'
});
const aggregatableDescription = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.aggregatableDescription', {
  defaultMessage: 'These fields can be used in visualization aggregations'
});
const isAggregatableAriaLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.isAggregatableAria', {
  defaultMessage: 'Is aggregatable'
});
const excludedLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.excludedLabel', {
  defaultMessage: 'Excluded'
});
const excludedDescription = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.excludedDescription', {
  defaultMessage: 'Fields that are excluded from _source when it is fetched'
});
const isExcludedAriaLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.isExcludedAria', {
  defaultMessage: 'Is excluded'
});
const editLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.editLabel', {
  defaultMessage: 'Edit'
});
const editDescription = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.editDescription', {
  defaultMessage: 'Edit'
});
const deleteLabel = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.deleteLabel', {
  defaultMessage: 'Delete'
});
const deleteDescription = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.deleteDescription', {
  defaultMessage: 'Delete'
});
const labelDescription = _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.customLabelTooltip', {
  defaultMessage: 'A custom label for the field.'
});
function runtimeIconTipTitle(fld) {
  var _fld$runtimeField;
  // composite runtime fields
  if (((_fld$runtimeField = fld.runtimeField) === null || _fld$runtimeField === void 0 ? void 0 : _fld$runtimeField.type) === 'composite') {
    // subfields definitions
    if (fld.type !== 'composite') {
      return _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.runtimeIconTipTitleCompositeSubfield', {
        defaultMessage: 'Composite runtime subfield'
      });
      // composite definitions
    } else {
      return _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.runtimeIconTipTitleComposite', {
        defaultMessage: 'Composite runtime field'
      });
    }
  }
  return _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.runtimeIconTipTitle', {
    defaultMessage: 'Runtime field'
  });
}
const runtimeIconTipText = _i18n.i18n.translate('indexPatternManagement.editDataView.fields.table.runtimeIconTipText', {
  defaultMessage: 'This field exists on the data view only.'
});
const conflictType = _i18n.i18n.translate('indexPatternManagement.editDataView.fields.table.conflictType', {
  defaultMessage: 'Conflict'
});
const getItems = conflictDescriptions => {
  const typesAndIndices = [];
  Object.keys(conflictDescriptions).forEach(type => {
    // only show first 100 indices just incase the list is CRAZY long
    typesAndIndices.push({
      type,
      indices: conflictDescriptions[type].slice(0, 99).join(', ')
    });
  });
  return typesAndIndices;
};
const renderFieldName = (field, timeFieldName) => /*#__PURE__*/_react.default.createElement("span", {
  "data-test-subj": `field-name-${field.name}`
}, field.name, field.info && field.info.length ? /*#__PURE__*/_react.default.createElement("span", null, "\xA0", /*#__PURE__*/_react.default.createElement(_eui.EuiIconTip, {
  type: "questionInCircle",
  color: "primary",
  "aria-label": additionalInfoAriaLabel,
  content: field.info.map((info, i) => /*#__PURE__*/_react.default.createElement("div", {
    key: i
  }, info))
})) : null, timeFieldName === field.name ? /*#__PURE__*/_react.default.createElement("span", null, "\xA0", /*#__PURE__*/_react.default.createElement(_eui.EuiIconTip, {
  type: "clock",
  color: "primary",
  "aria-label": primaryTimeAriaLabel,
  content: primaryTimeTooltip
})) : null, !field.isMapped && field.hasRuntime ? /*#__PURE__*/_react.default.createElement("span", null, "\xA0", /*#__PURE__*/_react.default.createElement(_eui.EuiIconTip, {
  type: "indexRuntime",
  title: runtimeIconTipTitle(field),
  content: runtimeIconTipText
})) : null, field.customLabel && field.customLabel !== field.name ? /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_eui.EuiToolTip, {
  content: labelDescription
}, /*#__PURE__*/_react.default.createElement(_eui.EuiBadge, {
  iconType: "flag",
  iconSide: "left"
}, field.customLabel))) : null);
exports.renderFieldName = renderFieldName;
const conflictColumns = [{
  field: 'type',
  name: _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.conflictModalTypeColumn', {
    defaultMessage: 'Type'
  })
}, {
  field: 'indices',
  name: _i18n.i18n.translate('indexPatternManagement.editIndexPattern.fields.table.conflictModalIndicesColumn', {
    defaultMessage: 'Indices'
  })
}];
const getConflictModalContent = ({
  closeFn,
  fieldName,
  conflictDescriptions
}) => /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiModalHeader, null, /*#__PURE__*/_react.default.createElement(_eui.EuiModalHeaderTitle, null, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
  id: "indexPatternManagement.editIndexPattern.fields.conflictModal.title",
  defaultMessage: "This field has a type conflict"
}))), /*#__PURE__*/_react.default.createElement(_eui.EuiModalBody, null, /*#__PURE__*/_react.default.createElement(_eui.EuiText, null, /*#__PURE__*/_react.default.createElement("p", null, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
  id: "indexPatternManagement.editIndexPattern.fields.conflictModal.description",
  defaultMessage: "The type of the {fieldName} field changes across indices and might not be available for search, visualizations, and other analysis.",
  values: {
    fieldName: /*#__PURE__*/_react.default.createElement(_eui.EuiCode, null, fieldName)
  }
})), /*#__PURE__*/_react.default.createElement(_eui.EuiBasicTable, {
  tableCaption: "Demo of EuiBasicTable",
  items: getItems(conflictDescriptions),
  rowHeader: "firstName",
  columns: conflictColumns,
  tableLayout: "auto"
}))), /*#__PURE__*/_react.default.createElement(_eui.EuiModalFooter, null, /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
  onClick: closeFn,
  fill: true
}, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, {
  id: "indexPatternManagement.editIndexPattern.fields.conflictModal.closeBtn",
  defaultMessage: "Close"
}))));
exports.getConflictModalContent = getConflictModalContent;
const getConflictBtn = (fieldName, conflictDescriptions, openModal, theme) => {
  const onClick = () => {
    const overlayRef = openModal((0, _public.toMountPoint)(getConflictModalContent({
      closeFn: () => {
        overlayRef.close();
      },
      fieldName,
      conflictDescriptions
    }), {
      theme$: theme.theme$
    }));
  };
  return /*#__PURE__*/_react.default.createElement("span", null, /*#__PURE__*/_react.default.createElement(_eui.EuiBadge, {
    color: "warning",
    iconType: "warning",
    onClick: onClick,
    iconOnClick: onClick,
    iconOnClickAriaLabel: conflictDetailIconAria,
    onClickAriaLabel: conflictDetailIconAria
  }, conflictType));
};
class Table extends _react.PureComponent {
  renderBooleanTemplate(value, arialLabel) {
    return value ? /*#__PURE__*/_react.default.createElement(_eui.EuiIcon, {
      type: "dot",
      color: "success",
      "aria-label": arialLabel
    }) : /*#__PURE__*/_react.default.createElement("span", null);
  }
  renderFieldType(type, field) {
    const conflictDescription = field.conflictDescriptions && field.conflictDescriptions[field.name];
    return /*#__PURE__*/_react.default.createElement("span", null, type === 'conflict' && conflictDescription ? '' : type, field.conflictDescriptions ? getConflictBtn(field.name, field.conflictDescriptions, this.props.openModal, this.props.theme) : '');
  }
  render() {
    const {
      items,
      editField,
      deleteField,
      indexPattern
    } = this.props;
    const pagination = {
      initialPageSize: 10,
      pageSizeOptions: [5, 10, 25, 50]
    };
    const columns = [{
      field: 'displayName',
      name: nameHeader,
      dataType: 'string',
      sortable: true,
      render: (value, field) => {
        return renderFieldName(field, indexPattern.timeFieldName);
      },
      width: '38%',
      'data-test-subj': 'indexedFieldName'
    }, {
      field: 'type',
      name: typeHeader,
      dataType: 'string',
      sortable: true,
      render: (value, field) => {
        return this.renderFieldType(value, field);
      },
      'data-test-subj': 'indexedFieldType'
    }, {
      field: 'format',
      name: formatHeader,
      dataType: 'string',
      sortable: true
    }, {
      field: 'searchable',
      name: searchableHeader,
      description: searchableDescription,
      dataType: 'boolean',
      sortable: true,
      render: value => this.renderBooleanTemplate(value, isSearchableAriaLabel)
    }, {
      field: 'aggregatable',
      name: aggregatableLabel,
      description: aggregatableDescription,
      dataType: 'boolean',
      sortable: true,
      render: value => this.renderBooleanTemplate(value, isAggregatableAriaLabel)
    }, {
      field: 'excluded',
      name: excludedLabel,
      description: excludedDescription,
      dataType: 'boolean',
      sortable: true,
      render: value => this.renderBooleanTemplate(value, isExcludedAriaLabel)
    }, {
      name: '',
      actions: [{
        name: editLabel,
        description: editDescription,
        icon: 'pencil',
        onClick: editField,
        type: 'icon',
        'data-test-subj': 'editFieldFormat',
        available: field => field.isUserEditable
      }],
      width: '40px'
    }, {
      name: '',
      actions: [{
        name: deleteLabel,
        description: deleteDescription,
        icon: 'trash',
        onClick: field => {
          var _field$spec, _field$spec$runtimeFi;
          const toDelete = [field.name];
          if ((_field$spec = field.spec) !== null && _field$spec !== void 0 && (_field$spec$runtimeFi = _field$spec.runtimeField) !== null && _field$spec$runtimeFi !== void 0 && _field$spec$runtimeFi.fields) {
            const childFieldNames = Object.keys(field.spec.runtimeField.fields).map(key => `${field.name}.${key}`);
            toDelete.push(...childFieldNames);
          }
          deleteField(toDelete);
        },
        type: 'icon',
        'data-test-subj': 'deleteField',
        available: showDelete
      }],
      width: '40px'
    }];
    return /*#__PURE__*/_react.default.createElement(_eui.EuiInMemoryTable, {
      items: items,
      columns: columns,
      pagination: pagination,
      sorting: {
        sort: {
          field: 'displayName',
          direction: 'asc'
        }
      }
    });
  }
}
exports.Table = Table;