"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CardHeader = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_styles_1 = require("@patternfly/react-styles");
const card_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Card/card"));
const Card_1 = require("./Card");
const CardHeaderMain_1 = require("./CardHeaderMain");
const CardActions_1 = require("./CardActions");
const CardSelectableActions_1 = require("./CardSelectableActions");
const Button_1 = require("../Button");
const angle_right_icon_1 = tslib_1.__importDefault(require('@patternfly/react-icons/dist/js/icons/angle-right-icon'));
const Radio_1 = require("../Radio");
const Checkbox_1 = require("../Checkbox");
const GenerateId_1 = require("../../helpers/GenerateId/GenerateId");
const CardHeader = (_a) => {
    var { children, className, actions, selectableActions, id, onExpand, toggleButtonProps, isToggleRightAligned } = _a, props = tslib_1.__rest(_a, ["children", "className", "actions", "selectableActions", "id", "onExpand", "toggleButtonProps", "isToggleRightAligned"]);
    return (React.createElement(GenerateId_1.GenerateId, null, (randomId) => (React.createElement(Card_1.CardContext.Consumer, null, ({ cardId, isClickable, isSelectable, isSelected, isDisabled: isCardDisabled }) => {
        const cardHeaderToggle = (React.createElement("div", { className: (0, react_styles_1.css)(card_1.default.cardHeaderToggle) },
            React.createElement(Button_1.Button, Object.assign({ variant: "plain", type: "button", onClick: (evt) => {
                    onExpand(evt, cardId);
                } }, toggleButtonProps, { icon: React.createElement("span", { className: (0, react_styles_1.css)(card_1.default.cardHeaderToggleIcon) },
                    React.createElement(angle_right_icon_1.default, null)) }))));
        const isClickableOrSelectableOnly = (isClickable && !isSelectable) || (isSelectable && !isClickable);
        if ((actions === null || actions === void 0 ? void 0 : actions.actions) && isClickableOrSelectableOnly) {
            // eslint-disable-next-line no-console
            console.error(`Card: ${isClickable ? 'Clickable' : 'Selectable'} only cards should not contain any other actions. If you wish to include additional actions, use a clickable and selectable card.`);
        }
        const isClickableOnlyCard = isClickable && !isSelectable;
        if ((isClickableOnlyCard || isSelectable) &&
            !(selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.selectableActionAriaLabel) &&
            !(selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.selectableActionAriaLabelledby)) {
            // eslint-disable-next-line no-console
            console.error(`Card: ${isClickableOnlyCard ? 'Clickable-only cards' : 'Cards with a selectable input'} must have either the selectableActions.selectableActionAriaLabel or selectableActions.selectableActionAriaLabelledby prop passed in order to provide an accessible name to the clickable element.`);
        }
        const SelectableCardInput = (selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.variant) === 'single' ? Radio_1.Radio : Checkbox_1.Checkbox;
        const getSelectableProps = () => {
            var _a, _b;
            return (Object.assign({ className: (0, react_styles_1.css)('pf-m-standalone'), inputClassName: (0, react_styles_1.css)((selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.isHidden) && 'pf-v6-screen-reader'), label: React.createElement(React.Fragment, null), 'aria-label': selectableActions.selectableActionAriaLabel, 'aria-labelledby': selectableActions.selectableActionAriaLabelledby, id: (_a = selectableActions.selectableActionId) !== null && _a !== void 0 ? _a : `card-selectable-${randomId}`, name: selectableActions.name, isDisabled: isCardDisabled, onChange: selectableActions.onChange, isChecked: (_b = selectableActions.isChecked) !== null && _b !== void 0 ? _b : isSelected }, selectableActions.selectableActionProps));
        };
        const isClickableLinkCard = (selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.to) !== undefined;
        const ClickableCardComponent = isClickableLinkCard ? 'a' : 'button';
        const getClickableProps = () => {
            const isDisabledLinkCard = isCardDisabled && isClickableLinkCard;
            const baseProps = Object.assign({ className: (0, react_styles_1.css)('pf-v6-c-card__clickable-action', isDisabledLinkCard && card_1.default.modifiers.disabled, (selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.isHidden) && 'pf-v6-screen-reader'), id: selectableActions.selectableActionId, 'aria-label': selectableActions.selectableActionAriaLabel, 'aria-labelledby': selectableActions.selectableActionAriaLabelledby }, selectableActions.selectableActionProps);
            if (isClickableLinkCard) {
                return Object.assign(Object.assign(Object.assign(Object.assign({}, baseProps), { href: selectableActions.to }), (isCardDisabled && { tabIndex: -1, 'aria-disabled': true })), (selectableActions.isExternalLink && { target: '_blank' }));
            }
            return Object.assign(Object.assign({}, baseProps), { type: 'button', disabled: isCardDisabled, onClick: selectableActions.onClickAction });
        };
        return (React.createElement("div", Object.assign({ className: (0, react_styles_1.css)(card_1.default.cardHeader, isToggleRightAligned && card_1.default.modifiers.toggleRight, className), id: id }, props),
            onExpand && !isToggleRightAligned && cardHeaderToggle,
            (actions || (selectableActions && (isClickable || isSelectable))) && (React.createElement(CardActions_1.CardActions, { className: actions === null || actions === void 0 ? void 0 : actions.className, hasNoOffset: (actions === null || actions === void 0 ? void 0 : actions.hasNoOffset) || (selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.hasNoOffset) }, actions === null || actions === void 0 ? void 0 :
                actions.actions,
                selectableActions && (isClickable || isSelectable) && (React.createElement(CardSelectableActions_1.CardSelectableActions, { className: selectableActions === null || selectableActions === void 0 ? void 0 : selectableActions.className },
                    isSelectable && React.createElement(SelectableCardInput, Object.assign({}, getSelectableProps())),
                    isClickableOnlyCard && React.createElement(ClickableCardComponent, Object.assign({}, getClickableProps())))))),
            children && React.createElement(CardHeaderMain_1.CardHeaderMain, null, children),
            onExpand && isToggleRightAligned && cardHeaderToggle));
    }))));
};
exports.CardHeader = CardHeader;
exports.CardHeader.displayName = 'CardHeader';
//# sourceMappingURL=CardHeader.js.map