"use strict";

exports.__esModule = true;
exports.default = void 0;
var _keys2 = _interopRequireDefault(require("lodash/fp/keys"));
var _kebabCase2 = _interopRequireDefault(require("lodash/fp/kebabCase"));
var _getOr2 = _interopRequireDefault(require("lodash/fp/getOr"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _layout = _interopRequireDefault(require("../layout"));
var _sidebar = _interopRequireDefault(require("../../../organism/sidebar"));
var _provider = _interopRequireDefault(require("../../../atom/provider"));
var _loading = _interopRequireDefault(require("../../app-player/loading"));
var _dashboardPopin = _interopRequireDefault(require("./dashboard-popin"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const currentDashboardSidebarSection = ({
  currentDashboard,
  onUpdateVersion,
  onUpdateField,
  inputParams
}) => {
  const dashboardDescription = {
    title: currentDashboard.name,
    type: 'info',
    value: currentDashboard.description
  };
  const dashboardVersion = {
    title: 'Version',
    type: 'select',
    name: 'version-field',
    onChange: onUpdateVersion,
    options: (0, _keys2.default)(currentDashboard.versions).map(v => ({
      name: v,
      value: v,
      selected: v === currentDashboard.currentVersion
    }))
  };
  const paramInputs = currentDashboard.schema.map(schema => ({
    title: schema,
    name: `${(0, _kebabCase2.default)(schema)}-field`,
    type: 'inputtext',
    onChange: newValue => onUpdateField(schema, newValue),
    value: (0, _getOr2.default)('', schema, inputParams)
  }));
  return [dashboardDescription, ...paramInputs, dashboardVersion];
};
const Dashboard = (props, context) => {
  const {
    url,
    error,
    selected
  } = props;
  const {
    skin,
    translate
  } = context;
  const body = () => {
    if (selected) {
      if (url) return /*#__PURE__*/_react.default.createElement("iframe", {
        src: url,
        className: _style.default.dashboardIframe,
        frameBorder: "0"
      });
      if (!error) return /*#__PURE__*/_react.default.createElement(_loading.default, null);
    } else if (!error) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.dashboardNoSelection,
        style: {
          color: (0, _getOr2.default)('#00B0FF', 'common.primary', skin)
        }
      }, translate('Select a dashboard in the Sidebar'));
    }
    return null;
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.dashboardSelection
  }, /*#__PURE__*/_react.default.createElement("h1", {
    className: _style.default.dashboardTitle
  }, (0, _getOr2.default)(translate('No Selected Dashboard'), 'name', selected)), body());
};
Dashboard.propTypes = process.env.NODE_ENV !== "production" ? {
  url: _propTypes.default.string,
  error: _propTypes.default.string,
  selected: _propTypes.default.shape({
    name: _propTypes.default.string
  })
} : {};
Dashboard.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
const ErrorPopin = ({
  onErrorRedirect,
  ctaLabel,
  error
}, {
  translate
}) => {
  return error ? /*#__PURE__*/_react.default.createElement(_dashboardPopin.default, {
    header: translate('An Error Occurred'),
    ctaLabel: ctaLabel,
    ctaOnClick: onErrorRedirect,
    closeOnClick: onErrorRedirect
  }, /*#__PURE__*/_react.default.createElement("p", null, error)) : null;
};
ErrorPopin.propTypes = process.env.NODE_ENV !== "production" ? {
  onErrorRedirect: _propTypes.default.func.isRequired,
  ctaLabel: _propTypes.default.string.isRequired,
  error: _propTypes.default.string
} : {};
ErrorPopin.contextTypes = {
  translate: _provider.default.childContextTypes.translate
};
const DashboardPreview = (props, context) => {
  const {
    dashboards = [],
    currentDashboard,
    onSelectDashboard = _noop2.default,
    onUpdateVersion = _noop2.default,
    onUpdateField = _noop2.default,
    onErrorRedirect = _noop2.default,
    inputParams = {},
    url,
    error
  } = props;
  const {
    translate
  } = context;
  if (!dashboards || dashboards.length === 0) {
    if (error) return /*#__PURE__*/_react.default.createElement(ErrorPopin, {
      ctaLabel: translate('Reload'),
      error: error,
      onErrorRedirect: onErrorRedirect
    });
    return /*#__PURE__*/_react.default.createElement(_loading.default, null);
  }
  const dashboardList = dashboards.map(dashboard => ({
    title: dashboard.name,
    name: `${(0, _kebabCase2.default)(dashboard.name)}-link`,
    type: 'link',
    href: dashboard.href,
    onClick: () => onSelectDashboard((0, _kebabCase2.default)(dashboard.name)),
    selected: dashboard.name === (0, _get2.default)('name', currentDashboard)
  }));
  const sidebarItems = [dashboardList];
  if (currentDashboard) {
    sidebarItems.push(currentDashboardSidebarSection({
      currentDashboard,
      onUpdateVersion,
      onUpdateField,
      inputParams
    }));
  }
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.container
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.dashboardAside
  }, /*#__PURE__*/_react.default.createElement(_sidebar.default, {
    items: sidebarItems
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.dashboardContent
  }, /*#__PURE__*/_react.default.createElement(Dashboard, {
    error: error,
    selected: currentDashboard,
    url: url
  }), /*#__PURE__*/_react.default.createElement(ErrorPopin, {
    ctaLabel: translate('Back to Dashboard Home'),
    error: error,
    onErrorRedirect: onErrorRedirect
  })));
};
DashboardPreview.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
DashboardPreview.propTypes = process.env.NODE_ENV !== "production" ? {
  dashboards: _propTypes.default.arrayOf(_propTypes.default.shape({
    name: _propTypes.default.string.isRequired,
    href: _propTypes.default.string,
    description: _propTypes.default.string.isRequired
  })),
  currentDashboard: _propTypes.default.shape({
    name: _propTypes.default.string.isRequired,
    description: _propTypes.default.string.isRequired,
    currentVersion: _propTypes.default.string.isRequired,
    versions: _propTypes.default.shape({}).isRequired,
    schema: _propTypes.default.arrayOf(_propTypes.default.string)
  }),
  url: _propTypes.default.string,
  error: _propTypes.default.string,
  onSelectDashboard: _propTypes.default.func,
  onUpdateVersion: _propTypes.default.func,
  onUpdateField: _propTypes.default.func,
  onErrorRedirect: _propTypes.default.func,
  inputParams: _propTypes.default.shape({})
} : {};
var _default = exports.default = (0, _layout.default)(DashboardPreview);
//# sourceMappingURL=index.js.map