'use client';

import { jsx as _jsx } from "react/jsx-runtime";
import { useControllableState } from '@payloadcms/ui';
import React, { createContext, use, useMemo } from 'react';
const RichTextViewContext = /*#__PURE__*/createContext({
  currentView: 'default',
  inheritable: false,
  setCurrentView: () => {}
});
/**
 * Provider component for rich text view context.
 *
 * This provider manages the current view state internally and makes it accessible to child components
 * via the useRichTextView hook. It automatically resolves the current view's node map
 * based on the active view name.
 *
 * @example
 * ```tsx
 * <RichTextViewProvider
 *   currentView="frontend"
 *   views={myViews}
 * >
 *   <MyEditor />
 * </RichTextViewProvider>
 * ```
 */
export const RichTextViewProvider = args => {
  const parentContext = useRichTextView();
  const {
    children,
    currentView: currentViewFromProps,
    inheritable,
    views
  } = parentContext.inheritable ? {
    ...parentContext,
    ...args
  } : args;
  const [currentView, setCurrentView] = useControllableState(currentViewFromProps, 'default');
  const value = useMemo(() => {
    const currentViewMap = views ? views[currentView] || views.default : undefined;
    return {
      currentView,
      currentViewMap,
      inheritable,
      setCurrentView,
      views
    };
  }, [currentView, inheritable, setCurrentView, views]);
  return /*#__PURE__*/_jsx(RichTextViewContext, {
    value: value,
    children: children
  });
};
/**
 * Hook to access the current rich text view context.
 *
 * Use this hook to access the currently active view and its node map,
 * or to programmatically switch between views.
 *
 * @returns An object containing:
 * - `currentView`: The name of the active view
 * - `currentViewMap`: The node overrides for the current view
 * - `setCurrentView`: Function to change views
 * - `views`: All available views
 *
 * @example
 * ```tsx
 * function MyComponent() {
 *   const { currentView, currentViewMap, setCurrentView } = useRichTextView()
 *
 *   return (
 *     <div>
 *       <p>Current view: {currentView}</p>
 *       {currentViewMap?.heading && <p>Heading overrides are active</p>}
 *       <button onClick={() => setCurrentView('frontend')}>
 *         Switch to frontend view
 *       </button>
 *     </div>
 *   )
 * }
 * ```
 */
export function useRichTextView() {
  return use(RichTextViewContext);
}
//# sourceMappingURL=RichTextViewProvider.js.map