'use client';
import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "react/jsx-dev-runtime";
import { getTranslation } from '@payloadcms/translations';
import { extractID } from 'payload/shared';
import React from 'react';
import { DocumentIcon } from '../../../icons/Document/index.js';
import { useConfig } from '../../../providers/Config/index.js';
import { useFolder } from '../../../providers/Folders/index.js';
import { useTranslation } from '../../../providers/Translation/index.js';
import { formatDate } from '../../../utilities/formatDocTitle/formatDateTitle.js';
import { ColoredFolderIcon } from '../ColoredFolderIcon/index.js';
import { DraggableTableRow } from '../DraggableTableRow/index.js';
import { SimpleTable, TableHeader } from '../SimpleTable/index.js';
import './index.scss';
const baseClass = 'folder-file-table';
export function FolderFileTable({ showRelationCell = true }) {
    const { checkIfItemIsDisabled, documents, focusedRowIndex, onItemClick, onItemKeyPress, selectedItemKeys, subfolders } = useFolder();
    const { config } = useConfig();
    const { i18n, t } = useTranslation();
    const [relationToMap] = React.useState(()=>{
        const map = {};
        config.collections.forEach((collection)=>{
            map[collection.slug] = {
                plural: getTranslation(collection.labels?.plural, i18n),
                singular: getTranslation(collection.labels?.singular, i18n)
            };
        });
        return map;
    });
    const [columns] = React.useState(()=>{
        const columnsToShow = [
            {
                name: 'name',
                label: t('general:name')
            },
            {
                name: 'createdAt',
                label: t('general:createdAt')
            },
            {
                name: 'updatedAt',
                label: t('general:updatedAt')
            }
        ];
        if (showRelationCell) {
            columnsToShow.push({
                name: 'type',
                label: t('version:type')
            });
        }
        return columnsToShow;
    });
    return /*#__PURE__*/ _jsxDEV(SimpleTable, {
        headerCells: columns.map(({ name, label })=>/*#__PURE__*/ _jsxDEV(TableHeader, {
                children: label
            }, name, false, {
                fileName: "src/elements/FolderView/FolderFileTable/index.tsx",
                lineNumber: 76,
                columnNumber: 9
            }, void 0)),
        tableRows: [
            ...subfolders.map((subfolder, rowIndex)=>{
                const { itemKey, relationTo, value } = subfolder;
                const subfolderID = extractID(value);
                return /*#__PURE__*/ _jsxDEV(DraggableTableRow, {
                    columns: columns.map(({ name }, index)=>{
                        let cellValue = '—';
                        if (name === 'name' && value._folderOrDocumentTitle !== undefined) {
                            cellValue = value._folderOrDocumentTitle;
                        }
                        if ((name === 'createdAt' || name === 'updatedAt') && value[name]) {
                            cellValue = formatDate({
                                date: value[name],
                                i18n,
                                pattern: config.admin.dateFormat
                            });
                        }
                        if (name === 'type') {
                            cellValue = /*#__PURE__*/ _jsxDEV(_Fragment, {
                                children: [
                                    relationToMap[relationTo]?.singular || relationTo,
                                    Array.isArray(subfolder.value?.folderType) ? subfolder.value?.folderType.reduce((acc, slug, index)=>{
                                        if (index === 0) {
                                            return ` — ${relationToMap[slug]?.plural || slug}`;
                                        }
                                        if (index > 0) {
                                            return `${acc}, ${relationToMap[slug]?.plural || slug}`;
                                        }
                                        return acc;
                                    }, '') : ''
                                ]
                            }, void 0, true);
                        }
                        if (index === 0) {
                            return /*#__PURE__*/ _jsxDEV("span", {
                                className: `${baseClass}__cell-with-icon`,
                                children: [
                                    /*#__PURE__*/ _jsxDEV(ColoredFolderIcon, {}, void 0, false, {
                                        fileName: "src/elements/FolderView/FolderFileTable/index.tsx",
                                        lineNumber: 121,
                                        columnNumber: 23
                                    }, void 0),
                                    cellValue
                                ]
                            }, `${itemKey}-${name}`, true, {
                                fileName: "src/elements/FolderView/FolderFileTable/index.tsx",
                                lineNumber: 120,
                                columnNumber: 21
                            }, void 0);
                        } else {
                            return cellValue;
                        }
                    }),
                    disabled: checkIfItemIsDisabled(subfolder),
                    dragData: {
                        id: subfolderID,
                        type: 'folder'
                    },
                    id: subfolderID,
                    isDroppable: true,
                    isFocused: focusedRowIndex === rowIndex,
                    isSelected: selectedItemKeys.has(itemKey),
                    isSelecting: selectedItemKeys.size > 0,
                    itemKey: itemKey,
                    onClick: (event)=>{
                        void onItemClick({
                            event,
                            index: rowIndex,
                            item: subfolder
                        });
                    },
                    onKeyDown: (event)=>{
                        void onItemKeyPress({
                            event,
                            index: rowIndex,
                            item: subfolder
                        });
                    }
                }, `${rowIndex}-${itemKey}`, false, {
                    fileName: "src/elements/FolderView/FolderFileTable/index.tsx",
                    lineNumber: 84,
                    columnNumber: 13
                }, void 0);
            }),
            ...documents.map((document, unadjustedIndex)=>{
                const { itemKey, relationTo, value } = document;
                const documentID = extractID(value);
                const rowIndex = unadjustedIndex + subfolders.length;
                return /*#__PURE__*/ _jsxDEV(DraggableTableRow, {
                    columns: columns.map(({ name }, index)=>{
                        let cellValue = '—';
                        if (name === 'name' && value._folderOrDocumentTitle !== undefined) {
                            cellValue = value._folderOrDocumentTitle;
                        }
                        if ((name === 'createdAt' || name === 'updatedAt') && value[name]) {
                            cellValue = formatDate({
                                date: value[name],
                                i18n,
                                pattern: config.admin.dateFormat
                            });
                        }
                        if (name === 'type') {
                            cellValue = relationToMap[relationTo]?.singular || relationTo;
                        }
                        if (index === 0) {
                            return /*#__PURE__*/ _jsxDEV("span", {
                                className: `${baseClass}__cell-with-icon`,
                                children: [
                                    /*#__PURE__*/ _jsxDEV(DocumentIcon, {}, void 0, false, {
                                        fileName: "src/elements/FolderView/FolderFileTable/index.tsx",
                                        lineNumber: 187,
                                        columnNumber: 23
                                    }, void 0),
                                    cellValue
                                ]
                            }, `${itemKey}-${name}`, true, {
                                fileName: "src/elements/FolderView/FolderFileTable/index.tsx",
                                lineNumber: 186,
                                columnNumber: 21
                            }, void 0);
                        } else {
                            return cellValue;
                        }
                    }),
                    disabled: checkIfItemIsDisabled(document),
                    dragData: {
                        id: documentID,
                        type: 'document'
                    },
                    id: documentID,
                    isFocused: focusedRowIndex === rowIndex,
                    isSelected: selectedItemKeys.has(itemKey),
                    isSelecting: selectedItemKeys.size > 0,
                    itemKey: itemKey,
                    onClick: (event)=>{
                        void onItemClick({
                            event,
                            index: rowIndex,
                            item: document
                        });
                    },
                    onKeyDown: (event)=>{
                        void onItemKeyPress({
                            event,
                            index: rowIndex,
                            item: document
                        });
                    }
                }, `${rowIndex}-${itemKey}`, false, {
                    fileName: "src/elements/FolderView/FolderFileTable/index.tsx",
                    lineNumber: 165,
                    columnNumber: 13
                }, void 0);
            })
        ]
    }, void 0, false, {
        fileName: "src/elements/FolderView/FolderFileTable/index.tsx",
        lineNumber: 74,
        columnNumber: 5
    }, this);
}

//# sourceMappingURL=index.js.map