'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { fieldIsHiddenOrDisabled, getFieldPaths, getFieldPermissions } from 'payload/shared';
import React from 'react';
import { RenderIfInViewport } from '../../elements/RenderIfInViewport/index.js';
import { useOperation } from '../../providers/Operation/index.js';
import './index.scss';
import { FieldPathContext } from './context.js';
import { RenderField } from './RenderField.js';
const baseClass = 'render-fields';
export const RenderFields = (props)=>{
    const { className, fields, forceRender, margins, parentIndexPath, parentPath, parentSchemaPath, permissions, readOnly: readOnlyFromParent } = props;
    const operation = useOperation();
    if (fields && fields.length > 0) {
        return /*#__PURE__*/ _jsxDEV(RenderIfInViewport, {
            className: [
                baseClass,
                className,
                margins && `${baseClass}--margins-${margins}`,
                margins === false && `${baseClass}--margins-none`
            ].filter(Boolean).join(' '),
            forceRender: forceRender,
            children: fields.map((field, i)=>{
                // For sidebar fields in the main fields array, `field` will be `null`, and visa versa
                // This is to keep the order of the fields consistent and maintain the correct index paths for the main fields (i)
                if (!field || fieldIsHiddenOrDisabled(field)) {
                    return null;
                }
                const { operation: hasOperationPermission, permissions: fieldPermissions, read: hasReadPermission } = getFieldPermissions({
                    field,
                    operation,
                    parentName: parentPath?.includes('.') ? parentPath.split('.')[parentPath.split('.').length - 1] : parentPath,
                    permissions
                });
                // If the user cannot read the field, then filter it out
                // This is different from `admin.readOnly` which is executed based on `operation`
                if ('name' in field && !hasReadPermission) {
                    return null;
                }
                // `admin.readOnly` displays the value but prevents the field from being edited
                let isReadOnly = readOnlyFromParent || field?.admin?.readOnly;
                // If parent field is `readOnly: true`, but this field is `readOnly: false`, the field should still be editable
                if (isReadOnly && field.admin?.readOnly === false) {
                    isReadOnly = false;
                }
                // If the user does not have access at the operation level, to begin with, force it to be read-only
                if ('name' in field && !hasOperationPermission) {
                    isReadOnly = true;
                }
                const { indexPath, path, schemaPath } = getFieldPaths({
                    field,
                    index: i,
                    parentIndexPath,
                    parentPath,
                    parentSchemaPath
                });
                return /*#__PURE__*/ _jsxDEV(FieldPathContext, {
                    value: path,
                    children: /*#__PURE__*/ _jsxDEV(RenderField, {
                        clientFieldConfig: field,
                        forceRender: forceRender,
                        indexPath: indexPath,
                        parentPath: parentPath,
                        parentSchemaPath: parentSchemaPath,
                        path: path,
                        permissions: fieldPermissions,
                        readOnly: isReadOnly,
                        schemaPath: schemaPath
                    }, void 0, false, {
                        fileName: "src/forms/RenderFields/index.tsx",
                        lineNumber: 97,
                        columnNumber: 15
                    }, this)
                }, `${path}-${i}`, false, {
                    fileName: "src/forms/RenderFields/index.tsx",
                    lineNumber: 96,
                    columnNumber: 13
                }, this);
            })
        }, void 0, false, {
            fileName: "src/forms/RenderFields/index.tsx",
            lineNumber: 37,
            columnNumber: 7
        }, this);
    }
    return null;
};

//# sourceMappingURL=index.js.map