'use client';

import { c as _c } from "react/compiler-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
import { createElement as _createElement } from "react";
import { ShimmerEffect, useConfig } from '@payloadcms/ui';
import React, { lazy, Suspense, useEffect, useState } from 'react';
import { defaultEditorLexicalConfig } from '../lexical/config/client/default.js';
import { loadClientFeatures } from '../lexical/config/client/loader.js';
import { sanitizeClientEditorConfig } from '../lexical/config/client/sanitize.js';
import { RichTextViewProvider, useRichTextView } from './RichTextViewProvider.js';
const RichTextEditor = /*#__PURE__*/lazy(() => import('./Field.js').then(module => ({
  default: module.RichText
})));
export const RichTextField = props => {
  return /*#__PURE__*/_jsx(RichTextViewProvider, {
    views: props.views,
    children: /*#__PURE__*/_jsx(RichTextFieldImpl, {
      ...props
    })
  });
};
export const RichTextFieldImpl = props => {
  const $ = _c(19);
  const {
    admin: t0,
    clientFeatures,
    featureClientImportMap: t1,
    featureClientSchemaMap,
    field,
    lexicalEditorConfig: t2,
    schemaPath,
    views
  } = props;
  let t3;
  if ($[0] !== t0) {
    t3 = t0 === undefined ? {} : t0;
    $[0] = t0;
    $[1] = t3;
  } else {
    t3 = $[1];
  }
  const _admin = t3;
  let t4;
  if ($[2] !== t1) {
    t4 = t1 === undefined ? {} : t1;
    $[2] = t1;
    $[3] = t4;
  } else {
    t4 = $[3];
  }
  const featureClientImportMap = t4;
  const _lexicalEditorConfig = t2 === undefined ? defaultEditorLexicalConfig : t2;
  const {
    currentView
  } = useRichTextView();
  const currentViewAdminConfig = views?.[currentView]?.admin ?? _admin;
  const currentViewLexicalEditorConfig = views?.[currentView]?.lexical ?? _lexicalEditorConfig;
  const {
    config
  } = useConfig();
  const [finalSanitizedEditorConfig, setFinalSanitizedEditorConfig] = useState(null);
  let t5;
  let t6;
  if ($[4] !== clientFeatures || $[5] !== config || $[6] !== currentView || $[7] !== currentViewAdminConfig || $[8] !== currentViewLexicalEditorConfig || $[9] !== featureClientImportMap || $[10] !== featureClientSchemaMap || $[11] !== field || $[12] !== finalSanitizedEditorConfig || $[13] !== schemaPath) {
    t5 = () => {
      if (finalSanitizedEditorConfig && finalSanitizedEditorConfig.view === currentView) {
        return;
      }
      const featureProvidersLocal = [];
      for (const clientFeature of Object.values(clientFeatures)) {
        if (!clientFeature.clientFeatureProvider) {
          continue;
        }
        featureProvidersLocal.push(clientFeature.clientFeatureProvider(clientFeature.clientFeatureProps));
      }
      const resolvedClientFeatures = loadClientFeatures({
        config,
        featureClientImportMap,
        featureClientSchemaMap,
        field,
        schemaPath: schemaPath ?? field.name,
        unSanitizedEditorConfig: {
          features: featureProvidersLocal,
          lexical: currentViewLexicalEditorConfig
        }
      });
      setFinalSanitizedEditorConfig(sanitizeClientEditorConfig(resolvedClientFeatures, currentViewLexicalEditorConfig, currentViewAdminConfig, currentView));
    };
    t6 = [currentViewAdminConfig, clientFeatures, config, featureClientImportMap, featureClientSchemaMap, field, finalSanitizedEditorConfig, currentViewLexicalEditorConfig, schemaPath, currentView];
    $[4] = clientFeatures;
    $[5] = config;
    $[6] = currentView;
    $[7] = currentViewAdminConfig;
    $[8] = currentViewLexicalEditorConfig;
    $[9] = featureClientImportMap;
    $[10] = featureClientSchemaMap;
    $[11] = field;
    $[12] = finalSanitizedEditorConfig;
    $[13] = schemaPath;
    $[14] = t5;
    $[15] = t6;
  } else {
    t5 = $[14];
    t6 = $[15];
  }
  useEffect(t5, t6);
  let t7;
  if ($[16] !== finalSanitizedEditorConfig || $[17] !== props) {
    t7 = _jsx(Suspense, {
      fallback: _jsx(ShimmerEffect, {
        height: "35vh"
      }),
      children: finalSanitizedEditorConfig && _createElement(RichTextEditor, {
        ...props,
        editorConfig: finalSanitizedEditorConfig,
        key: finalSanitizedEditorConfig.view
      })
    });
    $[16] = finalSanitizedEditorConfig;
    $[17] = props;
    $[18] = t7;
  } else {
    t7 = $[18];
  }
  return t7;
};
//# sourceMappingURL=index.js.map