/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-d1bbdd3
 * Released under the MIT license.
 */

import { jsx as _jsx } from "react/jsx-runtime";
import { forwardRef, useCallback } from 'react';
import { CloseIcon } from '../../../icons/CloseIcon.js';
import { CONTAINER_STYLES, extractStyles, tasty, } from '../../../tasty/index.js';
import { ItemAction } from '../../actions/ItemAction/index.js';
import { useDialogContext } from '../../overlays/Dialog/context.js';
import { Item } from '../Item/Item.js';
import { useLayoutPanelContext } from './LayoutContext.js';
const PanelHeaderElement = tasty(Item, {
    qa: 'PanelHeader',
    shape: 'sharp',
    type: 'header',
    styles: {
        border: 'bottom',
        boxSizing: 'content-box',
        '$inline-padding': '($content-padding, 1x)',
    },
});
function LayoutPanelHeader(props, ref) {
    const { title, level = 3, isClosable, onClose, actions, children, ...otherProps } = props;
    // Extract container styles
    const styles = extractStyles(otherProps, CONTAINER_STYLES);
    // Access dialog context if in dialog mode
    const dialogContext = useDialogContext();
    // Access panel context to update panel open state
    const panelContext = useLayoutPanelContext();
    // Close handler that works for both panel and dialog modes
    const handleClose = useCallback(() => {
        // Call user-provided onClose callback
        onClose?.();
        // Update panel's internal open state
        panelContext?.onOpenChange(false);
        // If in dialog mode, also close the dialog
        dialogContext?.onClose?.();
    }, [onClose, panelContext, dialogContext]);
    const closeAction = isClosable ? (_jsx(ItemAction, { icon: _jsx(CloseIcon, {}), "aria-label": "Close panel", onPress: handleClose })) : null;
    const finalActions = actions ?? closeAction;
    return (_jsx(PanelHeaderElement, { ref: ref, level: level, size: "large", actions: finalActions, styles: styles, ...otherProps, children: title ?? children }));
}
const _LayoutPanelHeader = forwardRef(LayoutPanelHeader);
_LayoutPanelHeader.displayName = 'Layout.PanelHeader';
export { _LayoutPanelHeader as LayoutPanelHeader };

        