'use strict';

var React = require('react');
var DataListContext = require('./DataListContext-cjs.js');
var reactRouterDom = require('react-router-dom');
var DataList_module = require('./DataList.module-cjs.js');
var DataListLayoutContext = require('./DataListLayoutContext-cjs2.js');

function DataListItemClickableInternal({ onClick, url, to, children, }) {
    const { activeItem } = DataListLayoutContext.useDataListLayoutActionsContext();
    if (!activeItem)
        return React.createElement(React.Fragment, null, children);
    if (to) {
        const computedTo = typeof to === "string" ? to : to(activeItem);
        return (React.createElement(reactRouterDom.Link, { className: DataList_module.styles.listItemClickable, to: computedTo, onClick: handleClick }, children));
    }
    if (url) {
        const href = typeof url === "string" ? url : url(activeItem);
        return (React.createElement("a", { className: DataList_module.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.createElement("div", { role: "button", tabIndex: 0, className: DataList_module.styles.listItemClickable, onClick: handleClick, onKeyDown: e => {
                if (e.key === "Enter" || e.key === " ") {
                    e.preventDefault();
                    handleClick();
                }
            } }, children));
    }
    return React.createElement(React.Fragment, null, children);
    function handleClick(evt) {
        activeItem && (onClick === null || onClick === void 0 ? void 0 : onClick(activeItem, evt));
    }
}

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

exports.DataListItemClickable = DataListItemClickable;
