import { Template , createCache } from './template' ; import type { TemplateCompiler , TemplateCache } from './template' ; import { isObject } from '@kirei/shared' ; import { clearNode } from './shared' ; export { defaultCompiler } from './compiler' ; export { Template , createCache } from './template' ; export type { TemplateCompiler } from './template' ; export { LRUWeakMap } from './shared' ; export type { TemplatePatcher } from './compiler' ; type Key = string | number | null | undefined ; type RootContainer = Element | ShadowRoot | DocumentFragment ; const rendered = new WeakMap < RootContainer , TemplateCache > ( ) ; const { html , svg , render } = customize ( { } ) ; export { html , svg , render } ; interface RenderOptions { /**
     * Scope name to inform what tagName the targeted root has, only required for Shady shims.
     */ scopeName ? : string ; /**
     * If false render will only compile the template and not render to root.
     * Essentially preparing for a render but not actually applying it, defaults to true
     */ mount ? : boolean ; } /**
 * Template literal to template markup with interpolated values.
 * Also has literals as static members as utility functions.
 */ export interface TemplateLiteral { param strings param values returns ( strings , ... values ) : Template ; param ref param key param template returns key ( ref , template ) : Node ; key ( ref , key , template ) : Node ; } /**
 * Custom compiler options
 */ interface CompilerOptions < T extends Partial < TemplateLiteral > > { /**
     * Custom compiler to use instead of the default,
     * will fallback to defaults if compiler does not implement all the members
     */ compiler ? : TemplateCompiler ; /**
     * Helper methods to assign to the returned TemplateLiteral as static members
     */ literals ? : T ; } /**
 * Template renderer object
 */ interface TemplateRenderer < T extends TemplateLiteral > { /**
     * Creates a template with html content
     */ html : T ; /**
     * Creates a template with svg content
     */ svg : T ; param template param root param renderOptions returns render ( template , root , renderOptions ? ) : Node ; render ( template , root , renderOptions ? ) : void ; render ( template , root , renderOptions ? ) : Node | null ; } param opts returns export function customize < T extends TemplateLiteral > ( opts ) { const { compiler } = opts ; return { html : createLiteral ( 'html' , opts ) , svg : createLiteral ( 'svg' , opts ) , render ( template , root , renderOptions = { } ) { const { scopeName , mount } = renderOptions ; let cache = rendered . get ( root ) ; if ( template != null ) { if ( ! cache ) { rendered . set ( root , ( cache = createCache ( ) ) ) ; } const wire = cache . node ; let node ; if ( template instanceof Template ) { node = template . update ( cache , compiler , scopeName ) ; } else if ( template instanceof Node ) { node = template ; } else { throw new TypeError ( 'Invalid render template, expected Template or Node' ) ; } if ( ! root . childNodes . length || wire !== node ) { if ( mount == null || mount ) { clearNode ( root ) ; root . appendChild ( node . valueOf ( ) as Node ) ; } } return node ; } if ( cache ) { clearNode ( root ) ; cache . node = null ; } } , } ; } param type param opts returns private function createLiteral < T extends TemplateLiteral > ( type , opts ) { const { compiler , literals } = opts ; const keyed = new WeakMap < any , Map < any , TemplateCache > > ( ) ; const template = ( strings , ... values ) => new Template ( type , strings , values ) ; template . key = ( ref , key , template ? ) => { if ( ! template ) { template = key as Template ; key = void 0 ; } let memo = keyed . get ( ref ) ; if ( ! memo ) { keyed . set ( ref , memo = new Map ( ) ) ; } let cache = memo . get ( key ) ; if ( ! cache ) { memo . set ( key , cache = createCache ( ) ) ; } return template . update ( cache , compiler ) ; } ; if ( isObject ( literals ) ) { for ( let key of Object . keys ( literals ) ) { if ( key in template ) { throw new TypeError ( 'Cannot override properties in literals' ) ; } template [ key ] = literals [ key ] ; } } return template as T ; }