"use strict";

exports.__esModule = true;
exports.default = InlineComposer;

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

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

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

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

var _Media = require("../../shared/Media");

var _Composer = _interopRequireWildcard(require("../Composer"));

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

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

var _notistack = require("notistack");

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

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

const _excluded = ["mediaObjectTypes", "defaultValue", "onSuccess"];

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 = 'SCInlineComposer';
const classes = {
  root: `${PREFIX}-root`,
  content: `${PREFIX}-content`,
  input: `${PREFIX}-input`,
  actions: `${PREFIX}-actions`,
  avatar: `${PREFIX}-avatar`
};
const Root = (0, _styles.styled)(_Widget.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  marginBottom: theme.spacing(2),
  [`& .${classes.content}`]: {
    padding: theme.spacing(),
    display: 'flex',
    flexDirection: 'row',
    justifyContent: 'space-between',
    [`& .${classes.input}`]: {
      flexGrow: 2
    },
    [`& .${classes.input} .MuiButton-text`]: {
      justifyContent: 'flex-start',
      textTransform: 'none'
    }
  }
}));
const PREFERENCES = [_core.SCPreferences.CONFIGURATIONS_POST_TYPE_ENABLED, _core.SCPreferences.CONFIGURATIONS_DISCUSSION_TYPE_ENABLED, _core.SCPreferences.ADDONS_POST_GEOLOCATION_ENABLED, _core.SCPreferences.ADDONS_POLLS_ENABLED, _core.SCPreferences.ADDONS_VIDEO_UPLOAD_ENABLED];
const INITIAL_STATE = {
  open: false,
  view: null
};
/**
 > API documentation for the Community-UI Inline Composer component. Learn about the available props and the CSS API.
 *
 #### Import
 ```jsx
 import {InlineComposer} from '@selfcommunity/ui';
 ```
 #### Component Name
 The name `SCInlineComposer` can be used when providing style overrides in the theme.

 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCInlineComposer-root|Styles applied to the root element.|
 |content|.SCInlineComposer-content|Styles applied to the content element.|
 |input|.SCInlineComposer-input|Styles applied to the input element.|
 |actions|.SCInlineComposer-actions|Styles applied to the actions section.|
 |avatar|.SCInlineComposer-avatar|Styles applied to the avatar element.|


 * @param inProps
 */

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

  const {
    mediaObjectTypes = [_Media.Image, _Media.Document, _Media.Link],
    defaultValue,
    onSuccess = null
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // Context


  const scContext = (0, _react.useContext)(_core.SCContext);
  const scPreferencesContext = (0, _react.useContext)(_core.SCPreferencesContext);
  const scUserContext = (0, _react.useContext)(_core.SCUserContext);
  const {
    enqueueSnackbar
  } = (0, _notistack.useSnackbar)(); // State variables

  const [state, setState] = (0, _react.useState)(Object.assign({}, INITIAL_STATE));
  const {
    open,
    view
  } = state;
  /*
   * Compute preferences
   */

  const preferences = (0, _react.useMemo)(() => {
    const _preferences = {};
    PREFERENCES.map(p => _preferences[p] = p in scPreferencesContext.preferences ? scPreferencesContext.preferences[p].value : null);
    return _preferences;
  }, [scPreferencesContext.preferences]); // Handlers

  const handleOpen = view => {
    return () => {
      if (scUserContext.user) {
        if (_core.UserUtils.isBlocked(scUserContext.user)) {
          enqueueSnackbar( /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
            id: "ui.common.userBlocked",
            defaultMessage: "ui.common.userBlocked"
          }), {
            variant: 'warning',
            autoHideDuration: 3000
          });
        } else {
          setState({
            view,
            open: true
          });
        }
      } else {
        scContext.settings.handleAnonymousAction();
      }
    };
  };

  const handleClose = () => {
    setState(Object.assign({}, INITIAL_STATE));
  };

  const handleSuccess = feedObject => {
    if (onSuccess) {
      onSuccess(feedObject);
    }

    enqueueSnackbar( /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "ui.inlineComposer.success",
      defaultMessage: "ui.inlineComposer.success"
    }), {
      variant: 'success',
      autoHideDuration: 3000
    });
    setState(Object.assign({}, INITIAL_STATE));
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(Root, _extends({
    className: classes.root
  }, rest), /*#__PURE__*/_react.default.createElement(_material.CardContent, {
    className: classes.content
  }, /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.input
  }, /*#__PURE__*/_react.default.createElement(_material.Button, {
    variant: "text",
    disableFocusRipple: true,
    disableRipple: true,
    disableElevation: true,
    onClick: handleOpen(_Composer.MAIN_VIEW),
    fullWidth: true,
    color: "inherit"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.inlineComposer.label",
    defaultMessage: "ui.inlineComposer.label"
  }))), /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.actions
  }, mediaObjectTypes.filter(mediaObjectType => mediaObjectType.editButton !== null).map(mediaObjectType => /*#__PURE__*/_react.default.createElement(mediaObjectType.editButton, {
    key: mediaObjectType.name,
    onClick: handleOpen(mediaObjectType.name)
  })), preferences[_core.SCPreferences.ADDONS_POLLS_ENABLED] && /*#__PURE__*/_react.default.createElement(_material.IconButton, {
    onClick: handleOpen(_Composer.POLL_VIEW)
  }, /*#__PURE__*/_react.default.createElement(_Icon.default, null, "bar_chart"))), /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.avatar
  }, !scUserContext.user ? /*#__PURE__*/_react.default.createElement(_material.Avatar, {
    variant: "circular"
  }) : /*#__PURE__*/_react.default.createElement(_material.Avatar, {
    alt: scUserContext.user.username,
    variant: "circular",
    src: scUserContext.user.avatar
  })))), /*#__PURE__*/_react.default.createElement(_Composer.default, {
    open: open,
    view: view,
    mediaObjectTypes: mediaObjectTypes,
    defaultValue: defaultValue,
    maxWidth: "sm",
    fullWidth: true,
    scroll: "body",
    onClose: handleClose,
    onSuccess: handleSuccess
  }));
}