'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { getTranslation } from '@payloadcms/translations';
import { fieldAffectsData, fieldIsID } from 'payload/shared';
import React from 'react';
// TODO: abstract this out to support all routers
import { useConfig } from '../../../providers/Config/index.js';
import { useTranslation } from '../../../providers/Translation/index.js';
import { formatAdminURL } from '../../../utilities/formatAdminURL.js';
import { getDisplayedFieldValue } from '../../../utilities/getDisplayedFieldValue.js';
import { isValidReactElement } from '../../../utilities/isValidReactElement.js';
import { Link } from '../../Link/index.js';
import { CodeCell } from './fields/Code/index.js';
import { cellComponents } from './fields/index.js';
export const DefaultCell = (props)=>{
    const { cellData, className: classNameFromProps, collectionSlug, field, field: { admin }, link, linkURL, onClick: onClickFromProps, rowData, viewType } = props;
    const { i18n } = useTranslation();
    const { config: { routes: { admin: adminRoute } }, getEntityConfig } = useConfig();
    const collectionConfig = getEntityConfig({
        collectionSlug
    });
    const classNameFromConfigContext = admin && 'className' in admin ? admin.className : undefined;
    const className = classNameFromProps || (field.admin && 'className' in field.admin ? field.admin.className : null) || classNameFromConfigContext;
    const onClick = onClickFromProps;
    let WrapElement = 'span';
    const wrapElementProps = {
        className
    };
    if (link) {
        wrapElementProps.prefetch = false;
        WrapElement = Link;
        // Use custom linkURL if provided, otherwise use default URL generation
        if (linkURL) {
            wrapElementProps.href = linkURL;
        } else {
            wrapElementProps.href = collectionConfig?.slug ? formatAdminURL({
                adminRoute,
                path: `/collections/${collectionConfig?.slug}${viewType === 'trash' ? '/trash' : ''}/${encodeURIComponent(rowData.id)}`
            }) : '';
        }
    }
    if (typeof onClick === 'function') {
        WrapElement = 'button';
        wrapElementProps.type = 'button';
        wrapElementProps.onClick = ()=>{
            onClick({
                cellData,
                collectionSlug: collectionConfig?.slug,
                rowData
            });
        };
    }
    if (fieldIsID(field)) {
        return /*#__PURE__*/ _jsxDEV(WrapElement, {
            ...wrapElementProps,
            children: /*#__PURE__*/ _jsxDEV(CodeCell, {
                cellData: `ID: ${cellData}`,
                collectionConfig: collectionConfig,
                collectionSlug: collectionSlug,
                field: {
                    ...field,
                    type: 'code'
                },
                nowrap: true,
                rowData: rowData
            }, void 0, false, {
                fileName: "src/elements/Table/DefaultCell/index.tsx",
                lineNumber: 95,
                columnNumber: 9
            }, this)
        }, void 0, false, {
            fileName: "src/elements/Table/DefaultCell/index.tsx",
            lineNumber: 94,
            columnNumber: 7
        }, this);
    }
    const displayedValue = getDisplayedFieldValue(cellData, field, i18n);
    const DefaultCellComponent = typeof cellData !== 'undefined' && cellComponents[field.type];
    let CellComponent = null;
    // Handle JSX labels before using DefaultCellComponent
    if (isValidReactElement(displayedValue)) {
        CellComponent = displayedValue;
    } else if (DefaultCellComponent) {
        CellComponent = /*#__PURE__*/ _jsxDEV(DefaultCellComponent, {
            cellData: cellData,
            rowData: rowData,
            ...props
        }, void 0, false, {
            fileName: "src/elements/Table/DefaultCell/index.tsx",
            lineNumber: 121,
            columnNumber: 21
        }, this);
    } else if (!DefaultCellComponent) {
        // DefaultCellComponent does not exist for certain field types like `text`
        if (collectionConfig?.upload && fieldAffectsData(field) && field.name === 'filename' && field.type === 'text') {
            const FileCellComponent = cellComponents.File;
            CellComponent = /*#__PURE__*/ _jsxDEV(FileCellComponent, {
                cellData: cellData,
                rowData: rowData,
                ...props,
                collectionConfig: collectionConfig,
                field: field
            }, void 0, false, {
                fileName: "src/elements/Table/DefaultCell/index.tsx",
                lineNumber: 133,
                columnNumber: 9
            }, this);
        } else {
            return /*#__PURE__*/ _jsxDEV(WrapElement, {
                ...wrapElementProps,
                children: [
                    (displayedValue === '' || typeof displayedValue === 'undefined' || displayedValue === null) && i18n.t('general:noLabel', {
                        label: getTranslation(('label' in field ? field.label : null) || 'data', i18n)
                    }),
                    typeof displayedValue === 'string' && displayedValue,
                    typeof displayedValue === 'number' && displayedValue,
                    typeof displayedValue === 'object' && displayedValue !== null && JSON.stringify(displayedValue)
                ]
            }, void 0, true, {
                fileName: "src/elements/Table/DefaultCell/index.tsx",
                lineNumber: 143,
                columnNumber: 9
            }, this);
        }
    }
    if ((field.type === 'select' || field.type === 'radio') && field.options.length && cellData) {
        const classes = Array.isArray(cellData) ? cellData.map((value)=>`selected--${value}`).join(' ') : `selected--${cellData}`;
        const className = [
            wrapElementProps.className,
            classes
        ].filter(Boolean).join(' ');
        return /*#__PURE__*/ _jsxDEV(WrapElement, {
            ...wrapElementProps,
            className: className,
            children: CellComponent
        }, void 0, false, {
            fileName: "src/elements/Table/DefaultCell/index.tsx",
            lineNumber: 168,
            columnNumber: 7
        }, this);
    }
    return /*#__PURE__*/ _jsxDEV(WrapElement, {
        ...wrapElementProps,
        children: CellComponent
    }, void 0, false, {
        fileName: "src/elements/Table/DefaultCell/index.tsx",
        lineNumber: 174,
        columnNumber: 10
    }, this);
};

//# sourceMappingURL=index.js.map