"use strict";

exports.__esModule = true;
exports.default = LoyaltyProgramDetail;

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

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

var _Card = _interopRequireDefault(require("@mui/material/Card"));

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

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

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

var _Chip = _interopRequireDefault(require("@mui/material/Chip"));

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

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

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

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

const _excluded = ["className", "autoHide", "points", "cardType", "requestable"];

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 messages = (0, _reactIntl.defineMessages)({
  points: {
    id: 'ui.loyaltyProgramDetail.points',
    defaultMessage: 'ui.loyaltyProgramDetail.points'
  },
  userPoints: {
    id: 'ui.loyaltyProgramDetail.userPoints',
    defaultMessage: 'ui.loyaltyProgramDetail.userPoints'
  }
});
const PREFIX = 'SCLoyaltyProgramDetail';
const classes = {
  root: `${PREFIX}-root`,
  header: `${PREFIX}-header`,
  intro: `${PREFIX}-intro`,
  icon: `${PREFIX}-icon`,
  card: `${PREFIX}-card`,
  title: `${PREFIX}-title`,
  points: `${PREFIX}-points`,
  description: `${PREFIX}-description`,
  action: `${PREFIX}-action`,
  chip: `${PREFIX}-chip`
};
const Root = (0, _styles.styled)(_Card.default, {
  name: PREFIX,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  minWidth: 500,
  margin: 2,
  [`& .${classes.header}`]: {
    backgroundColor: theme.palette.grey['A200'],
    marginBottom: '20px'
  },
  [`& .${classes.intro}`]: {
    display: 'flex',
    alignItems: 'center',
    marginBottom: '20px'
  },
  [`& .${classes.points}`]: {
    marginLeft: '16px'
  },
  [`& .${classes.title}`]: {
    fontWeight: 'bold'
  },
  [`& .${classes.icon}`]: {
    backgroundColor: theme.palette.grey['A200'],
    padding: 5,
    borderRadius: '5px',
    '& .MuiSvgIcon-root ': {
      fontSize: '2rem'
    }
  },
  [`& .${classes.card}`]: {
    height: '100%',
    flexDirection: 'column',
    justifyContent: 'space-between'
  },
  [`& .${classes.action}`]: {
    pointerEvents: 'all',
    cursor: 'not-allowed'
  },
  [`& .${classes.chip}`]: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center'
  }
}));

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

  const {
    className,
    autoHide,
    points,
    cardType,
    requestable
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const [prizes, setPrizes] = (0, _react.useState)([]); // CONTEXT

  const scUserContext = (0, _react.useContext)(_core.SCUserContext); // INTL

  const intl = (0, _reactIntl.useIntl)();
  /**
   * Fetches the list of available prizes
   */

  function fetchPrizes() {
    _core.http.request({
      url: _core.Endpoints.GetPrizes.url(null),
      method: _core.Endpoints.GetPrizes.method
    }).then(res => {
      setPrizes(res.data.results);
    }).catch(error => {
      console.log(error);
    });
  }
  /**
   * On mount, fetches prizes
   */


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

  const d = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, cardType && /*#__PURE__*/_react.default.createElement(_material.Typography, {
    component: "h3",
    align: "left",
    className: classes.header
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgram.lp",
    defaultMessage: "ui.loyaltyProgram.lp"
  })), /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.intro
  }, /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.icon
  }, /*#__PURE__*/_react.default.createElement(_Icon.default, null, "card_giftcard")), points && /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.points
  }, /*#__PURE__*/_react.default.createElement(_Chip.default, {
    label: `${intl.formatMessage(messages.userPoints, {
      total: points
    })}`
  }))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    container: true,
    spacing: 2
  }, /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: 12
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "h6"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgramDetail.community",
    defaultMessage: "ui.loyaltyProgramDetail.community"
  })), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    component: "div"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgramDetail.description",
    defaultMessage: "ui.loyaltyProgramDetail.description"
  }))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: 12
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgramDetail.listTitle",
    defaultMessage: "ui.loyaltyProgramDetail.listTitle"
  })), /*#__PURE__*/_react.default.createElement("ul", {
    style: {
      columnCount: 2
    }
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgramDetail.list",
    defaultMessage: "ui.loyaltyProgramDetail.list"
  }))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: 12,
    sx: {
      mb: 2
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "h6"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgramDetail.prizes",
    defaultMessage: "ui.loyaltyProgramDetail.prizes"
  })), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    component: "div"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgramDetail.prizesIntro",
    defaultMessage: "ui.loyaltyProgramDetail.prizesIntro"
  })), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    component: "div"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgramDetail.prizesContent",
    defaultMessage: "ui.loyaltyProgramDetail.prizesContent"
  })))), /*#__PURE__*/_react.default.createElement(_material.Grid, {
    container: true,
    spacing: 2,
    direction: "row"
  }, prizes.map((prize, index) => /*#__PURE__*/_react.default.createElement(_material.Grid, {
    item: true,
    xs: 12,
    sm: 6,
    md: 4,
    key: index
  }, /*#__PURE__*/_react.default.createElement(_Card.default, {
    className: classes.card
  }, /*#__PURE__*/_react.default.createElement(_material.CardMedia, {
    component: "img",
    height: "140",
    image: prize.image
  }), /*#__PURE__*/_react.default.createElement(_material.Box, {
    className: classes.chip
  }, /*#__PURE__*/_react.default.createElement(_Chip.default, {
    label: `${intl.formatMessage(messages.points, {
      total: prize.points
    })}`
  })), /*#__PURE__*/_react.default.createElement(_material.CardContent, null, /*#__PURE__*/_react.default.createElement(_material.Typography, {
    gutterBottom: true,
    variant: "body1",
    component: "div",
    className: classes.title
  }, prize.title), /*#__PURE__*/_react.default.createElement(_material.Typography, {
    variant: "body2",
    className: classes.description
  }, prize.description)), /*#__PURE__*/_react.default.createElement(_material.CardActions, {
    sx: {
      justifyContent: 'center'
    }
  }, /*#__PURE__*/_react.default.createElement(_material.Button, {
    size: "small",
    variant: "outlined",
    disabled: requestable,
    className: classes.action
  }, points >= prize.points ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgramDetail.button.request",
    defaultMessage: "ui.loyaltyProgramDetail.button.request"
  }) : /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "ui.loyaltyProgram.discover",
    defaultMessage: "ui.loyaltyProgram.discover"
  }))))))));

  if (!prizes) {
    return /*#__PURE__*/_react.default.createElement(_Skeleton.default, null);
  }
  /**
   * Renders the component (if not hidden by autoHide prop)
   */


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

  return null;
}