import React, { useMemo, useRef } from 'react';
import { useWixPatternsContainer } from '@wix/bex-core/react';
import { SchemaContext } from './SchemaContext';
import { CmsFieldModalState } from '../components/CmsFieldManagement/CmsFieldModalState';
import { CmsFieldModal } from '../components/CmsFieldManagement/CmsFieldModal';
export const SchemaProvider = ({ schema, children }) => {
    const container = useWixPatternsContainer();
    const schemaRef = useRef(schema);
    schemaRef.current = schema;
    const modalState = useMemo(() => new CmsFieldModalState(() => schemaRef.current, (e) => container.errorHandler?.getResolvedError?.(e) ?? {
        message: e instanceof Error ? e.message : String(e),
    }), [container]);
    const value = useMemo(() => ({
        schema,
        ...(schema.fieldActions?.addField && {
            addField: () => modalState.open(),
        }),
        ...(schema.fieldActions?.editField && {
            editField: (fieldId) => modalState.open(fieldId),
        }),
    }), [schema, modalState]);
    return (React.createElement(SchemaContext.Provider, { value: value },
        children,
        React.createElement(CmsFieldModal, { state: modalState })));
};
//# sourceMappingURL=SchemaProvider.js.map