import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { getTranslation } from '@payloadcms/translations';
import { fieldAffectsData, fieldIsHiddenOrDisabled } from 'payload/shared';
import React from 'react';
import { RenderServerComponent } from '../elements/RenderServerComponent/index.js';
import { GroupByHeader, GroupByPageControls, OrderableTable, Pill, SelectAll, SelectionProvider, SelectRow, SortHeader, SortRow, Table } from // eslint-disable-next-line payload/no-imports-from-exports-dir -- these MUST reference the exports dir: https://github.com/payloadcms/payload/issues/12002#issuecomment-2791493587
'../exports/client/index.js';
import { filterFieldsWithPermissions } from '../providers/TableColumns/buildColumnState/filterFieldsWithPermissions.js';
import { buildColumnState } from '../providers/TableColumns/buildColumnState/index.js';
export const renderFilters = (fields, importMap)=>fields.reduce((acc, field)=>{
        if (fieldIsHiddenOrDisabled(field)) {
            return acc;
        }
        if ('name' in field && field.admin?.components?.Filter) {
            acc.set(field.name, RenderServerComponent({
                Component: field.admin.components?.Filter,
                importMap
            }));
        }
        return acc;
    }, new Map());
export const renderTable = ({ clientCollectionConfig, clientConfig, collectionConfig, collections, columns, customCellProps, data, enableRowSelections, fieldPermissions, groupByFieldPath, groupByValue, heading, i18n, key = 'table', orderableFieldName, payload, query, renderRowTypes, req, tableAppearance, useAsTitle, viewType })=>{
    // Ensure that columns passed as args comply with the field config, i.e. `hidden`, `disableListColumn`, etc.
    let columnState;
    let clientFields = clientCollectionConfig?.fields || [];
    let serverFields = collectionConfig?.fields || [];
    const isPolymorphic = collections;
    const isGroupingBy = Boolean(collectionConfig?.admin?.groupBy && query?.groupBy);
    if (isPolymorphic) {
        clientFields = [];
        serverFields = [];
        for (const collection of collections){
            const clientCollectionConfig = clientConfig.collections.find((each)=>each.slug === collection);
            for (const field of filterFieldsWithPermissions({
                fieldPermissions,
                fields: clientCollectionConfig.fields
            })){
                if (fieldAffectsData(field)) {
                    if (clientFields.some((each)=>fieldAffectsData(each) && each.name === field.name)) {
                        continue;
                    }
                }
                clientFields.push(field);
            }
            const serverCollectionConfig = payload.collections[collection].config;
            for (const field of filterFieldsWithPermissions({
                fieldPermissions,
                fields: serverCollectionConfig.fields
            })){
                if (fieldAffectsData(field)) {
                    if (serverFields.some((each)=>fieldAffectsData(each) && each.name === field.name)) {
                        continue;
                    }
                }
                serverFields.push(field);
            }
        }
    }
    const sharedArgs = {
        clientFields,
        columns,
        enableRowSelections,
        fieldPermissions,
        i18n,
        // sortColumnProps,
        customCellProps,
        payload,
        req,
        serverFields,
        useAsTitle,
        viewType
    };
    if (isPolymorphic) {
        columnState = buildColumnState({
            ...sharedArgs,
            collectionSlug: undefined,
            dataType: 'polymorphic',
            docs: data?.docs || []
        });
    } else {
        columnState = buildColumnState({
            ...sharedArgs,
            collectionSlug: clientCollectionConfig.slug,
            dataType: 'monomorphic',
            docs: data?.docs || []
        });
    }
    const columnsToUse = [
        ...columnState
    ];
    if (renderRowTypes) {
        columnsToUse.unshift({
            accessor: 'collection',
            active: true,
            field: {
                admin: {
                    disabled: true
                },
                hidden: true
            },
            Heading: i18n.t('version:type'),
            renderedCells: (data?.docs || []).map((doc, i)=>/*#__PURE__*/ _jsxDEV(Pill, {
                    size: "small",
                    children: getTranslation(collections ? payload.collections[doc.relationTo].config.labels.singular : clientCollectionConfig.labels.singular, i18n)
                }, i, false, {
                    fileName: "src/utilities/renderTable.tsx",
                    lineNumber: 224,
                    columnNumber: 9
                }, this))
        });
    }
    if (enableRowSelections) {
        columnsToUse.unshift({
            accessor: '_select',
            active: true,
            field: {
                admin: {
                    disabled: true
                },
                hidden: true
            },
            Heading: /*#__PURE__*/ _jsxDEV(SelectAll, {}, void 0, false, {
                fileName: "src/utilities/renderTable.tsx",
                lineNumber: 246,
                columnNumber: 16
            }, this),
            renderedCells: (data?.docs || []).map((_, i)=>/*#__PURE__*/ _jsxDEV(SelectRow, {
                    rowData: data?.docs[i]
                }, i, false, {
                    fileName: "src/utilities/renderTable.tsx",
                    lineNumber: 248,
                    columnNumber: 9
                }, this))
        });
    }
    if (isGroupingBy) {
        return {
            columnState,
            // key is required since Next.js 15.2.0 to prevent React key error
            Table: /*#__PURE__*/ _jsxDEV("div", {
                className: [
                    'table-wrap',
                    groupByValue !== undefined && `table-wrap--group-by`
                ].filter(Boolean).join(' '),
                children: /*#__PURE__*/ _jsxDEV(SelectionProvider, {
                    docs: data?.docs || [],
                    totalDocs: data?.totalDocs || 0,
                    children: [
                        /*#__PURE__*/ _jsxDEV(GroupByHeader, {
                            collectionConfig: clientCollectionConfig,
                            groupByFieldPath: groupByFieldPath,
                            groupByValue: groupByValue,
                            heading: heading
                        }, void 0, false, {
                            fileName: "src/utilities/renderTable.tsx",
                            lineNumber: 265,
                            columnNumber: 13
                        }, this),
                        /*#__PURE__*/ _jsxDEV(Table, {
                            appearance: tableAppearance,
                            columns: columnsToUse,
                            data: data?.docs || []
                        }, void 0, false, {
                            fileName: "src/utilities/renderTable.tsx",
                            lineNumber: 271,
                            columnNumber: 13
                        }, this),
                        /*#__PURE__*/ _jsxDEV(GroupByPageControls, {
                            collectionConfig: clientCollectionConfig,
                            data: data,
                            groupByValue: groupByValue
                        }, void 0, false, {
                            fileName: "src/utilities/renderTable.tsx",
                            lineNumber: 272,
                            columnNumber: 13
                        }, this)
                    ]
                }, void 0, true, {
                    fileName: "src/utilities/renderTable.tsx",
                    lineNumber: 264,
                    columnNumber: 11
                }, this)
            }, key, false, {
                fileName: "src/utilities/renderTable.tsx",
                lineNumber: 258,
                columnNumber: 9
            }, this)
        };
    }
    if (!orderableFieldName) {
        return {
            columnState,
            // key is required since Next.js 15.2.0 to prevent React key error
            Table: /*#__PURE__*/ _jsxDEV("div", {
                className: "table-wrap",
                children: /*#__PURE__*/ _jsxDEV(Table, {
                    appearance: tableAppearance,
                    columns: columnsToUse,
                    data: data?.docs || []
                }, void 0, false, {
                    fileName: "src/utilities/renderTable.tsx",
                    lineNumber: 289,
                    columnNumber: 11
                }, this)
            }, key, false, {
                fileName: "src/utilities/renderTable.tsx",
                lineNumber: 288,
                columnNumber: 9
            }, this)
        };
    }
    columnsToUse.unshift({
        accessor: '_dragHandle',
        active: true,
        field: {
            admin: {
                disabled: true
            },
            hidden: true
        },
        Heading: /*#__PURE__*/ _jsxDEV(SortHeader, {}, void 0, false, {
            fileName: "src/utilities/renderTable.tsx",
            lineNumber: 304,
            columnNumber: 14
        }, this),
        renderedCells: (data?.docs || []).map((_, i)=>/*#__PURE__*/ _jsxDEV(SortRow, {}, i, false, {
                fileName: "src/utilities/renderTable.tsx",
                lineNumber: 305,
                columnNumber: 53
            }, this))
    });
    return {
        columnState,
        // key is required since Next.js 15.2.0 to prevent React key error
        Table: /*#__PURE__*/ _jsxDEV("div", {
            className: "table-wrap",
            children: /*#__PURE__*/ _jsxDEV(OrderableTable, {
                appearance: tableAppearance,
                collection: clientCollectionConfig,
                columns: columnsToUse,
                data: data?.docs || []
            }, void 0, false, {
                fileName: "src/utilities/renderTable.tsx",
                lineNumber: 313,
                columnNumber: 9
            }, this)
        }, key, false, {
            fileName: "src/utilities/renderTable.tsx",
            lineNumber: 312,
            columnNumber: 7
        }, this)
    };
};

//# sourceMappingURL=renderTable.js.map