"use strict";

exports.__esModule = true;
exports.default = Platform;

var _react = _interopRequireWildcard(require("react"));

var _styles = require("@mui/material/styles");

var _material = require("@mui/material");

var _core = require("@selfcommunity/core");

var _Icon = _interopRequireDefault(require("@mui/material/Icon"));

var _reactIntl = require("react-intl");

var _classnames = _interopRequireDefault(require("classnames"));

var _Widget = _interopRequireDefault(require("../Widget"));

var _useThemeProps = _interopRequireDefault(require("@mui/material/styles/useThemeProps"));

const _excluded = ["autoHide", "className"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const PREFIX = 'SCPlatform';
const classes = {
  root: `${PREFIX}-root`,
  title: `${PREFIX}-title`
};
const Root = (0, _styles.styled)(_Widget.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  marginBottom: theme.spacing(2),
  padding: 20
}));

/**
 * > API documentation for the Community-UI Platform component. Learn about the available props and the CSS API.

 #### Import

 ```jsx
 import {Platform} from '@selfcommunity/ui';
 ```

 #### Component Name

 The name `SCPlatform` can be used when providing style overrides in the theme.


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCPlatform-root|Styles applied to the root element.|
 |title|.SCPlatform-title|Styles applied to the title element.|

 *
 * @param inProps
 */
function Platform(inProps) {
  // PROPS
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  });

  const {
    autoHide,
    className
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // CONTEXT


  const scUserContext = (0, _react.useContext)(_core.SCUserContext);
  const scLocaleContext = (0, _core.useSCLocale)(); // CONST

  const language = scLocaleContext.locale;
  const roles = scUserContext.user && scUserContext.user.role;
  const isAdmin = roles && roles.includes('admin');
  const isModerator = roles && roles.includes('moderator');
  /**
   * Fetches platform url
   */

  function fetchPlatform(query) {
    _core.http.request({
      url: _core.Endpoints.Platform.url(),
      method: _core.Endpoints.Platform.method,
      params: {
        next: query
      }
    }).then(res => {
      const platformUrl = res.data.platform_url;
      window.open(platformUrl, '_blank').focus();
    }).catch(error => {
      console.log(error);
    });
  }
  /**
   * Renders platform card
   */


  const c = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.Grid, {
    container: true,
    spacing: isAdmin ? 1 : 3,
    justifyContent: "center"
  }, /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: 12
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    className: classes.title,
    component: "h3",
    align: "center"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.platformAccess.title",
    defaultMessage: "ui.platformAccess.title"
  }), /*#__PURE__*/_react.default.createElement(_Icon.default, {
    fontSize: "small"
  }, "lock"))), isAdmin && /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: "auto",
    style: {
      textAlign: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Button, {
    variant: "outlined",
    size: "small",
    onClick: () => fetchPlatform('')
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.platformAccess.adm",
    defaultMessage: "ui.platformAccess.adm"
  }))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: "auto",
    style: {
      textAlign: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Button, {
    variant: "outlined",
    size: "small",
    onClick: () => fetchPlatform('')
  }, isAdmin || isModerator ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.platformAccess.mod",
    defaultMessage: "ui.platformAccess.mod"
  }) : /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.platformAccess.edt",
    defaultMessage: "ui.platformAccess.edt"
  }))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: "auto",
    style: {
      textAlign: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Button, {
    variant: "outlined",
    size: "small",
    href: `https://support.selfcommunity.com/hc/${language}`,
    target: "_blank"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.platformAccess.hc",
    defaultMessage: "ui.platformAccess.hc"
  })))));
  /**
   * Renders root object (if not hidden by autoHide prop)
   */


  if (!autoHide && roles !== null) {
    return /*#__PURE__*/_react.default.createElement(Root, _extends({
      className: (0, _classnames.default)(classes.root, className)
    }, rest), c);
  }

  return null;
}