import _has from "lodash/fp/has";
import _getOr from "lodash/fp/getOr";
import _noop from "lodash/fp/noop";
import React from 'react';
import PropTypes from 'prop-types';
import Sidebar from '../sidebar';
import Provider from '../../atom/provider';
import Loader from '../../template/app-player/loading';
import AnalyticsPopin from './analytics-popin';
import style from './style.css';
const Analytics = (props, context) => {
  const {
    url,
    error,
    selected
  } = props;
  const {
    skin,
    translate
  } = context;
  const body = () => {
    if (selected) {
      if (url) return /*#__PURE__*/React.createElement("iframe", {
        src: url,
        className: style.dashboardIframe,
        frameBorder: "0"
      });
      if (!error) return /*#__PURE__*/React.createElement(Loader, null);
    }
    if (error) return null;
    return /*#__PURE__*/React.createElement("div", {
      className: style.dashboardNoSelection,
      style: {
        color: _getOr('#00B0FF', 'common.primary', skin)
      }
    }, translate('Select a dashboard in the Sidebar'));
  };
  return /*#__PURE__*/React.createElement("div", {
    className: style.dashboardSelection
  }, _has('name', selected) ? null : /*#__PURE__*/React.createElement("h1", {
    className: style.noSelectedDashboard
  }, translate('No Selected Dashboard')), body());
};
Analytics.propTypes = process.env.NODE_ENV !== "production" ? {
  url: PropTypes.string,
  error: PropTypes.string,
  selected: PropTypes.shape({
    name: PropTypes.string
  })
} : {};
Analytics.contextTypes = {
  skin: Provider.childContextTypes.skin,
  translate: Provider.childContextTypes.translate
};
const ErrorPopin = ({
  onErrorRedirect,
  ctaLabel,
  error
}, {
  translate
}) => {
  return error ? /*#__PURE__*/React.createElement(AnalyticsPopin, {
    header: translate('An Error Occurred'),
    ctaLabel: ctaLabel,
    ctaOnClick: onErrorRedirect,
    closeOnClick: onErrorRedirect
  }, /*#__PURE__*/React.createElement("p", null, error)) : null;
};
ErrorPopin.propTypes = process.env.NODE_ENV !== "production" ? {
  onErrorRedirect: PropTypes.func.isRequired,
  ctaLabel: PropTypes.string.isRequired,
  error: PropTypes.string
} : {};
ErrorPopin.contextTypes = {
  translate: Provider.childContextTypes.translate
};
const BrandAnalytics = (props, context) => {
  const {
    dashboards = [],
    currentDashboard,
    onErrorRedirect = _noop,
    url,
    error,
    sidebarItems
  } = props;
  const {
    translate
  } = context;
  if (!dashboards || dashboards.length === 0) {
    if (error) return /*#__PURE__*/React.createElement(ErrorPopin, {
      ctaLabel: translate('Reload'),
      error: error,
      onErrorRedirect: onErrorRedirect
    });
    return /*#__PURE__*/React.createElement(Loader, null);
  }
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Sidebar, {
    items: sidebarItems
  }), /*#__PURE__*/React.createElement("div", {
    className: style.dashboardContent
  }, /*#__PURE__*/React.createElement(Analytics, {
    error: error,
    selected: currentDashboard,
    url: url
  }), /*#__PURE__*/React.createElement(ErrorPopin, {
    ctaLabel: translate('Back to Dashboard Home'),
    error: error,
    onErrorRedirect: onErrorRedirect
  })));
};
BrandAnalytics.contextTypes = {
  skin: Provider.childContextTypes.skin,
  translate: Provider.childContextTypes.translate
};
BrandAnalytics.propTypes = process.env.NODE_ENV !== "production" ? {
  dashboards: PropTypes.arrayOf(PropTypes.shape({
    name: PropTypes.string.isRequired,
    href: PropTypes.string,
    description: PropTypes.string.isRequired
  })),
  currentDashboard: PropTypes.shape({
    name: PropTypes.string.isRequired,
    description: PropTypes.string.isRequired,
    currentVersion: PropTypes.string.isRequired,
    versions: PropTypes.shape({}).isRequired,
    schema: PropTypes.arrayOf(PropTypes.string)
  }),
  sidebarItems: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.shape({
    title: PropTypes.string.isRequired,
    type: PropTypes.string.isRequired,
    value: PropTypes.string.isRequired
  }), PropTypes.shape({
    title: PropTypes.string.isRequired,
    type: PropTypes.string.isRequired,
    name: PropTypes.string,
    onChange: PropTypes.func.isRequired,
    option: PropTypes.bool.isRequired
  }), PropTypes.shape({
    title: PropTypes.string.isRequired,
    type: PropTypes.string.isRequired,
    name: PropTypes.string,
    value: PropTypes.string,
    onChange: PropTypes.func
  })])),
  url: PropTypes.string,
  error: PropTypes.string,
  onErrorRedirect: PropTypes.func,
  inputParams: PropTypes.shape({})
} : {};
export default BrandAnalytics;
//# sourceMappingURL=index.js.map