"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.WorkpadTable = void 0;
var _react = _interopRequireWildcard(require("react"));
var _i18n = require("@kbn/i18n");
var _eui = require("@elastic/eui");
var _moment = _interopRequireDefault(require("moment"));
var _routing = require("../../routing");
var _workpad_table_tools = require("./workpad_table_tools");
var _workpad_import = require("./workpad_import");
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 getDisplayName = (name, workpadId, loadedWorkpadId) => {
  const workpadName = name.length ? /*#__PURE__*/_react.default.createElement("span", null, name) : /*#__PURE__*/_react.default.createElement("em", null, workpadId);
  return workpadId === loadedWorkpadId ? /*#__PURE__*/_react.default.createElement("strong", null, workpadName) : workpadName;
};
const WorkpadTable = ({
  workpads,
  canUserWrite,
  dateFormat,
  onExportWorkpad: onExport,
  onCloneWorkpad
}) => {
  const [selectedIds, setSelectedIds] = (0, _react.useState)([]);
  const formatDate = date => date && (0, _moment.default)(date).format(dateFormat);
  const selection = {
    onSelectionChange: selectedWorkpads => {
      setSelectedIds(selectedWorkpads.map(workpad => workpad.id).filter(id => !!id));
    }
  };
  const actions = [{
    render: workpad => /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
      gutterSize: "xs",
      alignItems: "center"
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
      grow: false
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiToolTip, {
      content: strings.getExportToolTip()
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiButtonIcon, {
      iconType: "exportAction",
      onClick: () => onExport(workpad.id),
      "aria-label": strings.getExportToolTip()
    }))), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
      grow: false
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiToolTip, {
      content: canUserWrite ? strings.getCloneToolTip() : strings.getNoPermissionToCloneToolTip()
    }, /*#__PURE__*/_react.default.createElement(_eui.EuiButtonIcon, {
      iconType: "copy",
      onClick: () => onCloneWorkpad(workpad.id),
      "aria-label": strings.getCloneToolTip(),
      disabled: !canUserWrite
    }))))
  }];
  const search = {
    toolsLeft: selectedIds.length > 0 ? /*#__PURE__*/_react.default.createElement(_workpad_table_tools.WorkpadTableTools, {
      selectedWorkpadIds: selectedIds
    }) : undefined,
    toolsRight: /*#__PURE__*/_react.default.createElement(_workpad_import.WorkpadImport, null),
    box: {
      schema: true,
      incremental: true,
      placeholder: strings.getWorkpadSearchPlaceholder(),
      'data-test-subj': 'tableListSearchBox'
    }
  };
  const columns = [{
    field: 'name',
    name: strings.getTableNameColumnTitle(),
    sortable: true,
    dataType: 'string',
    render: (name, workpad) => /*#__PURE__*/_react.default.createElement(_routing.RoutingLink, {
      "data-test-subj": "canvasWorkpadTableWorkpad",
      to: `/workpad/${workpad.id}`,
      "aria-label": strings.getLoadWorkpadArialLabel(name.length ? name : workpad.id)
    }, getDisplayName(name, workpad.id))
  }, {
    field: '@created',
    name: strings.getTableCreatedColumnTitle(),
    sortable: true,
    dataType: 'date',
    width: '20%',
    render: date => formatDate(date)
  }, {
    field: '@timestamp',
    name: strings.getTableUpdatedColumnTitle(),
    sortable: true,
    dataType: 'date',
    width: '20%',
    render: date => formatDate(date)
  }, {
    name: strings.getTableActionsColumnTitle(),
    actions,
    width: '100px'
  }];
  return /*#__PURE__*/_react.default.createElement(_eui.EuiInMemoryTable, {
    itemId: "id",
    items: workpads,
    columns: columns,
    message: strings.getNoWorkpadsFoundMessage(),
    search: search,
    sorting: {
      sort: {
        field: '@timestamp',
        direction: 'desc'
      }
    },
    pagination: true,
    selection: selection,
    "data-test-subj": "canvasWorkpadTable"
  });
};
exports.WorkpadTable = WorkpadTable;
const strings = {
  getCloneToolTip: () => _i18n.i18n.translate('xpack.canvas.workpadTable.cloneTooltip', {
    defaultMessage: 'Clone workpad'
  }),
  getExportToolTip: () => _i18n.i18n.translate('xpack.canvas.workpadTable.exportTooltip', {
    defaultMessage: 'Export workpad'
  }),
  getLoadWorkpadArialLabel: workpadName => _i18n.i18n.translate('xpack.canvas.workpadTable.loadWorkpadArialLabel', {
    defaultMessage: `Load workpad '{workpadName}'`,
    values: {
      workpadName
    }
  }),
  getNoPermissionToCloneToolTip: () => _i18n.i18n.translate('xpack.canvas.workpadTable.noPermissionToCloneToolTip', {
    defaultMessage: `You don't have permission to clone workpads`
  }),
  getNoWorkpadsFoundMessage: () => _i18n.i18n.translate('xpack.canvas.workpadTable.noWorkpadsFoundMessage', {
    defaultMessage: 'No workpads matched your search.'
  }),
  getWorkpadSearchPlaceholder: () => _i18n.i18n.translate('xpack.canvas.workpadTable.searchPlaceholder', {
    defaultMessage: 'Find workpad'
  }),
  getTableCreatedColumnTitle: () => _i18n.i18n.translate('xpack.canvas.workpadTable.table.createdColumnTitle', {
    defaultMessage: 'Created',
    description: 'This column in the table contains the date/time the workpad was created.'
  }),
  getTableNameColumnTitle: () => _i18n.i18n.translate('xpack.canvas.workpadTable.table.nameColumnTitle', {
    defaultMessage: 'Workpad name'
  }),
  getTableUpdatedColumnTitle: () => _i18n.i18n.translate('xpack.canvas.workpadTable.table.updatedColumnTitle', {
    defaultMessage: 'Updated',
    description: 'This column in the table contains the date/time the workpad was last updated.'
  }),
  getTableActionsColumnTitle: () => _i18n.i18n.translate('xpack.canvas.workpadTable.table.actionsColumnTitle', {
    defaultMessage: 'Actions',
    description: 'This column in the table contains the actions that can be taken on a workpad.'
  })
};