"use strict";

exports.__esModule = true;
exports.default = Category;

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

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

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

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

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

var _FollowCategoryButton = _interopRequireDefault(require("../FollowCategoryButton"));

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

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

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

var _BaseItemButton = _interopRequireDefault(require("../../shared/BaseItemButton"));

const _excluded = ["categoryId", "category", "className", "elevation", "autoHide", "followCategoryButtonProps", "showFollowers"];

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

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 messages = (0, _reactIntl.defineMessages)({
  categoryFollowers: {
    id: 'ui.category.categoryFollowers',
    defaultMessage: 'ui.category.categoryFollowers'
  }
});
const PREFIX = 'SCCategory';
const classes = {
  root: `${PREFIX}-root`,
  categoryImage: `${PREFIX}-category-image`,
  title: `${PREFIX}-title`,
  actions: `${PREFIX}-actions`
};
const Root = (0, _styles.styled)(_BaseItemButton.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({}));

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

 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCCategory-root|Styles applied to the root element.|
 |categoryImage|.SCCategory-category-image|Styles applied to category image element.|
 |title|.SCCategory-title|Styles applied to the title element.|
 |actions|.SCCategory-actions|Styles applied to action section.|

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

  const {
    categoryId = null,
    category = null,
    className = null,
    elevation,
    autoHide = false,
    followCategoryButtonProps = {},
    showFollowers = true
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // CONTEXT


  const scRoutingContext = (0, _core.useSCRouting)(); // STATE

  const {
    scCategory,
    setSCCategory
  } = (0, _core.useSCFetchCategory)({
    id: categoryId,
    category
  }); // INTL

  const intl = (0, _reactIntl.useIntl)();

  if (!scCategory) {
    return /*#__PURE__*/_react.default.createElement(_Skeleton.default, {
      elevation: elevation
    });
  } // RENDER


  if (!autoHide) {
    return /*#__PURE__*/_react.default.createElement(Root, _extends({
      elevation: elevation
    }, rest, {
      className: (0, _classnames.default)(classes.root, className),
      ButtonBaseProps: {
        component: _core.Link,
        to: scRoutingContext.url(_core.SCRoutes.CATEGORY_ROUTE_NAME, scCategory)
      },
      image: /*#__PURE__*/_react.default.createElement(_material.Avatar, {
        alt: scCategory.name,
        src: scCategory.image_medium,
        variant: "square",
        className: classes.categoryImage
      }),
      primary: scCategory.name,
      secondary: showFollowers ? `${intl.formatMessage(messages.categoryFollowers, {
        total: scCategory.followers_counter
      })}` : scCategory.slogan,
      actions: /*#__PURE__*/_react.default.createElement(_FollowCategoryButton.default, _extends({
        category: scCategory
      }, followCategoryButtonProps))
    }));
  }

  return null;
}