import React, { useCallback, useState, useEffect, useMemo } from 'react';
import { useSnackbar } from 'notistack';
import { ActionSplitScreen } from './ActionSplitScreen';
import { useActionState } from '../hooks/useActions';
import { apiMutate } from '../utilities/useAxiosMutate';
import { PageSubHeaderActionsTarget } from '../utilities/page/PageSubHeaderAction';
import { useActionStore } from '../stores/actionStore';
import { useShellStore } from '../stores/shellStore';
import { ConfigService } from '../configService';
export const PageActionWrapper = (props) => {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    const { children, forPageContainer = false, splitScreenStateKey = 'pageSplitScreen' } = props;
    const [actionPanel, setActionPanel] = useState(null);
    const { enqueueSnackbar } = useSnackbar();
    const actionState = useActionState();
    const selectedNode = useActionStore((state) => state.selectedNode);
    const currentContainerId = useShellStore((state) => state.currentContainerId);
    const action = useMemo(() => {
        var _a;
        let selectedAction = null;
        if (actionState) {
            if ((_a = actionState.pageSplitScreen) === null || _a === void 0 ? void 0 : _a.action) {
                selectedAction = actionState.pageSplitScreen.action;
            }
        }
        return selectedAction;
    }, [actionState]);
    const executeAction = useCallback(async (data, id) => apiMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${id}`, {
        method: 'post'
    }, data), []);
    useEffect(() => {
        var _a;
        if (action && actionState) {
            setActionPanel((_a = actionState === null || actionState === void 0 ? void 0 : actionState.actionPanels[currentContainerId || 0]) === null || _a === void 0 ? void 0 : _a.find((ap) => ap.panelActions.find((pa) => pa.actionId === action.actionId)));
        }
    }, [action, actionState, currentContainerId]);
    return (React.createElement(React.Fragment, null, React.createElement(PageSubHeaderActionsTarget, null), React.createElement(ActionSplitScreen, { body: children, open: (_a = actionState === null || actionState === void 0 ? void 0 : actionState[splitScreenStateKey]) === null || _a === void 0 ? void 0 : _a.open, action: (_b = actionState === null || actionState === void 0 ? void 0 : actionState[splitScreenStateKey]) === null || _b === void 0 ? void 0 : _b.action, type: (_c = actionState === null || actionState === void 0 ? void 0 : actionState[splitScreenStateKey]) === null || _c === void 0 ? void 0 : _c.type, params: (_d = actionState === null || actionState === void 0 ? void 0 : actionState[splitScreenStateKey]) === null || _d === void 0 ? void 0 : _d.params, executeAction: executeAction, actionPanel: actionPanel, enqueueSnackbar: enqueueSnackbar, selectedNodeObj: selectedNode === null || selectedNode === void 0 ? void 0 : selectedNode.nodeObj, menuNodeList: (_e = actionState === null || actionState === void 0 ? void 0 : actionState.menuListState) === null || _e === void 0 ? void 0 : _e.menuList, forPage: true, forPageContainer: forPageContainer, selectedNode: (_f = actionState === null || actionState === void 0 ? void 0 : actionState.inquiryParams[currentContainerId || 0]) === null || _f === void 0 ? void 0 : _f.selectedNode, selectedNodeParent: (_g = actionState === null || actionState === void 0 ? void 0 : actionState.inquiryParams[currentContainerId || 0]) === null || _g === void 0 ? void 0 : _g.selectedNodeParent, refreshTree: (_h = actionState === null || actionState === void 0 ? void 0 : actionState.inquiryParams[currentContainerId || 0]) === null || _h === void 0 ? void 0 : _h.refreshTree })));
};
//# sourceMappingURL=PageActionWrapper.js.map