"use strict";

exports.__esModule = true;
exports.default = LoyaltyProgram;

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

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

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

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

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

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

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

var _LoyaltyProgramDialog = _interopRequireDefault(require("./LoyaltyProgramDialog"));

var _LoyaltyProgramDetail = _interopRequireDefault(require("../LoyaltyProgramDetail"));

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

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

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

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

const messages = (0, _reactIntl.defineMessages)({
  points: {
    id: 'ui.loyaltyProgram.points',
    defaultMessage: 'ui.loyaltyProgram.points'
  }
});
const PREFIX = 'SCLoyaltyProgram';
const classes = {
  root: `${PREFIX}-root`,
  title: `${PREFIX}-title`,
  cardHeader: `${PREFIX}-card-header`,
  pointsIcon: `${PREFIX}-pointsIcon`,
  actions: `${PREFIX}-actions`,
  points: `${PREFIX}-points`,
  pointsBox: `${PREFIX}-points-box`,
  discoverMore: `${PREFIX}-discover-more`
};
const Root = (0, _styles.styled)(_Widget.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  marginBottom: theme.spacing(2),
  [`& .${classes.cardHeader}`]: {
    display: 'flex',
    alignItems: 'start',
    flexWrap: 'wrap',
    marginTop: theme.spacing(2)
  },
  [`& .${classes.pointsIcon}`]: {
    backgroundColor: theme.palette.grey['A200'],
    padding: 10,
    '& .MuiSvgIcon-root ': {
      fontSize: '3rem',
      color: theme.palette.primary.main
    }
  },
  [`& .${classes.actions}`]: {
    display: 'flex',
    justifyContent: 'space-between'
  },
  [`& .${classes.pointsBox}`]: {
    backgroundColor: theme.palette.primary.main,
    padding: 10
  },
  [`& .${classes.points}`]: {
    color: theme.palette.common.white,
    fontSize: '1rem'
  }
}));

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

 #### Import

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

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCLoyaltyProgram-root|Styles applied to the root element.|
 |title.SCLoyaltyProgram-title|Styles applied to the title element.|
 |cardHeader|.SCLoyaltyProgram-card-header|Styles applied to the card header element.|
 |pointsIcon|.SCLoyaltyProgram-pointsIcon|Styles applied to the points icon element.|
 |actions|.SCLoyaltyProgram-actions|Styles applied to the actions section.|
 |points|.SCLoyaltyProgram-points|Styles applied to the points section.|
 |pointsBox|.SCLoyaltyProgram-points-box|Styles applied to the points box element.|
 |discoverMore|.SCLoyaltyProgram-discover-more|Styles applied to discover more button element.|
 *
 * @param inProps
 */
function LoyaltyProgram(inProps) {
  const props = (0, _useThemeProps.default)({
    props: inProps,
    name: PREFIX
  }); // PROPS

  const {
    autoHide,
    className,
    cardType
  } = props; // CONTEXT

  const scUserContext = (0, _react.useContext)(_core.SCUserContext);
  const scRoutingContext = (0, _core.useSCRouting)(); // STATE

  const [points, setPoints] = (0, _react.useState)(null);
  const [openLoyaltyProgramDialog, setOpenLoyaltyProgramDialog] = (0, _react.useState)(false); // INTL

  const intl = (0, _reactIntl.useIntl)();
  /**
   * Handles dialog close
   */

  const handleClose = () => {
    setOpenLoyaltyProgramDialog(false);
  };
  /**
   * Fetches user loyalty points
   */


  function fetchLP() {
    _core.http.request({
      url: _core.Endpoints.GetUserLoyaltyPoints.url({
        id: scUserContext.user['id']
      }),
      method: _core.Endpoints.GetUserLoyaltyPoints.method
    }).then(res => {
      setPoints(res.data.points);
    }).catch(error => {
      console.log(error);
    });
  }
  /**
   * On mount, fetches user loyalty points
   */


  (0, _react.useEffect)(() => {
    if (scUserContext.user) {
      fetchLP();
    }
  }, [scUserContext.user]);
  /**
   * Renders loyalty card
   */

  const l = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_CardContent.default, null, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    className: classes.title,
    variant: "h5"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgram.title",
    defaultMessage: "ui.loyaltyProgram.title"
  })), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    container: true,
    spacing: 2,
    className: classes.cardHeader
  }, /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true
  }, /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.pointsIcon
  }, /*#__PURE__*/_react.default.createElement(_Icon.default, null, "card_membership"))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: 12,
    sm: true,
    container: true
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    gutterBottom: true,
    variant: "h6"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgram.lp",
    defaultMessage: "ui.loyaltyProgram.lp"
  })), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2",
    color: "text.secondary"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgram.description",
    defaultMessage: "ui.loyaltyProgram.description"
  }))))), /*#__PURE__*/_react.default.createElement(_material.CardActions, {
    className: classes.actions
  }, /*#__PURE__*/_react.default.createElement(_material.Box, {
    component: "span",
    className: classes.pointsBox
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2",
    className: classes.points
  }, `${intl.formatMessage(messages.points, {
    total: points
  })}`)), cardType ? /*#__PURE__*/_react.default.createElement(_material.Button, {
    variant: "outlined",
    size: "small",
    className: classes.discoverMore,
    component: _core.Link,
    to: scRoutingContext.url(_core.SCRoutes.LOYALTY_ROUTE_NAME, {
      LoyaltyProgramDetail: _LoyaltyProgramDetail.default
    })
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgram.discover",
    defaultMessage: "ui.loyaltyProgram.discover"
  })) : /*#__PURE__*/_react.default.createElement(_material.Button, {
    variant: "outlined",
    size: "small",
    className: classes.discoverMore,
    onClick: () => setOpenLoyaltyProgramDialog(true)
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgram.discover",
    defaultMessage: "ui.loyaltyProgram.discover"
  }))), openLoyaltyProgramDialog && /*#__PURE__*/_react.default.createElement(_LoyaltyProgramDialog.default, {
    open: openLoyaltyProgramDialog,
    onClose: handleClose,
    points: points
  }));
  /**
   * Renders root object (if not hidden by autoHide prop)
   */


  if (!autoHide && scUserContext.user) {
    return /*#__PURE__*/_react.default.createElement(Root, _extends({}, props, {
      className: (0, _classnames.default)(classes.root, className)
    }), l);
  }

  return null;
}