'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { CodeEditor } from '../../elements/CodeEditor/index.js';
import { RenderCustomComponent } from '../../elements/RenderCustomComponent/index.js';
import { FieldDescription } from '../../fields/FieldDescription/index.js';
import { FieldError } from '../../fields/FieldError/index.js';
import { FieldLabel } from '../../fields/FieldLabel/index.js';
import { useField } from '../../forms/useField/index.js';
import { withCondition } from '../../forms/withCondition/index.js';
import { mergeFieldStyles } from '../mergeFieldStyles.js';
import { fieldBaseClass } from '../shared/index.js';
import './index.scss';
const prismToMonacoLanguageMap = {
    js: 'javascript',
    ts: 'typescript',
    tsx: 'typescript'
};
const baseClass = 'code-field';
const CodeFieldComponent = (props)=>{
    const { field, field: { admin: { className, description, editorOptions, editorProps, language = 'javascript' } = {}, label, localized, required }, onMount, path: pathFromProps, readOnly, validate } = props;
    const inputChangeFromRef = React.useRef('formState');
    const [recalculatedHeightAt, setRecalculatedHeightAt] = useState(Date.now());
    const memoizedValidate = useCallback((value, options)=>{
        if (typeof validate === 'function') {
            return validate(value, {
                ...options,
                required
            });
        }
    }, [
        validate,
        required
    ]);
    const { customComponents: { AfterInput, BeforeInput, Description, Error, Label } = {}, disabled, initialValue, path, setValue, showError, value } = useField({
        potentiallyStalePath: pathFromProps,
        validate: memoizedValidate
    });
    const stringValueRef = React.useRef((value || initialValue) !== undefined ? value ?? initialValue : undefined);
    const handleChange = useCallback((val)=>{
        if (readOnly || disabled) {
            return;
        }
        inputChangeFromRef.current = 'internalEditor';
        try {
            setValue(val ? val : null);
            stringValueRef.current = val;
        } catch (e) {
            setValue(val ? val : null);
            stringValueRef.current = val;
        }
    }, [
        readOnly,
        disabled,
        setValue
    ]);
    useEffect(()=>{
        if (inputChangeFromRef.current === 'formState') {
            stringValueRef.current = (value || initialValue) !== undefined ? value ?? initialValue : undefined;
            setRecalculatedHeightAt(Date.now());
        }
        inputChangeFromRef.current = 'formState';
    }, [
        initialValue,
        path,
        value
    ]);
    const styles = useMemo(()=>mergeFieldStyles(field), [
        field
    ]);
    return /*#__PURE__*/ _jsxDEV("div", {
        className: [
            fieldBaseClass,
            baseClass,
            className,
            showError && 'error',
            (readOnly || disabled) && 'read-only'
        ].filter(Boolean).join(' '),
        style: styles,
        children: [
            /*#__PURE__*/ _jsxDEV(RenderCustomComponent, {
                CustomComponent: Label,
                Fallback: /*#__PURE__*/ _jsxDEV(FieldLabel, {
                    label: label,
                    localized: localized,
                    path: path,
                    required: required
                }, void 0, false, {
                    fileName: "src/fields/Code/index.tsx",
                    lineNumber: 115,
                    columnNumber: 11
                }, void 0)
            }, void 0, false, {
                fileName: "src/fields/Code/index.tsx",
                lineNumber: 112,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV("div", {
                className: `${fieldBaseClass}__wrap`,
                children: [
                    /*#__PURE__*/ _jsxDEV(RenderCustomComponent, {
                        CustomComponent: Error,
                        Fallback: /*#__PURE__*/ _jsxDEV(FieldError, {
                            path: path,
                            showError: showError
                        }, void 0, false, {
                            fileName: "src/fields/Code/index.tsx",
                            lineNumber: 121,
                            columnNumber: 21
                        }, void 0)
                    }, void 0, false, {
                        fileName: "src/fields/Code/index.tsx",
                        lineNumber: 119,
                        columnNumber: 9
                    }, this),
                    BeforeInput,
                    /*#__PURE__*/ _jsxDEV(CodeEditor, {
                        defaultLanguage: prismToMonacoLanguageMap[language] || language,
                        onChange: handleChange,
                        onMount: onMount,
                        options: editorOptions,
                        readOnly: readOnly || disabled,
                        recalculatedHeightAt: recalculatedHeightAt,
                        value: stringValueRef.current,
                        wrapperProps: {
                            id: `field-${path?.replace(/\./g, '__')}`
                        },
                        ...editorProps || {}
                    }, void 0, false, {
                        fileName: "src/fields/Code/index.tsx",
                        lineNumber: 124,
                        columnNumber: 9
                    }, this),
                    AfterInput
                ]
            }, void 0, true, {
                fileName: "src/fields/Code/index.tsx",
                lineNumber: 118,
                columnNumber: 7
            }, this),
            /*#__PURE__*/ _jsxDEV(RenderCustomComponent, {
                CustomComponent: Description,
                Fallback: /*#__PURE__*/ _jsxDEV(FieldDescription, {
                    description: description,
                    path: path
                }, void 0, false, {
                    fileName: "src/fields/Code/index.tsx",
                    lineNumber: 141,
                    columnNumber: 19
                }, void 0)
            }, void 0, false, {
                fileName: "src/fields/Code/index.tsx",
                lineNumber: 139,
                columnNumber: 7
            }, this)
        ]
    }, void 0, true, {
        fileName: "src/fields/Code/index.tsx",
        lineNumber: 100,
        columnNumber: 5
    }, this);
};
export const CodeField = withCondition(CodeFieldComponent);

//# sourceMappingURL=index.js.map