import { makeStyles, Typography } from '@material-ui/core';
import cloneDeep from 'lodash-es/cloneDeep';
import PropTypes from 'prop-types';
import React, { useCallback, useMemo } from 'react';
import { ActionSummaryPopover } from './actionSummary';
import { getIconFromName } from '../utilities/tree/TreeUtilities';
import { getUnityIconFromName } from '../utilities/iconLibrary/UnityIconLibrary';
import { PAHAdditionalRightComponent } from '../UI/utilityDisplay/utilityHeader/ui/PAHAdditionalRightComponent';
import { PanelPositionEnums } from '../enums/unitySystemEnums';
import { PrimaryActionButton } from '../UI/inputs/buttons/PrimaryActionButton';
import { SiteHeaderRightComponent } from '../shell/ui/SiteHeaderRightComponent';
import { ToolBarButton } from '../UI/inputs/buttons/ToolBarButton';
import { useActions } from '../hooks/useActions';
import { evaluateRule, mapRule } from '../utilities/rules/RuleUtilities';
import { ActionSpeedDial } from './ActionSpeedDial';
import { useActionStore } from '../stores/actionStore';
const useStyles = makeStyles((theme) => {
    var _a, _b, _c;
    return ({
        root: {},
        actionsMenu: {
            color: (_a = theme === null || theme === void 0 ? void 0 : theme.palette) === null || _a === void 0 ? void 0 : _a.getContrastText((_c = (_b = theme === null || theme === void 0 ? void 0 : theme.palette) === null || _b === void 0 ? void 0 : _b.primary) === null || _c === void 0 ? void 0 : _c.main)
        },
        additionalRightComponent: {
            display: 'flex',
            alignItems: 'center'
        }
    });
});
export const ActionPanelPlacement = (props) => {
    const { actionPanels, showInquiryHeaderActions, selectedNode, globalActions } = props;
    const classes = useStyles();
    const { triggerAction } = useActions();
    const hideActions = useActionStore((state) => state.hideActions);
    const handleTriggerAction = useCallback((actionId) => {
        triggerAction(actionId, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.nodeObj);
    }, [triggerAction, selectedNode]);
    const panelsMap = useMemo(() => {
        if (!actionPanels || !(actionPanels === null || actionPanels === void 0 ? void 0 : actionPanels.length)) {
            return {};
        }
        return actionPanels.reduce((panelsObj, panel) => {
            const panelPositionId = panel === null || panel === void 0 ? void 0 : panel.panelPositionId;
            panelsObj[panelPositionId] = panel === null || panel === void 0 ? void 0 : panel.panelActions;
            return panelsObj;
        }, {});
    }, [actionPanels]);
    const siteHeaderRightComponent = useMemo(() => {
        const siteHeaderActions = panelsMap[PanelPositionEnums.Site_Header];
        const siteHeaderSummaryActions = panelsMap[PanelPositionEnums.Site_Header_Summary];
        let siteHeaderActionsComponent1 = null;
        let siteHeaderActionsComponent2 = null;
        let siteHeaderSummaryComponent = null;
        if (siteHeaderActions === null || siteHeaderActions === void 0 ? void 0 : siteHeaderActions.length) {
            siteHeaderActionsComponent1 = siteHeaderActions.map((panelAction) => {
                var _a, _b, _c, _d, _e, _f;
                var _g;
                const passRule = (_g = (_a = panelAction.action.actionRules) === null || _a === void 0 ? void 0 : _a.reduce((boolValue, rule) => {
                    var _a;
                    const ruleExpression = mapRule(rule.mapping, rule.jsonLogic);
                    return (boolValue &&
                        evaluateRule(ruleExpression, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.nodeObj, (_a = selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.properties) === null || _a === void 0 ? void 0 : _a.joiningEntityCatalogObjectKey, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.wasLazyLoaded));
                }, true)) !== null && _g !== void 0 ? _g : true;
                if (!passRule && (panelAction === null || panelAction === void 0 ? void 0 : panelAction.hideAction)) {
                    return null;
                }
                return (React.createElement(ToolBarButton, { key: panelAction === null || panelAction === void 0 ? void 0 : panelAction.actionId, label: (_b = panelAction === null || panelAction === void 0 ? void 0 : panelAction.action) === null || _b === void 0 ? void 0 : _b.name, aria: (_c = panelAction === null || panelAction === void 0 ? void 0 : panelAction.action) === null || _c === void 0 ? void 0 : _c.name, icon: false, component: ((_d = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _d === void 0 ? void 0 : _d.type) === 'unity'
                        ? getUnityIconFromName((_e = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _e === void 0 ? void 0 : _e.name)
                        : getIconFromName((_f = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _f === void 0 ? void 0 : _f.name), click: () => handleTriggerAction(panelAction === null || panelAction === void 0 ? void 0 : panelAction.actionId), disabled: !passRule }));
            });
        }
        if (siteHeaderSummaryActions === null || siteHeaderSummaryActions === void 0 ? void 0 : siteHeaderSummaryActions.length) {
            if (!(siteHeaderActions === null || siteHeaderActions === void 0 ? void 0 : siteHeaderActions.length) && (siteHeaderSummaryActions === null || siteHeaderSummaryActions === void 0 ? void 0 : siteHeaderSummaryActions.length) <= 2) {
                siteHeaderActionsComponent2 = siteHeaderSummaryActions.map((panelAction) => {
                    var _a, _b, _c, _d, _e, _f;
                    var _g;
                    const passRule = (_g = (_a = panelAction.action.actionRules) === null || _a === void 0 ? void 0 : _a.reduce((boolValue, rule) => {
                        var _a;
                        const ruleExpression = mapRule(rule.mapping, rule.jsonLogic);
                        return (boolValue &&
                            evaluateRule(ruleExpression, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.nodeObj, (_a = selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.properties) === null || _a === void 0 ? void 0 : _a.joiningEntityCatalogObjectKey, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.wasLazyLoaded));
                    }, true)) !== null && _g !== void 0 ? _g : true;
                    if (!passRule && (panelAction === null || panelAction === void 0 ? void 0 : panelAction.hideAction)) {
                        return null;
                    }
                    return (React.createElement(ToolBarButton, { key: panelAction === null || panelAction === void 0 ? void 0 : panelAction.actionId, label: (_b = panelAction === null || panelAction === void 0 ? void 0 : panelAction.action) === null || _b === void 0 ? void 0 : _b.name, aria: (_c = panelAction === null || panelAction === void 0 ? void 0 : panelAction.action) === null || _c === void 0 ? void 0 : _c.name, icon: false, component: ((_d = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _d === void 0 ? void 0 : _d.type) === 'unity'
                            ? getUnityIconFromName((_e = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _e === void 0 ? void 0 : _e.name)
                            : getIconFromName((_f = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _f === void 0 ? void 0 : _f.name), click: () => handleTriggerAction(panelAction === null || panelAction === void 0 ? void 0 : panelAction.actionId), disabled: !passRule }));
                });
            }
            else {
                siteHeaderSummaryComponent = (React.createElement(ActionSummaryPopover, { panelActions: siteHeaderSummaryActions, selectedNode: selectedNode }));
            }
        }
        return (React.createElement(SiteHeaderRightComponent, null, React.createElement("div", { className: classes.additionalRightComponent }, siteHeaderActionsComponent1, siteHeaderActionsComponent2, siteHeaderSummaryComponent)));
    }, [panelsMap, handleTriggerAction, classes, selectedNode]);
    const inquiryHeaderComponent = useMemo(() => {
        var _a, _b;
        var _c;
        const panelsMapCopy = cloneDeep(panelsMap);
        const inquiryHeaderActions = panelsMapCopy[PanelPositionEnums.Inquiry_Header];
        const inquiryHeaderSummaryActions = panelsMapCopy[PanelPositionEnums.Inquiry_Header_Summary];
        let inquiryHeaderActionsComponent = null;
        let inquiryHeaderSummaryComponent = null;
        let inquiryHeaderPrimaryAction = null;
        // if inquiry header summary actions length is more than 2, display the Actions as primary and the primary action as secondary.
        // otherwise, display the primary action as primary and Actions as secondary
        const showActionsAsSecondary = !(inquiryHeaderSummaryActions === null || inquiryHeaderSummaryActions === void 0 ? void 0 : inquiryHeaderSummaryActions.length) ||
            (!(inquiryHeaderActions === null || inquiryHeaderActions === void 0 ? void 0 : inquiryHeaderActions.length) &&
                (inquiryHeaderSummaryActions === null || inquiryHeaderSummaryActions === void 0 ? void 0 : inquiryHeaderSummaryActions.length) <= 2);
        if (inquiryHeaderActions === null || inquiryHeaderActions === void 0 ? void 0 : inquiryHeaderActions.length) {
            const actionList = [];
            inquiryHeaderActions.forEach((action) => {
                var _a;
                var _b;
                const passRule = (_b = (_a = action.action.actionRules) === null || _a === void 0 ? void 0 : _a.reduce((boolValue, rule) => {
                    var _a;
                    const ruleExpression = mapRule(rule.mapping, rule.jsonLogic);
                    return (boolValue &&
                        evaluateRule(ruleExpression, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.nodeObj, (_a = selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.properties) === null || _a === void 0 ? void 0 : _a.joiningEntityCatalogObjectKey, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.wasLazyLoaded));
                }, true)) !== null && _b !== void 0 ? _b : true;
                if (!passRule && !(action === null || action === void 0 ? void 0 : action.hideAction)) {
                    actionList.push(Object.assign(Object.assign({}, action), { disabled: true }));
                }
                else if (passRule) {
                    actionList.push(action);
                }
            });
            inquiryHeaderActionsComponent = (_a = actionList === null || actionList === void 0 ? void 0 : actionList.filter((pa) => {
                return showActionsAsSecondary
                    ? !pa.isPrimary && pa.enabled
                    : pa.enabled;
            })) === null || _a === void 0 ? void 0 : _a.map((panelAction, i) => {
                var _a, _b, _c, _d, _e;
                var _f;
                return (React.createElement("div", { key: i }, React.createElement(ToolBarButton
                // key={panelAction?.actionId}
                , { 
                    // key={panelAction?.actionId}
                    label: (_a = panelAction === null || panelAction === void 0 ? void 0 : panelAction.action) === null || _a === void 0 ? void 0 : _a.name, aria: (_b = panelAction === null || panelAction === void 0 ? void 0 : panelAction.action) === null || _b === void 0 ? void 0 : _b.name, icon: false, component: ((_c = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _c === void 0 ? void 0 : _c.type) === 'unity'
                        ? getUnityIconFromName((_d = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _d === void 0 ? void 0 : _d.name)
                        : getIconFromName((_e = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _e === void 0 ? void 0 : _e.name), click: () => {
                        handleTriggerAction(panelAction === null || panelAction === void 0 ? void 0 : panelAction.actionId);
                    }, disabled: (_f = panelAction === null || panelAction === void 0 ? void 0 : panelAction.disabled) !== null && _f !== void 0 ? _f : false })));
            });
            if (showActionsAsSecondary) {
                const primaryAction = actionList === null || actionList === void 0 ? void 0 : actionList.find((panelAction) => panelAction.isPrimary);
                inquiryHeaderPrimaryAction = primaryAction ? (React.createElement(PrimaryActionButton, { onClick: () => handleTriggerAction(primaryAction === null || primaryAction === void 0 ? void 0 : primaryAction.actionId), disabled: (_c = primaryAction === null || primaryAction === void 0 ? void 0 : primaryAction.disabled) !== null && _c !== void 0 ? _c : false }, React.createElement(Typography, { style: { marginRight: 5 } }, (_b = primaryAction === null || primaryAction === void 0 ? void 0 : primaryAction.action) === null || _b === void 0 ? void 0 : _b.name))) : null;
            }
        }
        if (inquiryHeaderSummaryActions === null || inquiryHeaderSummaryActions === void 0 ? void 0 : inquiryHeaderSummaryActions.length) {
            if (showActionsAsSecondary) {
                inquiryHeaderActionsComponent = inquiryHeaderSummaryActions.map((panelAction) => {
                    var _a, _b, _c, _d, _e;
                    var _f;
                    const passRule = (_f = (_a = panelAction.action.actionRules) === null || _a === void 0 ? void 0 : _a.reduce((boolValue, rule) => {
                        var _a;
                        const ruleExpression = mapRule(rule.mapping, rule.jsonLogic);
                        return (boolValue &&
                            evaluateRule(ruleExpression, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.nodeObj, (_a = selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.properties) === null || _a === void 0 ? void 0 : _a.joiningEntityCatalogObjectKey, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.wasLazyLoaded));
                    }, true)) !== null && _f !== void 0 ? _f : true;
                    if (!passRule && (panelAction === null || panelAction === void 0 ? void 0 : panelAction.hideAction)) {
                        return null;
                    }
                    return (React.createElement(ToolBarButton, { key: panelAction === null || panelAction === void 0 ? void 0 : panelAction.actionId, label: (_b = panelAction === null || panelAction === void 0 ? void 0 : panelAction.action) === null || _b === void 0 ? void 0 : _b.name, icon: false, component: ((_c = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _c === void 0 ? void 0 : _c.type) === 'unity'
                            ? getUnityIconFromName((_d = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _d === void 0 ? void 0 : _d.name)
                            : getIconFromName((_e = panelAction === null || panelAction === void 0 ? void 0 : panelAction.icon) === null || _e === void 0 ? void 0 : _e.name), click: () => handleTriggerAction(panelAction === null || panelAction === void 0 ? void 0 : panelAction.actionId), disabled: !passRule }));
                });
            }
            else {
                inquiryHeaderSummaryComponent = (React.createElement(ActionSummaryPopover, { panelActions: inquiryHeaderSummaryActions, noContrastColor: true, selectedNode: selectedNode }));
            }
        }
        return (React.createElement(PAHAdditionalRightComponent, null, showInquiryHeaderActions && !hideActions && (React.createElement("div", { className: classes.additionalRightComponent }, inquiryHeaderActionsComponent, inquiryHeaderPrimaryAction, inquiryHeaderSummaryComponent))));
    }, [
        panelsMap,
        handleTriggerAction,
        classes,
        showInquiryHeaderActions,
        selectedNode,
        hideActions
    ]);
    const inquirySpeedDialComponent = useMemo(() => {
        var _a;
        const panelsMapCopy = cloneDeep(panelsMap);
        const inquirySpeedDialActions = panelsMapCopy[PanelPositionEnums.Speed_Dial];
        const enabledSortedActions = (_a = inquirySpeedDialActions === null || inquirySpeedDialActions === void 0 ? void 0 : inquirySpeedDialActions.filter((action) => {
            var _a;
            var _b;
            return action.enabled &&
                ((_b = (_a = action.action.actionRules) === null || _a === void 0 ? void 0 : _a.reduce((boolValue, rule) => {
                    var _a;
                    const ruleExpression = mapRule(rule.mapping, rule.jsonLogic);
                    return (boolValue &&
                        evaluateRule(ruleExpression, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.nodeObj, (_a = selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.properties) === null || _a === void 0 ? void 0 : _a.joiningEntityCatalogObjectKey, selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.wasLazyLoaded));
                }, true)) !== null && _b !== void 0 ? _b : true);
        }).sort((a, b) => a.sequence - b.sequence)) !== null && _a !== void 0 ? _a : [];
        let parsedGlobalActions = [];
        if (globalActions === null || globalActions === void 0 ? void 0 : globalActions.length) {
            parsedGlobalActions = globalActions.map((action) => {
                const parameters = JSON.parse(action.parameters);
                return Object.assign({ icon: { name: parameters.icon, type: 'fluent' }, action: {
                        name: action.name
                    } }, action);
            });
        }
        return ((!!(enabledSortedActions === null || enabledSortedActions === void 0 ? void 0 : enabledSortedActions.length) || !!(parsedGlobalActions === null || parsedGlobalActions === void 0 ? void 0 : parsedGlobalActions.length)) &&
            !hideActions && (React.createElement(ActionSpeedDial, { actions: [...enabledSortedActions, ...parsedGlobalActions].map((action) => ({
                icon: action.icon,
                onClick: () => handleTriggerAction(action.actionId),
                name: action.action.name
            })) })));
    }, [
        panelsMap,
        handleTriggerAction,
        selectedNode,
        globalActions,
        hideActions
    ]);
    return (React.createElement(React.Fragment, null, siteHeaderRightComponent, inquiryHeaderComponent, inquirySpeedDialComponent));
};
ActionPanelPlacement.propTypes = {
    /**
     * Action Panels
     */
    actionPanels: PropTypes.array,
    /**
     * Boolean to show inquiry header actions
     */
    showInquiryHeaderActions: PropTypes.bool,
    /**
     * Action Panels
     */
    selectedNode: PropTypes.object
};
//# sourceMappingURL=ActionPanelPlacement.js.map