import { ConstructorType, GenericClassDecorator, ReflectUtils } from '@dooboostore/core'; import { SimConfigProxy, SimConfigUsing } from '@dooboostore/simple-boot'; export const componentSelectors = new Map>(); export interface ComponentConfig { selector?: string; noStrip?: boolean; template?: string | ((instance: T) => Promise); styles?: (string)[] | string | ((instance: T) => Promise); proxy?: SimConfigProxy; using?: SimConfigUsing; } export const ComponentMetadataKey = Symbol.for('simple-boot-front:component-metadata'); // Partial const componentProcess = (config: ComponentConfig, target: ConstructorType) => { if (!config.selector) { config.selector = target.name.toLowerCase(); } ReflectUtils.defineMetadata(ComponentMetadataKey, config, target); componentSelectors.set(config.selector.toLowerCase(), target); } export function Component(target: ConstructorType): void; export function Component(config: ComponentConfig): GenericClassDecorator>; export function Component(configOrTarget: ConstructorType | ComponentConfig): void | GenericClassDecorator> { if (typeof configOrTarget === 'function') { componentProcess({}, configOrTarget); } else { return (target: ConstructorType) => { componentProcess(configOrTarget, target); } } } export const getComponent = (target: ConstructorType | Function | any): ComponentConfig | undefined => { if (target && typeof target === 'object') { target = target.constructor; } try { return ReflectUtils.getMetadata(ComponentMetadataKey, target); } catch (e) { } } // Alias for @component (lowercase) export const component = Component;