"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styles_1 = require("@mui/material/styles");
const CardContent_1 = tslib_1.__importDefault(require("@mui/material/CardContent"));
const material_1 = require("@mui/material");
const api_services_1 = require("@selfcommunity/api-services");
const react_core_1 = require("@selfcommunity/react-core");
const react_intl_1 = require("react-intl");
const react_core_2 = require("@selfcommunity/react-core");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const Widget_1 = tslib_1.__importDefault(require("../Widget"));
const system_1 = require("@mui/system");
const HiddenPlaceholder_1 = tslib_1.__importDefault(require("../../shared/HiddenPlaceholder"));
const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
const Errors_1 = require("../../constants/Errors");
const utils_1 = require("@selfcommunity/utils");
const constants_1 = require("./constants");
const classes = {
    root: `${constants_1.PREFIX}-root`,
    title: `${constants_1.PREFIX}-title`,
    actions: `${constants_1.PREFIX}-actions`,
    points: `${constants_1.PREFIX}-points`,
    discoverMore: `${constants_1.PREFIX}-discover-more`
};
const Root = (0, styles_1.styled)(Widget_1.default, {
    name: constants_1.PREFIX,
    slot: 'Root'
})(() => ({}));
/**
 * > API documentation for the Community-JS Loyalty Program Widget component. Learn about the available props and the CSS API.
 *
 *
 * This component renders a widget containing the points earn by the user with the loyalty program.
 * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/LoyaltyProgram)

 #### Import

 ```jsx
 import {LoyaltyProgramWidget} from '@selfcommunity/react-ui';
 ```

 #### Component Name

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


 #### CSS

 |Rule Name|Global class|Description|
 |---|---|---|
 |root|.SCLoyaltyProgramWidget-root|Styles applied to the root element.|
 |title.SCLoyaltyProgramWidget-title|Styles applied to the title element.|
 |actions|.SCLoyaltyProgramWidget-actions|Styles applied to the actions section.|
 |points|.SCLoyaltyProgramWidget-points|Styles applied to the points section.|
 |discoverMore|.SCLoyaltyProgramWidget-discover-more|Styles applied to discover more button element.|
 *
 * @param inProps
 */
function LoyaltyProgramWidget(inProps) {
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    // PROPS
    const { className, userId = null } = props, rest = tslib_1.__rest(props, ["className", "userId"]);
    // CONTEXT
    const scUserContext = (0, react_1.useContext)(react_core_1.SCUserContext);
    const scPreferencesContext = (0, react_core_1.useSCPreferences)();
    const scRoutingContext = (0, react_core_2.useSCRouting)();
    // STATE
    const [points, setPoints] = (0, react_1.useState)(null);
    const [loading, setLoading] = (0, react_1.useState)(true);
    const authUserId = scUserContext.user ? scUserContext.user.id : null;
    //MEMO
    const loyaltyEnabled = (0, react_1.useMemo)(() => react_core_1.SCPreferences.ADDONS_LOYALTY_POINTS_COLLECTION in scPreferencesContext.preferences &&
        scPreferencesContext.preferences[react_core_1.SCPreferences.ADDONS_LOYALTY_POINTS_COLLECTION].value, [scPreferencesContext.preferences]);
    /**
     * Fetches user loyalty points
     */
    const fetchLP = () => {
        api_services_1.http
            .request({
            url: api_services_1.Endpoints.GetUserLoyaltyPoints.url({ id: scUserContext.user['id'] }),
            method: api_services_1.Endpoints.GetUserLoyaltyPoints.method
        })
            .then((res) => {
            setPoints(res.data.points);
            setLoading(false);
        })
            .catch((error) => {
            setLoading(false);
            utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error);
            console.log(error);
        });
    };
    /**
     * On mount, fetches user loyalty points
     */
    (0, react_1.useEffect)(() => {
        if (loyaltyEnabled && authUserId) {
            fetchLP();
        }
    }, [authUserId, loyaltyEnabled]);
    /**
     * Rendering
     */
    if (!loyaltyEnabled || !scUserContext.user || (userId && userId !== scUserContext.user.id)) {
        return (0, jsx_runtime_1.jsx)(HiddenPlaceholder_1.default, {});
    }
    if (loading) {
        return (0, jsx_runtime_1.jsx)(Skeleton_1.default, {});
    }
    return ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: [(0, jsx_runtime_1.jsx)(CardContent_1.default, { children: (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ className: classes.title }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.loyaltyProgramWidget.title", defaultMessage: "ui.loyaltyProgramWidget.title" }) })) }), (0, jsx_runtime_1.jsxs)(material_1.CardActions, Object.assign({ className: classes.actions }, { children: [(0, jsx_runtime_1.jsxs)(material_1.Typography, Object.assign({ className: classes.points }, { children: [(0, jsx_runtime_1.jsx)(material_1.Chip, { size: 'medium', component: "span", label: points }), (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.loyaltyProgramWidget.points", defaultMessage: "ui.loyaltyProgramWidget.points" })] })), (0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ size: "small", variant: "outlined", className: classes.discoverMore, component: react_core_2.Link, to: scRoutingContext.url(react_core_2.SCRoutes.LOYALTY_ROUTE_NAME, {}) }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.loyaltyProgramWidget.discover", defaultMessage: "ui.loyaltyProgramWidget.discover" }) }))] }))] })));
}
exports.default = LoyaltyProgramWidget;
