import React__default from 'react';
import { u as useDataListContext } from './DataListContext-es.js';
import { Link } from 'react-router-dom';
import { s as styles } from './DataList.module-es.js';
import { u as useDataListLayoutActionsContext } from './DataListLayoutContext-es2.js';

function DataListItemClickableInternal({ onClick, url, to, children, }) {
    const { activeItem } = useDataListLayoutActionsContext();
    if (!activeItem)
        return React__default.createElement(React__default.Fragment, null, children);
    if (to) {
        const computedTo = typeof to === "string" ? to : to(activeItem);
        return (React__default.createElement(Link, { className: styles.listItemClickable, to: computedTo, onClick: handleClick }, children));
    }
    if (url) {
        const href = typeof url === "string" ? url : url(activeItem);
        return (React__default.createElement("a", { className: styles.listItemClickable, href: href, onClick: handleClick }, children));
    }
    if (onClick) {
        return (
        // A button can be nested within the list item. To prevent a button inside
        // button error, we need to manually declare a div to be a button
        React__default.createElement("div", { role: "button", tabIndex: 0, className: styles.listItemClickable, onClick: handleClick, onKeyDown: e => {
                if (e.key === "Enter" || e.key === " ") {
                    e.preventDefault();
                    handleClick();
                }
            } }, children));
    }
    return React__default.createElement(React__default.Fragment, null, children);
    function handleClick(evt) {
        activeItem && (onClick === null || onClick === void 0 ? void 0 : onClick(activeItem, evt));
    }
}

function DataListItemClickable({ children }) {
    const { itemActionComponent } = useDataListContext();
    if (itemActionComponent) {
        const props = itemActionComponent.props;
        return (React__default.createElement(DataListItemClickableInternal, Object.assign({}, props), children));
    }
    return React__default.createElement(React__default.Fragment, null, children);
}

export { DataListItemClickable as D };
