import { createMemo , untrack , createSignal , onError , children , Accessor } from "../reactive/signal" ; import { mapArray , indexArray } from "../reactive/array" ; import { sharedConfig } from "./hydration" ; import type { JSX } from "../jsx" ; description export function For < T , U extends JSX . Element > ( props ) { const fallback = "fallback" in props && { fallback : ( ) => props . fallback } ; return createMemo ( mapArray < T , U > ( ( ) => props . each , props . children , fallback ? fallback : undefined ) ) ; } description export function Index < T , U extends JSX . Element > ( props ) { const fallback = "fallback" in props && { fallback : ( ) => props . fallback } ; return createMemo ( indexArray < T , U > ( ( ) => props . each , props . children , fallback ? fallback : undefined ) ) ; } description export function Show < T > ( props ) { let strictEqual = false ; const condition = createMemo < T | undefined | null | boolean > ( ( ) => props . when , undefined , { equals : ( a , b ) => ( strictEqual ? a === b : ! a === ! b ) } ) ; return createMemo ( ( ) => { const c = condition ( ) ; if ( c ) { const child = props . children ; return ( strictEqual = typeof child === "function" && child . length > 0 ) ? untrack ( ( ) => ( child as any ) ( c as T ) ) : child ; } return props . fallback ; } ) as ( ) => JSX . Element ; } type EvalConditions = [ number , unknown ? , MatchProps < unknown > ? ] ; description export function Switch ( props ) { let strictEqual = false ; const conditions = children ( ( ) => props . children ) as unknown as ( ) => MatchProps < unknown > [ ] , evalConditions = createMemo ( ( ) : EvalConditions => { let conds = conditions ( ) ; if ( ! Array . isArray ( conds ) ) conds = [ conds ] ; for ( let i = 0 ; i < conds . length ; i ++ ) { const c = conds [ i ] . when ; if ( c ) return [ i , c , conds [ i ] ] ; } return [ - 1 ] ; } , undefined , { equals : ( a , b ) => a [ 0 ] === b [ 0 ] && ( strictEqual ? a [ 1 ] === b [ 1 ] : ! a [ 1 ] === ! b [ 1 ] ) && a [ 2 ] === b [ 2 ] } ) ; return createMemo ( ( ) => { const [ index , when , cond ] = evalConditions ( ) ; if ( index < 0 ) return props . fallback ; const c = cond ! . children ; return ( strictEqual = typeof c === "function" && c . length > 0 ) ? untrack ( ( ) => ( c as any ) ( when ) ) : c ; } ) ; } export type MatchProps < T > = { when : T | undefined | null | false ; children : JSX . Element | ( ( item : NonNullable < T > ) => JSX . Element ) ; } ; description export function Match < T > ( props ) { return props as unknown as JSX . Element ; } description export function ErrorBoundary ( props ) { let err = undefined ; if ( sharedConfig ! . context && sharedConfig ! . load ) { err = sharedConfig . load ( sharedConfig . context . id + sharedConfig . context . count ) ; } const [ errored , setErrored ] = createSignal < any > ( err ) ; let e ; return createMemo ( ( ) => { if ( ( e = errored ( ) ) != null ) { const f = props . fallback ; return typeof f === "function" && f . length ? untrack ( ( ) => f ( e , ( ) => setErrored ( null ) ) ) : f ; } onError ( setErrored ) ; return props . children ; } ) as Accessor < JSX . Element > ; }