//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// PLAIN HELPERS REPRESENTED AS SIMPLE FUNCTIONS —
// FOR INVOCATION FROM WASM (.NET-BLAZOR) AND, POSSIBLY, FROM PLAIN JS (HTML/SCRIPT):
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//==================================================================================================
// ELEMENT HELPERS:
//--------------------------------------------------------------------------------------------------

function Element_GetId(element: HTMLElement): string|null {
   HArgChecking.NOT_NULL(element, 'Element_GetId', 'element')
   return element.id
}

//--------------------------------------------------------------------------------------------------

function Element_IsDisabled(element: HTMLElement): boolean {
   HArgChecking.NOT_NULL(element, 'Element_IsDisabled', 'element')
   return HElement.IsDisabled(element)
}

//--------------------------------------------------------------------------------------------------

function Element_HasAttribute(element: HTMLElement, attributeName: string): boolean {
   HArgChecking.NOT_NULL(element, 'Element_HasAttribute', 'attributeName')
   let elem = element as any
   return elem && elem.getAttribute ? elem.hasAttribute(attributeName) : false
}
function Element_GetAttribute(element: HTMLElement, attributeName: string): string|null {
   HArgChecking.NOT_NULL(element, 'Element_GetAttribute', 'attributeName')
   let elem = element as any
   return elem && elem.getAttribute ? elem.getAttribute(attributeName) : null
}
function Element_SetAttribute( element: HTMLElement,
                               attributeName: string,
                               value: string|null ): boolean {
   HArgChecking.NOT_NULL(element, 'Element_SetAttribute', 'attributeName')
   var elem: any = element
   if (!elem && !elem.setAttribute)
      return false
   elem.setAttribute(attributeName, value)
   return true
}
function Element_RemoveAttribute(element: HTMLElement, attributeName: string): boolean {
   HArgChecking.NOT_NULL(element, 'Element_RemoveAttribute', 'attributeName')
   var elem: any = element
   if (!elem || !elem.removeAttribute)
      return false
   elem.removeAttribute(attributeName)
   return true
}

//--------------------------------------------------------------------------------------------------

function Element_GetStyleProperty(element: HTMLElement, propertyName: string): string|null {
   HArgChecking.NOT_NULL(element, 'Element_GetStyleProperty', 'propertyName')
   return element.style ? element.style.getPropertyValue(propertyName) : null
}
function Element_SetStyleProperty( element: HTMLElement,
                                   propertyName: string,
                                   value: string|null ): boolean {
   HArgChecking.NOT_NULL(element, 'Element_SetStyleProperty', 'propertyName')
   if (!element.style)
      return false
   element.style.setProperty(propertyName, value)
   return true
}
function Element_RemoveStyleProperty(element: HTMLElement, propertyName: string): boolean {
   HArgChecking.NOT_NULL(element, 'Element_RemoveStyleProperty', 'propertyName')
   if (!element.style)
      return false
   element.style.removeProperty(propertyName)
   return true
}

//--------------------------------------------------------------------------------------------------

function Element_ContainsFocus(element: HTMLElement): boolean {
   HArgChecking.NOT_NULL(element, 'Element_ContainsFocus', 'element')
   return HElement.ContainsFocus(element)
}
function Element_IsFocused(element: HTMLElement): boolean {
   HArgChecking.NOT_NULL(element, 'Element_IsFocused', 'element')
   return HElement.IsFocused(element)
}

//--------------------------------------------------------------------------------------------------

function Element_Click(element: HTMLElement): boolean {
   HArgChecking.NOT_NULL(element, 'Element_Click', 'element')
   return HElement.Click(element)
}
function Element_DeferredClick( element: HTMLElement,
                                milliseconds: number=0 ): boolean {
   HArgChecking.NOT_NULL(element, 'Element_DeferredClick', 'element')
   return HElement.DeferredClick(element, milliseconds)
}

function Element_Focus(element: HTMLElement) {
   HArgChecking.NOT_NULL(element, 'Element_Focus', 'element')
   element.focus()
}
function Element_DeferredFocus( element: HTMLElement,
                                milliseconds: number=0 ) {
   HArgChecking.NOT_NULL(element, 'Element_DeferredFocus', 'element')
   HElement.DeferredFocus(element, milliseconds)
}

function Element_Select(element: HTMLElement): boolean {
   HArgChecking.NOT_NULL(element, 'Element_Select', 'element')
   return HElement.Select(element)
}
function Element_DeferredSelect( element: HTMLElement,
                                 milliseconds: number=0 ): boolean {
   HArgChecking.NOT_NULL(element, 'Element_DeferredSelect', 'element')
   return HElement.DeferredSelect(element, milliseconds)
}

