"use strict";

exports.__esModule = true;
exports.default = void 0;
var _has2 = _interopRequireDefault(require("lodash/fp/has"));
var _getOr2 = _interopRequireDefault(require("lodash/fp/getOr"));
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _sidebar = _interopRequireDefault(require("../sidebar"));
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _loading = _interopRequireDefault(require("../../template/app-player/loading"));
var _analyticsPopin = _interopRequireDefault(require("./analytics-popin"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const Analytics = (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);
    }
    if (error) return null;
    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 /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.dashboardSelection
  }, (0, _has2.default)('name', selected) ? null : /*#__PURE__*/_react.default.createElement("h1", {
    className: _style.default.noSelectedDashboard
  }, translate('No Selected Dashboard')), body());
};
Analytics.propTypes = process.env.NODE_ENV !== "production" ? {
  url: _propTypes.default.string,
  error: _propTypes.default.string,
  selected: _propTypes.default.shape({
    name: _propTypes.default.string
  })
} : {};
Analytics.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
const ErrorPopin = ({
  onErrorRedirect,
  ctaLabel,
  error
}, {
  translate
}) => {
  return error ? /*#__PURE__*/_react.default.createElement(_analyticsPopin.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 BrandAnalytics = (props, context) => {
  const {
    dashboards = [],
    currentDashboard,
    onErrorRedirect = _noop2.default,
    url,
    error,
    sidebarItems
  } = 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);
  }
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_sidebar.default, {
    items: sidebarItems
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.dashboardContent
  }, /*#__PURE__*/_react.default.createElement(Analytics, {
    error: error,
    selected: currentDashboard,
    url: url
  }), /*#__PURE__*/_react.default.createElement(ErrorPopin, {
    ctaLabel: translate('Back to Dashboard Home'),
    error: error,
    onErrorRedirect: onErrorRedirect
  })));
};
BrandAnalytics.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
BrandAnalytics.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)
  }),
  sidebarItems: _propTypes.default.arrayOf(_propTypes.default.oneOfType([_propTypes.default.shape({
    title: _propTypes.default.string.isRequired,
    type: _propTypes.default.string.isRequired,
    value: _propTypes.default.string.isRequired
  }), _propTypes.default.shape({
    title: _propTypes.default.string.isRequired,
    type: _propTypes.default.string.isRequired,
    name: _propTypes.default.string,
    onChange: _propTypes.default.func.isRequired,
    option: _propTypes.default.bool.isRequired
  }), _propTypes.default.shape({
    title: _propTypes.default.string.isRequired,
    type: _propTypes.default.string.isRequired,
    name: _propTypes.default.string,
    value: _propTypes.default.string,
    onChange: _propTypes.default.func
  })])),
  url: _propTypes.default.string,
  error: _propTypes.default.string,
  onErrorRedirect: _propTypes.default.func,
  inputParams: _propTypes.default.shape({})
} : {};
var _default = exports.default = BrandAnalytics;
//# sourceMappingURL=index.js.map