"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Layout = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const uikit_1 = require("@gravity-ui/uikit");
const hooks_1 = require("../../gravity-blocks/hooks/index.js");
const models_1 = require("../../models/index.js");
const utils_1 = require("../../utils/index.js");
const BackgroundImage_1 = tslib_1.__importDefault(require("../BackgroundImage/BackgroundImage.js"));
const RouterLink_1 = tslib_1.__importDefault(require("../RouterLink/RouterLink.js"));
const b = (0, utils_1.block)('card-base-block');
const Header = () => null;
const Content = () => null;
const Footer = () => null;
const Layout = (props) => {
    const { className, bodyClassName, analyticsEvents, contentClassName, children, url, target, border = 'shadow', urlTitle, qa, extraProps = {}, } = props;
    const handleAnalytics = (0, hooks_1.useAnalytics)(models_1.DefaultEventNames.CardBase, url);
    let header, content, footer, image, headerClass, footerClass;
    const qaAttributes = (0, utils_1.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 = ((0, jsx_runtime_1.jsxs)(React.Fragment, { children: [(header || image) && ((0, jsx_runtime_1.jsx)(BackgroundImage_1.default, { className: b('header', headerClass), ...(typeof image === 'string' ? { src: image } : image), qa: qaAttributes.header, children: (0, jsx_runtime_1.jsx)("div", { className: b('header-content'), children: header }) })), (0, jsx_runtime_1.jsxs)("div", { className: b('body', bodyClassName), "data-qa": qaAttributes.body, children: [(0, jsx_runtime_1.jsx)("div", { className: b('content', contentClassName), "data-qa": qaAttributes.content, children: content }), footer && ((0, jsx_runtime_1.jsx)("div", { className: b('footer', footerClass), "data-qa": qaAttributes.footer, children: footer }))] })] }));
    const fullClassName = b({ border }, className);
    const onClick = () => {
        handleAnalytics(analyticsEvents);
    };
    return url ? ((0, jsx_runtime_1.jsx)(RouterLink_1.default, { href: url, children: (0, jsx_runtime_1.jsx)(uikit_1.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 }) })) : ((0, jsx_runtime_1.jsx)("div", { className: fullClassName, "data-qa": qa, ...extraProps, children: cardContent }));
};
exports.Layout = Layout;
exports.Layout.Header = Header;
exports.Layout.Content = Content;
exports.Layout.Footer = Footer;
exports.default = exports.Layout;
//# sourceMappingURL=CardBase.js.map
