"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DashboardEmptyScreen = DashboardEmptyScreen;
var _react = _interopRequireWildcard(require("react"));
var _useObservable2 = _interopRequireDefault(require("react-use/lib/useObservable"));
var _eui = require("@elastic/eui");
var _analytics = require("@kbn/analytics");
var _public = require("@kbn/embeddable-plugin/public");
var _plugin_services = require("../../../services/plugin_services");
var _dashboard_container_strings = require("../../_dashboard_container_strings");
var _dashboard_container = require("../../embeddable/dashboard_container");
var _dashboard_constants = require("../../../dashboard_constants");
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.
 */

function DashboardEmptyScreen() {
  var _useObservable, _embeddableAppContext, _embeddableAppContext2;
  const {
    settings: {
      theme: {
        theme$
      }
    },
    usageCollection,
    data: {
      search
    },
    http: {
      basePath
    },
    embeddable: {
      getStateTransfer
    },
    dashboardCapabilities: {
      showWriteControls
    },
    visualizations: {
      getAliases: getVisTypeAliases
    }
  } = _plugin_services.pluginServices.getServices();
  const lensAlias = (0, _react.useMemo)(() => getVisTypeAliases().find(({
    name
  }) => name === 'lens'), [getVisTypeAliases]);
  const dashboardContainer = (0, _dashboard_container.useDashboardContainer)();
  const isDarkTheme = (_useObservable = (0, _useObservable2.default)(theme$)) === null || _useObservable === void 0 ? void 0 : _useObservable.darkMode;
  const isEditMode = dashboardContainer.select(state => state.explicitInput.viewMode) === _public.ViewMode.EDIT;
  const embeddableAppContext = dashboardContainer.getAppContext();
  const originatingPath = (_embeddableAppContext = embeddableAppContext === null || embeddableAppContext === void 0 ? void 0 : (_embeddableAppContext2 = embeddableAppContext.getCurrentPath) === null || _embeddableAppContext2 === void 0 ? void 0 : _embeddableAppContext2.call(embeddableAppContext)) !== null && _embeddableAppContext !== void 0 ? _embeddableAppContext : '';
  const originatingApp = embeddableAppContext === null || embeddableAppContext === void 0 ? void 0 : embeddableAppContext.currentAppId;
  const goToLens = (0, _react.useCallback)(() => {
    var _usageCollection$repo;
    if (!lensAlias || !lensAlias.aliasPath) return;
    const trackUiMetric = (_usageCollection$repo = usageCollection.reportUiCounter) === null || _usageCollection$repo === void 0 ? void 0 : _usageCollection$repo.bind(usageCollection, _dashboard_constants.DASHBOARD_UI_METRIC_ID);
    if (trackUiMetric) {
      trackUiMetric(_analytics.METRIC_TYPE.CLICK, `${lensAlias.name}:create`);
    }
    getStateTransfer().navigateToEditor(lensAlias.aliasApp, {
      path: lensAlias.aliasPath,
      state: {
        originatingApp,
        originatingPath,
        searchSessionId: search.session.getSessionId()
      }
    });
  }, [getStateTransfer, lensAlias, originatingApp, originatingPath, search.session, usageCollection]);

  // TODO replace these SVGs with versions from EuiIllustration as soon as it becomes available.
  const imageUrl = basePath.prepend(`/plugins/dashboard/assets/${isDarkTheme ? 'dashboards_dark' : 'dashboards_light'}.svg`);

  // If the user ends up in edit mode without write privileges, we shouldn't show the edit prompt.
  const showEditPrompt = showWriteControls && isEditMode;
  const emptyPromptTestSubject = (() => {
    if (showEditPrompt) return 'emptyDashboardWidget';
    return showWriteControls ? 'dashboardEmptyReadWrite' : 'dashboardEmptyReadOnly';
  })();
  const title = (() => {
    const titleString = showEditPrompt ? _dashboard_container_strings.emptyScreenStrings.getEditModeTitle() : showWriteControls ? _dashboard_container_strings.emptyScreenStrings.getViewModeWithPermissionsTitle() : _dashboard_container_strings.emptyScreenStrings.getViewModeWithoutPermissionsTitle();
    return /*#__PURE__*/_react.default.createElement("h2", null, titleString);
  })();
  const body = (() => {
    const bodyString = showEditPrompt ? _dashboard_container_strings.emptyScreenStrings.getEditModeSubtitle() : showWriteControls ? _dashboard_container_strings.emptyScreenStrings.getViewModeWithPermissionsSubtitle() : _dashboard_container_strings.emptyScreenStrings.getViewModeWithoutPermissionsSubtitle();
    return /*#__PURE__*/_react.default.createElement(_eui.EuiText, {
      size: "s",
      color: "subdued"
    }, /*#__PURE__*/_react.default.createElement("span", null, bodyString));
  })();
  const actions = (() => {
    if (showEditPrompt) {
      return /*#__PURE__*/_react.default.createElement(_eui.EuiFlexGroup, {
        justifyContent: "center",
        gutterSize: "l",
        alignItems: "center"
      }, /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
        grow: false
      }, /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
        iconType: "lensApp",
        onClick: () => goToLens()
      }, _dashboard_container_strings.emptyScreenStrings.getCreateVisualizationButtonTitle())), /*#__PURE__*/_react.default.createElement(_eui.EuiFlexItem, {
        grow: false
      }, /*#__PURE__*/_react.default.createElement(_eui.EuiButtonEmpty, {
        flush: "left",
        iconType: "folderOpen",
        onClick: () => dashboardContainer.addFromLibrary()
      }, _dashboard_container_strings.emptyScreenStrings.getAddFromLibraryButtonTitle())));
    }
    if (showWriteControls) {
      return /*#__PURE__*/_react.default.createElement(_eui.EuiButton, {
        iconType: "pencil",
        onClick: () => dashboardContainer.dispatch.setViewMode(_public.ViewMode.EDIT)
      }, _dashboard_container_strings.emptyScreenStrings.getEditLinkTitle());
    }
  })();
  return /*#__PURE__*/_react.default.createElement("div", {
    className: "dshEmptyPromptParent"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiPageTemplate, {
    grow: false,
    "data-test-subj": emptyPromptTestSubject,
    className: "dshEmptyPromptPageTemplate"
  }, /*#__PURE__*/_react.default.createElement(_eui.EuiPageTemplate.EmptyPrompt, {
    icon: /*#__PURE__*/_react.default.createElement(_eui.EuiImage, {
      size: "fullWidth",
      src: imageUrl,
      alt: ""
    }),
    title: title,
    body: body,
    actions: actions,
    titleSize: "xs",
    color: "transparent",
    className: "dshEmptyWidgetContainer"
  })));
}