"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importStar(require("react"));
const styles_1 = require("@mui/material/styles");
const material_1 = require("@mui/material");
const react_intl_1 = require("react-intl");
const react_core_1 = require("@selfcommunity/react-core");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const system_1 = require("@mui/system");
const Icon_1 = tslib_1.__importDefault(require("@mui/material/Icon"));
const types_1 = require("../../types");
const messages = (0, react_intl_1.defineMessages)({
    socialAdd: {
        id: 'ui.userSocialAssociation.add',
        defaultMessage: 'ui.userSocialAssociation.add'
    },
    socialRemove: {
        id: 'ui.userSocialAssociation.remove',
        defaultMessage: 'ui.userSocialAssociation.remove'
    }
});
const PREFIX = 'SCUserSocialAssociation';
const classes = {
    root: `${PREFIX}-root`,
    field: `${PREFIX}-field`
};
const Root = (0, styles_1.styled)(material_1.Stack, {
    name: PREFIX,
    slot: 'Root',
    overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({}));
const PREFERENCES = [
    react_core_1.SCPreferences.PROVIDERS_FACEBOOK_SIGNIN_ENABLED,
    react_core_1.SCPreferences.PROVIDERS_GOOGLE_SIGNIN_ENABLED,
    react_core_1.SCPreferences.PROVIDERS_TWITTER_SIGNIN_ENABLED,
    react_core_1.SCPreferences.PROVIDERS_LINKEDIN_SIGNIN_ENABLED
];
/**
 * > API documentation for the Community-JS User Social Association component. Learn about the available props and the CSS API.

 #### Import

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

 #### Component Name

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


 #### CSS

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

 * @param inProps
 */
function UserSocialAssociation(inProps) {
    // PROPS
    const props = (0, system_1.useThemeProps)({
        props: inProps,
        name: PREFIX
    });
    const { className = null, userId = null, user = null, providers = null, onCreateAssociation = null, onDeleteAssociation = null, children = null, deletingProvider = null } = props, rest = tslib_1.__rest(props, ["className", "userId", "user", "providers", "onCreateAssociation", "onDeleteAssociation", "children", "deletingProvider"]);
    // HOOKS
    const { scUser } = (0, react_core_1.useSCFetchUser)({ id: userId, user });
    const { scUserProviders, setSCUserProviders } = (0, react_core_1.useSCFetchUserProviders)({ id: userId || scUser.id, providers });
    // INTL
    const intl = (0, react_intl_1.useIntl)();
    // Compute preferences
    const scPreferences = (0, react_core_1.useSCPreferences)();
    const preferences = (0, react_1.useMemo)(() => {
        const _preferences = {};
        PREFERENCES.map((p) => (_preferences[p] = p in scPreferences.preferences ? scPreferences.preferences[p].value : null));
        return _preferences;
    }, [scPreferences.preferences]);
    // MEMO PROVIDERS
    const _providers = (0, react_1.useMemo)(() => {
        return scUserProviders
            .map((provider) => ({ [provider.provider]: provider }))
            .reduce((prev, provider) => Object.assign(prev, provider), {});
    }, [scUserProviders]);
    const providersEnabled = Object.values(types_1.SCUserSocialAssociations).filter((p) => preferences[`providers.${p}_signin_enabled`]);
    const providersToLink = providersEnabled === null || providersEnabled === void 0 ? void 0 : providersEnabled.filter((p) => { var _a, _b; return !((_a = _providers[p]) === null || _a === void 0 ? void 0 : _a.provider) && !((_b = _providers[p]) === null || _b === void 0 ? void 0 : _b.ext_id); });
    const providersLinked = Object.values(_providers).filter((p) => providersEnabled.includes(p.provider));
    /**
     * Updates providers list onDelete association
     */
    (0, react_1.useEffect)(() => {
        if (deletingProvider) {
            const updatedProviders = scUserProviders.filter((p) => p.provider !== deletingProvider.provider);
            setSCUserProviders(updatedProviders);
        }
    }, [deletingProvider]);
    /**
     * Renders social association block
     */
    if (!providersEnabled) {
        return null;
    }
    return ((0, jsx_runtime_1.jsxs)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { direction: "column" }, { children: [providersLinked.length !== 0 || (providersToLink.length !== 0 && onCreateAssociation) ? children : null, providersToLink.length !== 0 && onCreateAssociation && ((0, jsx_runtime_1.jsxs)(material_1.Box, { children: [(0, jsx_runtime_1.jsxs)(material_1.Typography, Object.assign({ variant: "body2" }, { children: [" ", intl.formatMessage(messages.socialAdd)] })), providersToLink.map((p, index) => ((0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: (0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ onClick: onCreateAssociation ? () => onCreateAssociation(p) : null }, { children: (0, jsx_runtime_1.jsx)(Icon_1.default, { children: p }) })) }, index)))] })), providersLinked.length !== 0 && ((0, jsx_runtime_1.jsxs)(material_1.Box, { children: [(0, jsx_runtime_1.jsxs)(material_1.Typography, Object.assign({ variant: "body2" }, { children: [" ", intl.formatMessage(messages.socialRemove)] })), providersLinked.map((p, index) => ((0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: (0, jsx_runtime_1.jsx)(material_1.IconButton, Object.assign({ color: "primary", onClick: () => onDeleteAssociation(p) }, { children: (0, jsx_runtime_1.jsx)(Icon_1.default, { children: p.provider }) })) }, index)))] }))] })));
}
exports.default = UserSocialAssociation;
