"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePopup = usePopup;
const react_1 = require("react");
const INITIAL_POPUP_STATE = {
    open: false,
    messageId: null,
    actionType: null,
    anchorElement: null,
    content: null,
    title: undefined,
    subtitle: undefined,
    actionConfig: null,
};
/**
 * Hook for managing action popup state in MessageList.
 * Handles opening, closing, and dynamic content/title updates for popups
 * triggered by message action buttons.
 *
 * @returns Popup state, open/close handlers, and whether the action popup should render.
 */
function usePopup() {
    const [popupState, setPopupState] = (0, react_1.useState)(INITIAL_POPUP_STATE);
    const handleActionPopup = (0, react_1.useCallback)((message, action, anchorElement) => {
        if (!action.popup) {
            return;
        }
        const context = {
            setContent: (newContent) => {
                setPopupState((prev) => (Object.assign(Object.assign({}, prev), { content: newContent })));
            },
            setTitle: (newTitle) => {
                setPopupState((prev) => (Object.assign(Object.assign({}, prev), { title: newTitle })));
            },
            setSubtitle: (newSubtitle) => {
                setPopupState((prev) => (Object.assign(Object.assign({}, prev), { subtitle: newSubtitle })));
            },
            closePopup: () => {
                setPopupState(INITIAL_POPUP_STATE);
            },
        };
        const content = action.popup.getContent(message, context);
        setPopupState({
            open: true,
            messageId: message.id || null,
            actionType: action.actionType || null,
            anchorElement,
            content,
            title: action.popup.title,
            subtitle: action.popup.subtitle,
            actionConfig: action,
        });
    }, []);
    const handlePopupOpenChange = (0, react_1.useCallback)((open) => {
        if (!open) {
            setPopupState(INITIAL_POPUP_STATE);
        }
    }, []);
    const showActionPopup = popupState.open && popupState.content !== null && popupState.anchorElement !== null;
    return {
        popupState,
        handleActionPopup,
        handlePopupOpenChange,
        showActionPopup,
    };
}
//# sourceMappingURL=usePopup.js.map
