"use strict";

exports.__esModule = true;
exports.default = Incubator;

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

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

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

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

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

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

var _CardContent = _interopRequireDefault(require("@mui/material/CardContent"));

var _Skeleton = _interopRequireDefault(require("./Skeleton"));

var _LinearProgress = _interopRequireDefault(require("@mui/material/LinearProgress"));

var _SubscribeButton = _interopRequireDefault(require("./SubscribeButton"));

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

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

const _excluded = ["incubatorId", "incubator", "className", "autoHide", "subscribeButtonProps", "ButtonProps", "detailView"];

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

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

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

const PREFIX = 'SCIncubator';
const classes = {
  root: `${PREFIX}-root`,
  name: `${PREFIX}-name`,
  slogan: `${PREFIX}-slogan`,
  author: `${PREFIX}-author`,
  progressBox: `${PREFIX}-progress-box`,
  progressBar: `${PREFIX}-progress-bar`
};
const Root = (0, _styles.styled)(_Widget.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  [`& .${classes.name}`]: {
    display: 'flex',
    padding: 0
  },
  [`& .${classes.progressBar}`]: {
    marginTop: 8,
    height: '25px'
  },
  [`& .${classes.slogan}`]: {
    whiteSpace: 'nowrap',
    overflow: 'hidden',
    textOverflow: 'ellipsis'
  },
  [`& .${classes.author}`]: {
    marginLeft: 2,
    marginRight: 2
  }
}));

function LinearProgressWithLabel(props) {
  return /*#__PURE__*/_react.default.createElement(_material.Box, {
    sx: {
      position: 'relative'
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Box, {
    sx: {
      width: '100%',
      mr: 1
    }
  }, /*#__PURE__*/_react.default.createElement(_LinearProgress.default, _extends({
    variant: "determinate"
  }, props))), /*#__PURE__*/_react.default.createElement(_material.Box, {
    sx: {
      position: 'absolute',
      top: '3px',
      left: props.value + 2 + '%',
      transform: 'translateX(-50%)'
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2",
    color: "text.secondary"
  }, !props.subscribed ? null : props.subscribed)));
}

/**
 * > API documentation for the Community-UI Incubator component. Learn about the available props and the CSS API.
 *
 * #### Import
 ```jsx
 import {Incubator} from '@selfcommunity/ui';
 ```
 #### Component Name
 The name `SCIncubator` can be used when providing style overrides in the theme.

 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCIncubator-root|Styles applied to the root element.|
 |name|.SCIncubator-name|Styles applied to the name section.|
 |slogan|.SCIncubator-slogan|Styles applied to the slogan section.|
 |author|.SCIncubator-author|Styles applied to the author element.|
 |progressBox|.SCIncubator-progress-box|Styles applied to the progress box element.|
 |progressBar|.SCIncubator-progress-bar|Styles applied to the progress bar element.|

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

  const {
    incubatorId = null,
    incubator = null,
    className = null,
    autoHide = false,
    subscribeButtonProps = {},
    ButtonProps = {},
    detailView = false
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const {
    scIncubator,
    setSCIncubator
  } = (0, _core.useSCFetchIncubator)({
    id: incubatorId,
    incubator
  }); // CONTEXT

  const scContext = (0, _core.useSCContext)();
  const scRoutingContext = (0, _core.useSCRouting)();
  /**
   * Renders total votes
   */

  function renderVotes(voteCount, totalVotes) {
    if (totalVotes === 0) {
      return 0;
    }

    return 100 * voteCount / totalVotes;
  }
  /**
   * Renders Incubator object
   */


  const i = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, scIncubator ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_CardContent.default, null, /*#__PURE__*/_react.default.createElement(_material.Button, _extends({
    variant: "text",
    className: classes.name
  }, ButtonProps), scIncubator.name), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    component: 'span'
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    defaultMessage: "ui.incubator.proposedBy",
    id: "ui.incubator.proposedBy"
  }), /*#__PURE__*/_react.default.createElement(_core.Link, {
    className: classes.author,
    to: scRoutingContext.url(_core.SCRoutes.USER_PROFILE_ROUTE_NAME, scIncubator.user)
  }, scIncubator.user.username)), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    component: 'p',
    className: !detailView ? classes.slogan : null
  }, scIncubator.slogan), /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.progressBox
  }, /*#__PURE__*/_react.default.createElement(LinearProgressWithLabel, {
    className: classes.progressBar,
    value: renderVotes(scIncubator.subscribers_count, scIncubator.subscribers_threshold),
    subscribed: scIncubator.subscribers_count
  }), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    container: true,
    spacing: 3
  }, /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: true
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    defaultMessage: "ui.incubator.progressBar.proposal",
    id: "ui.incubator.progressBar.proposal"
  }))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: 6
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    sx: {
      display: 'flex',
      justifyContent: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    defaultMessage: "ui.incubator.progressBar.collectingSubscribers",
    id: "ui.incubator.progressBar.collectingSubscribers"
  }))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: true
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    align: "right"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    defaultMessage: "ui.incubator.progressBar.approved",
    id: "ui.incubator.progressBar.approved"
  }))))), /*#__PURE__*/_react.default.createElement(_SubscribeButton.default, _extends({
    incubator: scIncubator
  }, subscribeButtonProps)))) : /*#__PURE__*/_react.default.createElement(_Skeleton.default, {
    elevation: 0
  }));
  /**
   * Renders root object (if not hidden by autoHide prop)
   */


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

  return null;
}