import { useDataAttributes } from '@bento/use-data-attributes'; import { type BoxContext } from '@bento/box'; import { type ComponentType } from 'react'; /** * Interface representing the arguments required for replacing a component. * * @interface ReplaceArgs * @property {Props} props - The properties of the component. * @property {ComponentType} Component - The component which should be rendered. * @property {SlotContext} context - The current Slot context. * @property {string} name - The name of the component. * @private */ interface ReplaceArgs { props: Props; Component: ComponentType; context: BoxContext; name: string; } /** * Interface for props override objects * @private */ interface PropsOverride { /** The properties of the props override. */ props: Record; } /** * Checks if the provided data is a props override instead of a component * override. * * @param data - The data to check. * @returns Returns true if the data is a props override, false otherwise. * @private */ function isPropsOverride(data: any): data is PropsOverride { if (typeof data !== 'object' || Array.isArray(data)) return false; const keys = Object.keys(data); return keys.includes('props') && keys.length === 1; } /** * Replaces a component in the context with the specified name and props. * * @param args.props - The properties to be passed to the component. * @param args.name - The name of the component to be replaced. * @param args.context - The context containing the components. * @returns An object containing the updated props, the component to be replaced, and the updated context. * @public */ export function replace>({ props, name, context }: ReplaceArgs) { if (!context.env || !context.env.components || !(name in context.env.components)) return; const target = context.env.components[name]; if (!target) return; const result = { context: { slots: { override: !!target } }, props: {} } as any; const causes = (props['data-override'] || '').split(' ').filter(Boolean); if (!causes.includes('context')) causes.push('context'); result.props = useDataAttributes({ override: causes }); if (isPropsOverride(target)) result.props = { ...target.props }; else result.Component = target; return result; }