import _keys from "lodash/fp/keys";
import _kebabCase from "lodash/fp/kebabCase";
import _getOr from "lodash/fp/getOr";
import _get from "lodash/fp/get";
import _noop from "lodash/fp/noop";
import React from 'react';
import PropTypes from 'prop-types';
import Layout from '../layout';
import Sidebar from '../../../organism/sidebar';
import Provider from '../../../atom/provider';
import Loader from '../../app-player/loading';
import DashboardPopin from './dashboard-popin';
import style from './style.css';
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: _keys(currentDashboard.versions).map(v => ({
      name: v,
      value: v,
      selected: v === currentDashboard.currentVersion
    }))
  };
  const paramInputs = currentDashboard.schema.map(schema => ({
    title: schema,
    name: `${_kebabCase(schema)}-field`,
    type: 'inputtext',
    onChange: newValue => onUpdateField(schema, newValue),
    value: _getOr('', 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.createElement("iframe", {
        src: url,
        className: style.dashboardIframe,
        frameBorder: "0"
      });
      if (!error) return /*#__PURE__*/React.createElement(Loader, null);
    } else if (!error) {
      return /*#__PURE__*/React.createElement("div", {
        className: style.dashboardNoSelection,
        style: {
          color: _getOr('#00B0FF', 'common.primary', skin)
        }
      }, translate('Select a dashboard in the Sidebar'));
    }
    return null;
  };
  return /*#__PURE__*/React.createElement("div", {
    className: style.dashboardSelection
  }, /*#__PURE__*/React.createElement("h1", {
    className: style.dashboardTitle
  }, _getOr(translate('No Selected Dashboard'), 'name', selected)), body());
};
Dashboard.propTypes = process.env.NODE_ENV !== "production" ? {
  url: PropTypes.string,
  error: PropTypes.string,
  selected: PropTypes.shape({
    name: PropTypes.string
  })
} : {};
Dashboard.contextTypes = {
  skin: Provider.childContextTypes.skin,
  translate: Provider.childContextTypes.translate
};
const ErrorPopin = ({
  onErrorRedirect,
  ctaLabel,
  error
}, {
  translate
}) => {
  return error ? /*#__PURE__*/React.createElement(DashboardPopin, {
    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 DashboardPreview = (props, context) => {
  const {
    dashboards = [],
    currentDashboard,
    onSelectDashboard = _noop,
    onUpdateVersion = _noop,
    onUpdateField = _noop,
    onErrorRedirect = _noop,
    inputParams = {},
    url,
    error
  } = 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);
  }
  const dashboardList = dashboards.map(dashboard => ({
    title: dashboard.name,
    name: `${_kebabCase(dashboard.name)}-link`,
    type: 'link',
    href: dashboard.href,
    onClick: () => onSelectDashboard(_kebabCase(dashboard.name)),
    selected: dashboard.name === _get('name', currentDashboard)
  }));
  const sidebarItems = [dashboardList];
  if (currentDashboard) {
    sidebarItems.push(currentDashboardSidebarSection({
      currentDashboard,
      onUpdateVersion,
      onUpdateField,
      inputParams
    }));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: style.container
  }, /*#__PURE__*/React.createElement("div", {
    className: style.dashboardAside
  }, /*#__PURE__*/React.createElement(Sidebar, {
    items: sidebarItems
  })), /*#__PURE__*/React.createElement("div", {
    className: style.dashboardContent
  }, /*#__PURE__*/React.createElement(Dashboard, {
    error: error,
    selected: currentDashboard,
    url: url
  }), /*#__PURE__*/React.createElement(ErrorPopin, {
    ctaLabel: translate('Back to Dashboard Home'),
    error: error,
    onErrorRedirect: onErrorRedirect
  })));
};
DashboardPreview.contextTypes = {
  skin: Provider.childContextTypes.skin,
  translate: Provider.childContextTypes.translate
};
DashboardPreview.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)
  }),
  url: PropTypes.string,
  error: PropTypes.string,
  onSelectDashboard: PropTypes.func,
  onUpdateVersion: PropTypes.func,
  onUpdateField: PropTypes.func,
  onErrorRedirect: PropTypes.func,
  inputParams: PropTypes.shape({})
} : {};
export default Layout(DashboardPreview);
//# sourceMappingURL=index.js.map