'use client';
import { jsxDEV as _jsxDEV, Fragment as _Fragment } from "react/jsx-dev-runtime";
import { getTranslation } from '@payloadcms/translations';
import React from 'react';
import { Collapsible } from '../../elements/Collapsible/index.js';
import { ErrorPill } from '../../elements/ErrorPill/index.js';
import { Pill } from '../../elements/Pill/index.js';
import { ShimmerEffect } from '../../elements/ShimmerEffect/index.js';
import { useFormSubmitted } from '../../forms/Form/context.js';
import { RenderFields } from '../../forms/RenderFields/index.js';
import { RowLabel } from '../../forms/RowLabel/index.js';
import { useThrottledValue } from '../../hooks/useThrottledValue.js';
import { useTranslation } from '../../providers/Translation/index.js';
import { RowActions } from './RowActions.js';
import { SectionTitle } from './SectionTitle/index.js';
const baseClass = 'blocks-field';
export const BlockRow = ({ addRow, attributes, block, blocks, copyRow, duplicateRow, errorCount, fields, hasMaxRows, isLoading: isLoadingFromProps, isSortable, Label, labels, listeners, moveRow, parentPath, pasteRow, path, permissions, readOnly, removeRow, row, rowCount, rowIndex, schemaPath, setCollapse, setNodeRef, transform })=>{
    const isLoading = useThrottledValue(isLoadingFromProps, 500);
    const { i18n } = useTranslation();
    const hasSubmitted = useFormSubmitted();
    const fieldHasErrors = hasSubmitted && errorCount > 0;
    const showBlockName = !block.admin?.disableBlockName;
    const classNames = [
        `${baseClass}__row`,
        fieldHasErrors ? `${baseClass}__row--has-errors` : `${baseClass}__row--no-errors`
    ].filter(Boolean).join(' ');
    let blockPermissions = true;
    if (permissions === true) {
        blockPermissions = true;
    } else {
        const permissionsBlockSpecific = permissions?.blocks?.[block.slug] || permissions?.blocks;
        if (permissionsBlockSpecific === true) {
            blockPermissions = true;
        } else if (permissionsBlockSpecific?.fields) {
            blockPermissions = permissionsBlockSpecific.fields;
        } else {
            // Check if we should fall back to read-only mode based on permission structure
            // This handles cases where field-level access control exists but block permissions were sanitized
            if (typeof permissions === 'object' && permissions && !permissionsBlockSpecific) {
                // If permissions object exists but has no block-specific permissions,
                // check if it has any restrictive characteristics
                const hasReadPermission = permissions.read === true;
                const missingCreateOrUpdate = !permissions.create || !permissions.update;
                const hasRestrictiveStructure = hasReadPermission && (missingCreateOrUpdate || typeof permissions === 'object' && Object.keys(permissions).length === 1 && permissions.read);
                if (hasRestrictiveStructure) {
                    blockPermissions = {
                        read: true
                    };
                } else {
                    blockPermissions = permissionsBlockSpecific?.fields;
                }
            } else {
                blockPermissions = permissionsBlockSpecific?.fields;
            }
        }
    }
    return /*#__PURE__*/ _jsxDEV("div", {
        id: `${parentPath?.split('.').join('-')}-row-${rowIndex}`,
        ref: setNodeRef,
        style: {
            transform
        },
        children: /*#__PURE__*/ _jsxDEV(Collapsible, {
            actions: !readOnly ? /*#__PURE__*/ _jsxDEV(RowActions, {
                addRow: addRow,
                blocks: blocks,
                blockType: row.blockType,
                copyRow: copyRow,
                duplicateRow: duplicateRow,
                fields: block.fields,
                hasMaxRows: hasMaxRows,
                isSortable: isSortable,
                labels: labels,
                moveRow: moveRow,
                pasteRow: pasteRow,
                removeRow: removeRow,
                rowCount: rowCount,
                rowIndex: rowIndex
            }, void 0, false, {
                fileName: "src/fields/Blocks/BlockRow.tsx",
                lineNumber: 145,
                columnNumber: 13
            }, void 0) : undefined,
            className: classNames,
            collapsibleStyle: fieldHasErrors ? 'error' : 'default',
            dragHandleProps: isSortable ? {
                id: row.id,
                attributes,
                listeners
            } : undefined,
            header: isLoading ? /*#__PURE__*/ _jsxDEV(ShimmerEffect, {
                height: "1rem",
                width: "8rem"
            }, void 0, false, {
                fileName: "src/fields/Blocks/BlockRow.tsx",
                lineNumber: 176,
                columnNumber: 13
            }, void 0) : /*#__PURE__*/ _jsxDEV("div", {
                className: `${baseClass}__block-header`,
                children: [
                    /*#__PURE__*/ _jsxDEV(RowLabel, {
                        CustomComponent: Label,
                        label: /*#__PURE__*/ _jsxDEV(_Fragment, {
                            children: [
                                /*#__PURE__*/ _jsxDEV("span", {
                                    className: `${baseClass}__block-number`,
                                    children: String(rowIndex + 1).padStart(2, '0')
                                }, void 0, false, {
                                    fileName: "src/fields/Blocks/BlockRow.tsx",
                                    lineNumber: 183,
                                    columnNumber: 21
                                }, void 0),
                                /*#__PURE__*/ _jsxDEV(Pill, {
                                    className: `${baseClass}__block-pill ${baseClass}__block-pill-${row.blockType}`,
                                    pillStyle: "white",
                                    size: "small",
                                    children: getTranslation(block.labels.singular, i18n)
                                }, void 0, false, {
                                    fileName: "src/fields/Blocks/BlockRow.tsx",
                                    lineNumber: 186,
                                    columnNumber: 21
                                }, void 0),
                                showBlockName && /*#__PURE__*/ _jsxDEV(SectionTitle, {
                                    path: `${path}.blockName`,
                                    readOnly: readOnly
                                }, void 0, false, {
                                    fileName: "src/fields/Blocks/BlockRow.tsx",
                                    lineNumber: 194,
                                    columnNumber: 23
                                }, void 0)
                            ]
                        }, void 0, true),
                        path: path,
                        rowNumber: rowIndex
                    }, void 0, false, {
                        fileName: "src/fields/Blocks/BlockRow.tsx",
                        lineNumber: 179,
                        columnNumber: 15
                    }, void 0),
                    fieldHasErrors && /*#__PURE__*/ _jsxDEV(ErrorPill, {
                        count: errorCount,
                        i18n: i18n,
                        withMessage: true
                    }, void 0, false, {
                        fileName: "src/fields/Blocks/BlockRow.tsx",
                        lineNumber: 201,
                        columnNumber: 34
                    }, void 0)
                ]
            }, void 0, true, {
                fileName: "src/fields/Blocks/BlockRow.tsx",
                lineNumber: 178,
                columnNumber: 13
            }, void 0),
            isCollapsed: row.collapsed,
            onToggle: (collapsed)=>setCollapse(row.id, collapsed),
            children: isLoading ? /*#__PURE__*/ _jsxDEV(ShimmerEffect, {}, void 0, false, {
                fileName: "src/fields/Blocks/BlockRow.tsx",
                lineNumber: 210,
                columnNumber: 11
            }, this) : /*#__PURE__*/ _jsxDEV(RenderFields, {
                className: `${baseClass}__fields`,
                fields: fields,
                margins: "small",
                parentIndexPath: "",
                parentPath: path,
                parentSchemaPath: schemaPath,
                permissions: blockPermissions,
                readOnly: readOnly
            }, void 0, false, {
                fileName: "src/fields/Blocks/BlockRow.tsx",
                lineNumber: 212,
                columnNumber: 11
            }, this)
        }, row.id, false, {
            fileName: "src/fields/Blocks/BlockRow.tsx",
            lineNumber: 142,
            columnNumber: 7
        }, this)
    }, `${parentPath}-row-${rowIndex}`, false, {
        fileName: "src/fields/Blocks/BlockRow.tsx",
        lineNumber: 134,
        columnNumber: 5
    }, this);
};

//# sourceMappingURL=BlockRow.js.map