import type { Component } from '../' ; const hasMap = typeof Map !== 'undefined' ; const hasSet = typeof Set !== 'undefined' ; /**
 * Helper functions, used in templates and component definitions.
 * The fist argument of every helper *must* be a component!
 */ /**
 * Dispatches bubbling `type` event on given `elem` element
 */ export function emit < T extends Element > ( elem , type , detail ? ) { elem . dispatchEvent ( new CustomEvent ( type , { bubbles : true , cancelable : true , composed : true , detail } ) ) ; return elem ; } /**
 * Same as `emit` but doesn’t bubble up event. A convenience for notifying component
 * subscribers only.
 */ export function notify < T extends Element > ( elem , type , detail ? ) { elem . dispatchEvent ( new CustomEvent ( type , { bubbles : false , cancelable : false , detail } ) ) ; return elem ; } /**
 * Subscribes on given `type` event on `elem` element
 */ export function on < T extends Element > ( elem , type , callback , options ? ) { elem . addEventListener ( type , callback , options ) ; return elem ; } /**
 * Unsubscribes from given `type` event on `elem` element
 */ export function off < T extends Element > ( elem , type , callback , options ? ) { elem . removeEventListener ( type , callback , options ) ; return elem ; } /**
 * Returns amount of elements in given collection. For non-collections returns
 * `0` for `null` and `undefined, `1` for all other types
 */ export function count ( component , obj ) { if ( obj == null ) { return 0 ; } if ( Array . isArray ( obj ) ) { return obj . length ; } if ( ( hasMap && obj instanceof Map ) || ( hasSet && obj instanceof Set ) ) { return obj . size ; } return 1 ; } /**
 * Check if given `value` exists in `collection`
 */ export function contains ( component , collection , value ) { if ( hasSet && collection instanceof Set ) { return collection . has ( value ) ; } if ( hasMap && collection instanceof Map ) { return contains ( component , Array . from ( collection . values ( ) ) , value ) ; } if ( Array . isArray ( collection ) || ( typeof collection === 'string' && value != null ) ) { return collection . indexOf ( value ) !== - 1 ; } return collection != null ? collection === value : false ; } /**
 * Returns index of given `value` in collection
 */ export function indexOf ( component , data , value ) { return data != null && data . indexOf ? data . indexOf ( value ) : - 1 ; } /**
 * Replaces given `pattern` in string with `value`
 */ export function replace ( component , str , pattern , value ) { return str != null ? String ( str ) . replace ( pattern , value ) : '' ; } /**
 * Returns lowercase version of given string
 */ export function lowercase ( component , str ) { return str != null ? String ( str ) . toLowerCase ( ) : '' ; } /**
 * Returns uppercase version of given string
 */ export function uppercase ( component , str ) { return str != null ? String ( str ) . toUpperCase ( ) : '' ; } /**
 * Returns slice of given string or array
 */ export function slice < T > ( component , data , start ? , end ? ) ; export function slice ( component , data , start ? , end ? ) ; export function slice ( component , data , start ? , end ? ) { return Array . isArray ( data ) || typeof data === 'string' ? data . slice ( start , end ) : null ; } /**
 * Logs given data
 */ export function log ( component , ... args ) { console . log ( ... args ) ; } /**
 * Returns slot container with given name
 */ export function getSlot ( component , name ) { const slot = component . componentModel && component . componentModel . input . slots ! [ name ] ; return slot && slot . element ; }