function Element_FocusSelect(element: HTMLElement) {
   HArgChecking.NOT_NULL(element, 'Element_FocusSelect', 'element')
   HElement.FocusSelect(element)
}
function Element_DeferredFocusSelect( element: HTMLElement,
                                      milliseconds: number=0 ) {
   HArgChecking.NOT_NULL(element, 'Element_DeferredFocusSelect', 'element')
   HElement.DeferredFocusSelect(element, milliseconds)
}

//--------------------------------------------------------------------------------------------------

function Element_SelectAll(container: HTMLElement): boolean {
   HArgChecking.NOT_NULL(container, 'Element_SelectAll', 'container')
   return HElement.SelectAll(container)
}
function Element_DeferredSelectAll( container: HTMLElement,
                                    milliseconds: number=0 ) {
   HArgChecking.NOT_NULL(container, 'Element_DeferredSelectAll', 'container')
   HElement.DeferredSelectAll(container, milliseconds)
}

//--------------------------------------------------------------------------------------------------

function Element_ScrollIntoView(element: HTMLElement) {
   HArgChecking.NOT_NULL(element, 'Element_ScrollIntoViewElement', 'element')
   element.scrollIntoView()
}
function Element_DeferredScrollTo(element: HTMLElement, milliseconds: number=0) {
   HArgChecking.NOT_NULL(element, 'DeferredScrollIntoView', 'element')
   HElement.DeferredScrollIntoView(element, milliseconds)
}

//==================================================================================================
// BY-ID HELPERS:
//--------------------------------------------------------------------------------------------------

function ById_Exists(id: string): boolean {
   HArgChecking.PROVIDED(id, 'ById_Exists', 'id')
   return HById.Exists(id)
}

function ById_Element(id: string): HTMLElement {
   HArgChecking.PROVIDED(id, 'ById_Element', 'id')
   return HElement.ById(id)
}
function ById_InputElement(id: string): HTMLInputElement {
   HArgChecking.PROVIDED(id, 'ById_InputElement', 'id')
   return HById.InputElement(id)
}

function ById_Focus(id: string) {
   HArgChecking.PROVIDED(id, 'ById_Focus', 'id')
   HElement.ById(id).focus()
}
function ById_Click(id: string) {
   HArgChecking.PROVIDED(id, 'ById_Click', 'id')
   HElement.ById(id).click()
}

function ById_DeferredFocus(id: string, milliseconds: number=0) {
   HArgChecking.PROVIDED(id, 'ById_DeferredFocus', 'id')
   HElement.DeferredFocus(HElement.ById(id), milliseconds)
}
function ById_DeferredClick(id: string, milliseconds: number=0) {
   HArgChecking.PROVIDED(id, 'ById_DeferredClick', 'id')
   HElement.DeferredClick(HElement.ById(id), milliseconds)
}

//--------------------------------------------------------------------------------------------------

function ById_ScrollIntoView(id: string) {
   HArgChecking.PROVIDED(id, 'ById_ScrollIntoView', 'id')
   HElement.ById(id).scrollIntoView()
}
function ById_DeferredScrollIntoView(id: string, milliseconds: number=0) {
   HArgChecking.PROVIDED(id, 'ById_DeferredScrollIntoView', 'id')
   HElement.DeferredScrollIntoView(HElement.ById(id), milliseconds)
}

//==================================================================================================
// HREF HELPERS:
//--------------------------------------------------------------------------------------------------

function HRef_ScrollToId(id: string): boolean {
   HArgChecking.PROVIDED(id, 'HRef_ScrollToId', 'id')
   HElement.ById(id).scrollIntoView()
   return false
}
function HRef_ScrollToPageTop(): boolean {
   Page.ScrollToTop()
   return false
}
function HRef_ScrollToPageBottom(): boolean {
   Page.ScrollToBottom()
   return false
}

function HRef_DeferredScrollToId(id: string): boolean {
   HArgChecking.PROVIDED(id, 'HRef_DeferredScrollToId', 'id')
   HElement.DeferredScrollIntoView(HElement.ById(id))
   return false
}
function HRef_DeferredScrollToPageTop(): boolean {
   Page.DeferredScrollToTop()
   return false
}
function HRef_DeferredScrollToPageBottom(): boolean {
   Page.DeferredScrollToBottom()
   return false
}

function HRef_EscapeFocus(id: string|null=null) {
   if (id)
      HElement.ById(id).focus()
   else
      Page.FocusBackground()
}

//==================================================================================================
// CONSOLE HELPERS (TRACE):
//--------------------------------------------------------------------------------------------------

function Console_PrintError(message: string) {
   HArgChecking.NOT_NULL(message, 'Console_PrintError', 'message')
   console.error(message)
}

//==================================================================================================
