"use strict";

exports.__esModule = true;
exports.default = FriendshipUserButton;

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

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

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

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

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

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

const _excluded = ["className", "userId", "user", "connected"];

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 _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 = 'SCFriendshipUserButton';
const classes = {
  root: `${PREFIX}-root`
};
const Root = (0, _styles.styled)(_material.Button, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  border: '0px',
  color: 'black',
  borderRadius: 20,
  backgroundColor: '#e2e2e2',
  paddingTop: '4px',
  paddingRight: '16px',
  paddingBottom: '4px',
  paddingLeft: '16px'
}));

// TODO: fix component

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

 #### Import

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

 #### Component Name

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


 #### CSS

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

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

  const {
    className,
    userId,
    user,
    connected
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const [status, setStatus] = (0, _react.useState)(connected ? 'Remove' : 'Connect');
  const {
    scUser,
    setSCUser
  } = (0, _core.useSCFetchUser)({
    id: userId,
    user
  }); // CONTEXT

  const scContext = (0, _core.useSCContext)();
  const scUserContext = (0, _react.useContext)(_core.SCUserContext);

  function updateStatus(status) {
    if (status === 'Connect') {
      setStatus('Remove');
    } else {
      setStatus('Connect');
    }
  }

  function requestConnection() {
    updateStatus(status);

    _core.http.request({
      url: _core.Endpoints.RequestConnection.url({
        id: user.id
      }),
      method: _core.Endpoints.RequestConnection.method
    }).then(res => {
      console.log(res);
    }).catch(error => {
      console.log(error);
    });
  }

  function cancelConnectionRequest() {
    console.log('connection');

    _core.http.request({
      url: _core.Endpoints.CancelConnectionRequest.url({
        id: user.id
      }),
      method: _core.Endpoints.CancelConnectionRequest.method
    }).then(res => {
      console.log(res);
    }).catch(error => {
      console.log(error);
    });
  }

  function removeConnection() {
    _core.http.request({
      url: _core.Endpoints.RemoveConnection.url({
        id: user.id
      }),
      method: _core.Endpoints.RemoveConnection.method
    }).then(res => {
      console.log(res);
    }).catch(error => {
      console.log(error);
    });
  }

  function handleConnectionStatus() {
    if (!scUserContext.user) {
      scContext.settings.handleAnonymousAction();
    } else {
      connected ? removeConnection() : requestConnection();
    }
  } // same user


  if (scUserContext.user && scUserContext.user.id === scUser.id) {
    return null;
  }

  return /*#__PURE__*/_react.default.createElement(Root, {
    size: "small",
    variant: "outlined",
    onClick: () => handleConnectionStatus(),
    className: (0, _classnames.default)(classes.root, className)
  }, status);
}