import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import { Link } from '@gravity-ui/uikit';
import { useAnalytics } from "../../gravity-blocks/hooks/index.js";
import { DefaultEventNames, } from "../../models/index.js";
import { block, getQaAttrubutes } from "../../utils/index.js";
import BackgroundImage from "../BackgroundImage/BackgroundImage.js";
import RouterLink from "../RouterLink/RouterLink.js";
import './CardBase.css';
const b = block('card-base-block');
const Header = () => null;
const Content = () => null;
const Footer = () => null;
export const Layout = (props) => {
    const { className, bodyClassName, analyticsEvents, contentClassName, children, url, target, border = 'shadow', urlTitle, qa, extraProps = {}, } = props;
    const handleAnalytics = useAnalytics(DefaultEventNames.CardBase, url);
    let header, content, footer, image, headerClass, footerClass;
    const qaAttributes = getQaAttrubutes(qa, 'header', 'footer', 'body', 'content');
    function handleChild(child) {
        switch (child.type) {
            case Header:
                header = child.props.children;
                image = child.props.image;
                headerClass = child.props.className;
                break;
            case Content:
                content = child.props.children;
                break;
            case Footer:
                footer = child.props.children;
                footerClass = child.props.className;
                break;
        }
    }
    React.Children.toArray(children).forEach((child) => {
        if (React.isValidElement(child)) {
            handleChild(child);
        }
    });
    const cardContent = (_jsxs(React.Fragment, { children: [(header || image) && (_jsx(BackgroundImage, { className: b('header', headerClass), ...(typeof image === 'string' ? { src: image } : image), qa: qaAttributes.header, children: _jsx("div", { className: b('header-content'), children: header }) })), _jsxs("div", { className: b('body', bodyClassName), "data-qa": qaAttributes.body, children: [_jsx("div", { className: b('content', contentClassName), "data-qa": qaAttributes.content, children: content }), footer && (_jsx("div", { className: b('footer', footerClass), "data-qa": qaAttributes.footer, children: footer }))] })] }));
    const fullClassName = b({ border }, className);
    const onClick = () => {
        handleAnalytics(analyticsEvents);
    };
    return url ? (_jsx(RouterLink, { href: url, children: _jsx(Link, { href: url, target: target, rel: target === '_blank' ? 'noopener noreferrer' : undefined, className: fullClassName, onClick: onClick, title: urlTitle, extraProps: {
                draggable: false,
                onDragStart: (e) => e.preventDefault(),
                ...extraProps,
            }, qa: qa, children: cardContent }) })) : (_jsx("div", { className: fullClassName, "data-qa": qa, ...extraProps, children: cardContent }));
};
Layout.Header = Header;
Layout.Content = Content;
Layout.Footer = Footer;
export default Layout;
//# sourceMappingURL=CardBase.js.map
