import type { HTMLTagsType } from '@intlayer/core/transpiler';
import type { ComponentProps, FC, HTMLAttributes, JSX } from 'react';
/**
* Helper to map string types from dictionary to TypeScript types
*/
type PropTypeMap = T extends 'string'
? string
: T extends 'number'
? number
: T extends 'boolean'
? boolean
: T;
/**
* Helper to extract specific props from the configuration value.
*/
type PropsFromConfig = Value extends true
? {}
: Value extends object
? { [K in Exclude]?: PropTypeMap }
: {};
/**
* Smart Type to resolve props based on the key K.
*/
type ElementProps = K extends keyof JSX.IntrinsicElements
? ComponentProps
: HTMLAttributes;
/**
* Helper: Defines the mapping for the explicitly listed keys in T.
* Handles whether they are Required or Optional.
*/
type DefinedComponents = IsRequired extends true
? {
// Required Case
[K in keyof T]: FC & PropsFromConfig>;
}
: {
// Optional Case
[K in keyof T]?: FC & PropsFromConfig>;
};
/**
* Helper: Defines the standard HTML tags NOT listed in T.
* These are always optional when included.
*/
type RestHTMLComponents = {
[K in Exclude]?: FC<
K extends keyof JSX.IntrinsicElements
? ComponentProps
: HTMLAttributes
>;
};
/**
* The supported modes for the HTMLComponents type.
*/
export type HTMLComponentMode =
| 'permissive' // T is optional, HTML is optional, unknown strings allowed
| 'optional' // T is optional, HTML is optional (Default)
| 'inclusive' // T is required, HTML is optional
| 'strict'; // T is required, HTML is forbidden
/**
* The main component definition with Mode support.
*/
export type HTMLComponents<
Mode extends HTMLComponentMode = 'optional',
T = {},
> = Mode extends 'strict'
? // Strict: Only keys in T are allowed, and they are required.
DefinedComponents
: Mode extends 'inclusive'
? // Inclusive: Keys in T are required. Rest of HTML is optional.
DefinedComponents & RestHTMLComponents
: Mode extends 'permissive'
? // Permissive: Keys in T optional. Rest of HTML optional. Any other string allowed.
DefinedComponents &
RestHTMLComponents & {
[key: string]: FC;
}
: // Optional (Default): Keys in T optional. Rest of HTML optional.
DefinedComponents & RestHTMLComponents;