/** * react-candlesticks, trendingcandles.com * * Copyright (c) 2026 Jason Wilson * Licensed under the MIT License (see LICENSE file in the project root). */ import { FunctionComponent } from 'react'; import Layer from '../config/layer/Layer'; import { BaseLayerConfig, BaseLayerConfigComplete } from '../config/layer/BaseLayerConfig'; import LAYER_COMPONENT_TYPE_KEY from '../config/layer/layerComponentTypeKey'; export type LayerComponent = FunctionComponent>; export interface LayerDefinition< C extends BaseLayerConfig = BaseLayerConfig, Complete extends BaseLayerConfigComplete = BaseLayerConfigComplete, > extends Layer { type: C['type']; Component: LayerComponent; } export type DefineLayerOptions< C extends BaseLayerConfig, Complete extends BaseLayerConfigComplete, > = Layer & { type: C['type']; displayName?: string; }; export type LayerDefinitionConfig = D extends LayerDefinition ? C : never; export type LayerDefinitionConfigComplete = D extends LayerDefinition ? Complete : never; export type CustomLayerComponent = LayerComponent; export type CustomLayerDefinition< C extends BaseLayerConfig = BaseLayerConfig, Complete extends BaseLayerConfigComplete = BaseLayerConfigComplete, > = LayerDefinition; export type CustomLayerOptions< C extends BaseLayerConfig, Complete extends BaseLayerConfigComplete, > = DefineLayerOptions; const defineLayer = < C extends BaseLayerConfig, Complete extends BaseLayerConfigComplete, >( options: DefineLayerOptions, ): LayerDefinition => { const { type, displayName = `${type}Layer`, ...layer } = options; if (!type) { throw new Error('Layer type must be a non-empty string'); } const Component = (() => null) as LayerComponent & { displayName?: string; [LAYER_COMPONENT_TYPE_KEY]?: string; }; Component.displayName = displayName; Component[LAYER_COMPONENT_TYPE_KEY] = type; return { ...layer, type, Component, } as LayerDefinition; }; export default defineLayer;