"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 Box_1 = tslib_1.__importDefault(require("@mui/material/Box"));
const system_1 = require("@mui/system");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const constants_1 = require("../../constants");
const material_1 = require("@mui/material");
const react_intl_1 = require("react-intl");
const react_core_1 = require("@selfcommunity/react-core");
const SocialShare_1 = require("../../../../constants/SocialShare");
const api_services_1 = require("@selfcommunity/api-services");
const utils_1 = require("@selfcommunity/utils");
const Errors_1 = require("../../../../constants/Errors");
const types_1 = require("@selfcommunity/types");
const classes = {
    root: `${constants_1.PREFIX}-invite-root`,
    title: `${constants_1.PREFIX}-invite-title`,
    social: `${constants_1.PREFIX}-invite-social`,
    email: `${constants_1.PREFIX}-invite-email`,
    iconContainer: `${constants_1.PREFIX}-invite-social-icon-container`,
    icon: `${constants_1.PREFIX}-invite-social-icon`,
    action: `${constants_1.PREFIX}-invite-action`
};
const Root = (0, styles_1.styled)(Box_1.default, {
    name: constants_1.PREFIX,
    slot: 'InviteRoot'
})(() => ({}));
const META_ROBOTS_ENABLE_DEFAULT = 'index,follow';
function Invite(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: constants_1.PREFIX
    });
    const { className, onCompleteAction = null } = props;
    // STATUS
    const [isMetaRobotsUpdating, setMetaRobotsUpdating] = (0, react_1.useState)(false);
    // CONTEXT
    const scPreferencesContext = (0, react_1.useContext)(react_core_1.SCPreferencesContext);
    const metaRobots = (0, react_1.useMemo)(() => {
        return scPreferencesContext.preferences && react_core_1.SCPreferences.WEBMASTER_META_ROBOTS in scPreferencesContext.preferences
            ? scPreferencesContext.preferences[react_core_1.SCPreferences.WEBMASTER_META_ROBOTS].value
            : null;
    }, [scPreferencesContext.preferences]);
    const url = (0, react_1.useMemo)(() => scPreferencesContext.preferences &&
        react_core_1.SCPreferences.CONFIGURATIONS_GROUPS_ENABLED in scPreferencesContext.preferences &&
        scPreferencesContext.preferences[react_core_1.SCPreferences.CONFIGURATIONS_APP_URL].value, [scPreferencesContext.preferences]);
    // HANDLERS
    const handleShare = (shareUrl, shareType) => tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (metaRobots.toLowerCase().replace(/\s+/g, '') !== META_ROBOTS_ENABLE_DEFAULT) {
            yield enableSeo();
        }
        window.open(shareUrl, `${shareType}-share-dialog`, 'width=626,height=436');
        onCompleteAction();
    });
    const enableSeo = () => tslib_1.__awaiter(this, void 0, void 0, function* () {
        try {
            setMetaRobotsUpdating(true);
            yield api_services_1.PreferenceService.updatePreferences({ [types_1.SCPreferenceName.META_ROBOTS]: 'index, follow' });
        }
        catch (e) {
            utils_1.Logger.error(Errors_1.SCOPE_SC_UI, e);
        }
        finally {
            setMetaRobotsUpdating(false);
        }
    });
    /**
     * Fetches platform url
     */
    function fetchPlatform(query) {
        api_services_1.http
            .request({
            url: api_services_1.Endpoints.Platform.url(),
            method: api_services_1.Endpoints.Platform.method,
            params: {
                next: query
            }
        })
            .then((res) => {
            const platformUrl = res.data.platform_url;
            window.open(platformUrl, '_blank').focus();
        })
            .catch((error) => {
            console.log(error);
        });
    }
    const handleCompleteAction = () => {
        onCompleteAction();
        fetchPlatform('/marketing/invitation');
    };
    return ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "h4", className: classes.title }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.onBoardingWidget.invite", defaultMessage: "ui.onBoardingWidget.invite" }) })), (0, jsx_runtime_1.jsxs)(Box_1.default, Object.assign({ className: classes.social }, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ className: classes.title, variant: "h4" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.onBoardingWidget.step.invite.social.title", defaultMessage: "ui.onBoardingWidget.step.invite.social.title" }) })), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "subtitle1" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.onBoardingWidget.step.invite.social.subtitle", defaultMessage: "ui.onBoardingWidget.step.invite.social.subtitle" }) })), (0, jsx_runtime_1.jsxs)(Box_1.default, Object.assign({ className: classes.iconContainer }, { children: [(0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ onClick: () => handleShare(SocialShare_1.FACEBOOK_SHARE + url, 'facebook'), disabled: !url || isMetaRobotsUpdating }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, Object.assign({ classes: { root: classes.icon } }, { children: "facebook" })) })), (0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ onClick: () => handleShare(SocialShare_1.X_SHARE + url, 'x'), disabled: !url || isMetaRobotsUpdating }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, Object.assign({ classes: { root: classes.icon } }, { children: "twitter" })) })), (0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ onClick: () => handleShare(SocialShare_1.LINKEDIN_SHARE + url, 'linkedin'), disabled: !url || isMetaRobotsUpdating }, { children: (0, jsx_runtime_1.jsx)(material_1.Icon, Object.assign({ classes: { root: classes.icon } }, { children: "linkedin" })) }))] }))] })), (0, jsx_runtime_1.jsxs)(Box_1.default, Object.assign({ className: classes.email }, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ className: classes.title, variant: "h4" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.onBoardingWidget.step.invite.email.title", defaultMessage: "ui.onBoardingWidget.step.invite.email.title" }) })), (0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ variant: "subtitle1" }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.onBoardingWidget.step.invite.email.subtitle", defaultMessage: "ui.onBoardingWidget.step.invite.email.subtitle" }) }))] })), (0, jsx_runtime_1.jsx)(Box_1.default, Object.assign({ component: "span", className: classes.action }, { children: (0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ variant: "outlined", size: "small", onClick: handleCompleteAction }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { defaultMessage: "ui.onBoardingWidget.step.invite.button", id: "ui.onBoardingWidget.step.invite.button" }) })) }))] })));
}
exports.default = Invite;
